Skip to content Skip to sidebar Skip to footer

What Is The Best Way To Implement Color Detection In Android?

I want to implement color detection in Android. What I exactly want to do is that after taking a picture with Android Camera, I want to detect the color of object in that picture.

Solution 1:

If I understand your question correctly, you want a method that will take an image and determine the most dominant color in that method and then return the result.

I have put together a class that should provide you with the result, don't know if it's the most efficient algorithm but give it a try. To implement the solution you can do the following

newColorFinder(newCallbackInterface() {
    @OverridepublicvoidonCompleted(String color) {
        Toast.makeText(context, "Your Color : " + color, Toast.LENGTH_SHORT).show();
    }
}).findDominantColor(yourBitmap);

ColorFinder.java

import android.graphics.Bitmap;
import android.os.AsyncTask;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Neil on 15/02/23.
 */publicclassColorFinder {
    privatestaticfinalStringTAG= ColorFinder.class.getSimpleName();

    private CallbackInterface callback;

    publicColorFinder(CallbackInterface callback) {
        this.callback = callback;
    }

    publicvoidfindDominantColor(Bitmap bitmap) {
        newGetDominantColor().execute(bitmap);
    }

    privateintgetDominantColorFromBitmap(Bitmap bitmap) {
        int [] pixels = newint[bitmap.getWidth()*bitmap.getHeight()];
        bitmap.getPixels(pixels,0,bitmap.getWidth(),0,0, bitmap.getWidth(), bitmap.getHeight());

        Map<Integer, PixelObject> pixelList = getMostDominantPixelList(pixels);
        return getDominantPixel(pixelList);
    }

    private Map<Integer, PixelObject> getMostDominantPixelList(int [] pixels) {
        Map<Integer, PixelObject> pixelList = newHashMap<>();

        for (int pixel : pixels) {
            if (pixelList.containsKey(pixel)) {
                pixelList.get(pixel).pixelCount++;
            } else {
                pixelList.put(pixel, newPixelObject(pixel, 1));
            }
        }

        return pixelList;
    }

    privateintgetDominantPixel(Map<Integer, PixelObject> pixelList) {
        intdominantColor=0;
        intlargestCount=0;
        for (Map.Entry<Integer, PixelObject> entry : pixelList.entrySet()) {
            PixelObjectpixelObject= entry.getValue();

            if (pixelObject.pixelCount > largestCount) {
                largestCount = pixelObject.pixelCount;
                dominantColor = pixelObject.pixel;
            }
        }

        return dominantColor;
    }

    privateclassGetDominantColorextendsAsyncTask<Bitmap, Integer, Integer> {

        @Overrideprotected Integer doInBackground(Bitmap... params) {
            intdominantColor= getDominantColorFromBitmap(params[0]);
            return dominantColor;
        }

        @OverrideprotectedvoidonPostExecute(Integer dominantColor) {
            StringhexColor= colorToHex(dominantColor);
            if (callback != null)
                callback.onCompleted(hexColor);
        }

        private String colorToHex(int color) {
            return String.format("#%06X", (0xFFFFFF & color));
        }
    }

    publicinterfaceCallbackInterface {
        publicvoidonCompleted(String dominantColor);
    }
}

PixelObject.java

publicclassPixelObject {
    publicint pixel;
    publicint pixelCount;

    publicPixelObject(int pixel, int pixelCount){
        this.pixel = pixel;
        this.pixelCount = pixelCount;
    }
}

Post a Comment for "What Is The Best Way To Implement Color Detection In Android?"