6
I’m taking a picture of a camera and passing it to a ImagemView
and would like to take the predominant color of this image, how could do this on Android?
my next classLaiout ( 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 the case of 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);
}
}
A recommendation would be to use the library Palette, Google. With it you can get this information easily.
– Wakim
I will not provide an answer because I have no way to test it (I am without the development environment for Android installed). But the principle is simple: build a histogram (more precisely, a color histogram), and select the color that appears most often (i.e., with the largest "bin").
– Luiz Vieira
You do not need to do an instogram for each separate RGB. You can directly count the occurrence number of each combined value (R+G+B). If I’m not mistaken, the method
Bitmap::getPixel
is the one used to access pixel values. That question from Soen may also be of some help.– Luiz Vieira
you have some example of Palette ? I can’t find any working to use as a base ...
– Edras Cremasco
This stackoverflow post can help you believe i
 
 http://stackoverflow.com/questions/8471236/finding-the-dominant-color-of-an-image-in-an-android-drawable
– Caique Oliveira