I'm taking a photo of a camera and passing it to a ImagemView
and would like to take the predominant color of this image, how could I do this on Android?
my class proximoLaiout (I wrote laiout with i for wanting kk)
package com.example.edras.comeconovo;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.graphics.Palette;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.support.v7.graphics.Palette;
public class ProximoLaiout extends ActionBarActivity {
Button btn1;
int CAMERA_PIC_REQUEST = 0;
ImageView imgView;
int colorpublic;
public Palette.PaletteAsyncListener mListener;
public Bitmap mBitmap;
@Override
// abrindo a camera e armazenando a foto
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_proximo_laiout);
imgView = (ImageView) findViewById(R.id.img_view);
btn1 = (Button) findViewById(R.id.btnCores);
btn1.setCompoundDrawables(null, getResources().getDrawable(R.drawable.olho), null, null);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera_intent, CAMERA_PIC_REQUEST);
}
});
}
@Override
// exibir a foto no imageview
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imgView.setImageBitmap(thumbnail);
mListener = new PaletteListener();
Palette.generateAsync(thumbnail, mListener);
}
}
}
public void clickParaIrConfiguracoes(View view) {
Intent it = new Intent(this, Configuracoes.class);
startActivity(it);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_proximo_laiout, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and in this case the PaletteListener
package com.example.edras.comeconovo;
import android.support.v7.graphics.Palette;
public class PaletteListener implements Palette.PaletteAsyncListener{
@Override
public void onGenerated(Palette palette){
// Pega duas cores predominantes, uma vibrante e uma "muda"(cor com tom cinza) e
//separa elas em três categorias: normal, leve e escuro.
int vibrant = palette.getVibrantColor(0x000000);
int vibrantLight = palette.getLightVibrantColor(0x000000);
int vibrantDark = palette.getDarkVibrantColor(0x000000);
int muted = palette.getMutedColor(0x000000);
int mutedLight = palette.getLightMutedColor(0x000000);
int mutedDark = palette.getDarkMutedColor(0x000000);
}
}