How do I get the predominant color in an image?

6

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);

    }
}
    
asked by anonymous 05.08.2015 / 03:10

1 answer

7

You can use the Palette library of Android as it allows you to extract the color of the image in a simple way (without having to use histograms, etc.). This library allows the generation of predominant color palettes in an image asynchronously (outside the UIThread) and synchronously (within the UIThread). In this example I will explain how to make the generation of the pellet in an asynchronous way, which is more appropriate. So we'll need to do the following:

  • Generate the color palette asynchronously
  • Create a class that is listener, that is, it will receive the color palette as soon as it is generated.
  • Well, if you're in Android Studio, to use the library in your project, add the following line within dependencies to your build.gradle(app) .

    compile 'com.android.support:palette-v7:21.0.0'
    

    Now let's go to the steps.

  • In some class, use the following code to generate a predominant color palette from an image.

    // Gera, de maneira assíncrona, uma paleta de cores baseadas 
    // na sua imagem 'myBitmap' e envia o resultado para um listener 'myListener'.
    Palette palette = Palette.generateAsync(myBitmap,myListener);
    
  • Create a listener class that implements the public void onGenerated(Palette palette) method of the Palette.PaletteAsyncListener interface. This method is called as soon as the listener receives the generated palette.

      @Override
      public void onGenerated(Palette palette) {
          // Acesse o palette gerado e faça o tratamento aqui.
      }
    
  • A practical example would be:

  • MinhaAcitivity class

     public class MinhaActivity extends AppCompatActivity{
    
        private Palette.PaletteAsyncListener mListener;
        private Bitmap mBitmap;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.meuLayout);
    
            mBitmap = // Realiza os métodos para acessar a foto
    
            mListener = new PaletteListener();
            Palette.generateAsync(mBitmap,mListener);
        }
    }
    
  • PaletteListener class

    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);
        }
    }
    
  • 05.08.2015 / 18:06