Tuesday, September 29, 2015

Extract Prominent Colors from an Image, using Palette class



The Android Support Library r21 and above includes the Palette class, which lets you extract prominent colors from an image. This class extracts the following prominent colors:
- Vibrant
- Vibrant dark
- Vibrant light
- Muted
- Muted dark
- Muted light

reference: http://developer.android.com/training/material/drawables.html#ColorExtract

This example show how to load photos, and get Prominent Colors using Palette class.
(Actually I don't know what the Prominent Colors means!)

To use the Palette class in your project, add the following Gradle dependency to your app's module:
dependencies {
    ...
    compile 'com.android.support:palette-v7:xx.x.x'
}


com.blogspot.android_er.androidpalette.MainActivity.java
package com.blogspot.android_er.androidpalette;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.FileNotFoundException;

public class MainActivity extends AppCompatActivity {

    Button buttonOpen;
    TextView textUri;
    ImageView imageView;
    TextView textVibrant, textVibrantDark, textVibrantLight;
    TextView textMuted, textMutedDark, textMutedLight;
    View viewVibrant, viewVibrantDark, viewVibrantLight;
    View viewMuted, viewMutedDark, viewMutedLight;

    private static final int RQS_OPEN_IMAGE = 1;

    Uri targetUri = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textUri = (TextView) findViewById(R.id.texturi);
        imageView = (ImageView) findViewById(R.id.image);
        buttonOpen = (Button) findViewById(R.id.btnopen);
        buttonOpen.setOnClickListener(buttonOpenOnClickListener);

        textVibrant = (TextView)findViewById(R.id.textVibrant);
        textVibrantDark = (TextView)findViewById(R.id.textVibrantDark);
        textVibrantLight = (TextView)findViewById(R.id.textVibrantLight);
        textMuted = (TextView)findViewById(R.id.textMuted);
        textMutedDark = (TextView)findViewById(R.id.textMutedDark);
        textMutedLight = (TextView)findViewById(R.id.textMutedLight);

        viewVibrant = (View)findViewById(R.id.viewVibrant);
        viewVibrantDark = (View)findViewById(R.id.viewVibrantDark);
        viewVibrantLight = (View)findViewById(R.id.viewVibrantLight);
        viewMuted = (View)findViewById(R.id.viewMuted);
        viewMutedDark = (View)findViewById(R.id.viewMutedDark);
        viewMutedLight = (View)findViewById(R.id.viewMutedLight);
    }

    View.OnClickListener buttonOpenOnClickListener =
            new View.OnClickListener() {

                @TargetApi(Build.VERSION_CODES.KITKAT)
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();

                    if (Build.VERSION.SDK_INT >=
                            Build.VERSION_CODES.KITKAT) {
                        intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                    } else {
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                    }

                    intent.addCategory(Intent.CATEGORY_OPENABLE);

                    // set MIME type for image
                    intent.setType("image/*");

                    startActivityForResult(intent, RQS_OPEN_IMAGE);
                }

            };

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected void onActivityResult(int requestCode,
                                    int resultCode, Intent data) {

        if (resultCode == Activity.RESULT_OK) {

            Uri dataUri = data.getData();

            if (requestCode == RQS_OPEN_IMAGE) {
                targetUri = dataUri;
                textUri.setText(dataUri.toString());
                updatImage(dataUri);
            }
        }

    }

    private void updatImage(Uri uri){

        if (uri != null){
            Bitmap bm;
            try {
                bm = BitmapFactory.decodeStream(
                        getContentResolver()
                                .openInputStream(uri));
                imageView.setImageBitmap(bm);

                extractProminentColors(bm);

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    //extract prominent colors
    /*
    compile 'com.android.support:palette-v7:23.0.1'
    is needed in Gradle dependency
     */
    private void extractProminentColors(Bitmap bitmap){
        int defaultColor = 0x000000;

        Palette p = Palette.from(bitmap).generate();

        int VibrantColor = p.getVibrantColor(defaultColor);
        textVibrant.setText("VibrantColor: " + String.format("#%X", VibrantColor));
        viewVibrant.setBackgroundColor(VibrantColor);

        int VibrantColorDark = p.getDarkVibrantColor(defaultColor);
        textVibrantDark.setText("VibrantColorDark: " + String.format("#%X", VibrantColorDark));
        viewVibrantDark.setBackgroundColor(VibrantColorDark);

        int VibrantColorLight = p.getLightVibrantColor(defaultColor);
        textVibrantLight.setText("VibrantColorLight: " + String.format("#%X", VibrantColorLight));
        viewVibrantLight.setBackgroundColor(VibrantColorLight);

        int MutedColor = p.getMutedColor(defaultColor);
        textMuted.setText("MutedColor: " + String.format("#%X", MutedColor));
        viewMuted.setBackgroundColor(MutedColor);

        int MutedColorDark = p.getDarkMutedColor(defaultColor);
        textMutedDark.setText("MutedColorDark: " + String.format("#%X", MutedColorDark));
        viewMutedDark.setBackgroundColor(MutedColorDark);

        int MutedColorLight = p.getLightMutedColor(defaultColor);
        textMutedLight.setText("MutedColorLight: " + String.format("#%X", MutedColorLight));
        viewMutedLight.setBackgroundColor(MutedColorLight);

    }

}


layout/activity_main.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"/>
    <Button
        android:id="@+id/btnopen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Load image"/>
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/texturi"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"/>

            <TextView
                android:id="@+id/textVibrant"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Vibrant"/>
            <View
                android:id="@+id/viewVibrant"
                android:layout_width="match_parent"
                android:layout_height="25dp"/>

            <TextView
                android:id="@+id/textVibrantDark"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="VibrantDark"/>
            <View
                android:id="@+id/viewVibrantDark"
                android:layout_width="match_parent"
                android:layout_height="25dp"/>

            <TextView
                android:id="@+id/textVibrantLight"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="VibrantLight"/>
            <View
                android:id="@+id/viewVibrantLight"
                android:layout_width="match_parent"
                android:layout_height="25dp"/>

            <TextView
                android:id="@+id/textMuted"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Muted"/>
            <View
                android:id="@+id/viewMuted"
                android:layout_width="match_parent"
                android:layout_height="25dp"/>

            <TextView
                android:id="@+id/textMutedDark"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="MutedDark"/>
            <View
                android:id="@+id/viewMutedDark"
                android:layout_width="match_parent"
                android:layout_height="25dp"/>

            <TextView
                android:id="@+id/textMutedLight"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="MutedLight"/>
            <View
                android:id="@+id/viewMutedLight"
                android:layout_width="match_parent"
                android:layout_height="25dp"/>

        </LinearLayout>
    </ScrollView>
</LinearLayout>



download filesDownload the APK to try .

No comments: