什么是实现颜色检测的Android的最佳途径? [英] What is the best way to implement Color Detection in Android?
问题描述
我要实现在Android的颜色检测。我究竟想要做的是,拍摄图片Android相机后,我要检测该图片对象的颜色。我的目标是根据颜色强度检测颜色。在这一点上,我搜索,看到有关这一目标的不同方法。有一些算法该图像转换成位图,然后检测的颜色,其中一些使用RGB。此外,我看到了OpenCV的也是这个问题的一个已知的解决方案。
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. My goal is detecting colors according to color intensity. At this point, I searched and saw different approaches related to this goal. There are some algorithms which converts the images into Bitmap and then detects the colors, some of them use RGB. Additionally, I saw that OpenCV is also a known solution for this problem.
现在,我不知道我应该遵循的道路。哪种方式对我的情况好。是否有任何人谁可以帮助和指导我通过一种方法?
Now, I wonder which way I should follow. Which way is better for my case. Are there anyone who can help and direct me through a method?
任何帮助将AP preciated。谢谢大家提前。
Any help will be appreciated. Thank you all in advance.
推荐答案
如果我正确地理解你的问题,你希望那将拍摄图像,并确定该方法中最具统治力的颜色,然后返回结果的方法。
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
new ColorFinder(new CallbackInterface() {
@Override
public void onCompleted(String color) {
Toast.makeText(context, "Your Color : " + color, Toast.LENGTH_SHORT).show();
}
}).findDominantColor(yourBitmap);
ColorFinder.java 的
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.
*/
public class ColorFinder {
private static final String TAG = ColorFinder.class.getSimpleName();
private CallbackInterface callback;
public ColorFinder(CallbackInterface callback) {
this.callback = callback;
}
public void findDominantColor(Bitmap bitmap) {
new GetDominantColor().execute(bitmap);
}
private int getDominantColorFromBitmap(Bitmap bitmap) {
int [] pixels = new int[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 = new HashMap<>();
for (int pixel : pixels) {
if (pixelList.containsKey(pixel)) {
pixelList.get(pixel).pixelCount++;
} else {
pixelList.put(pixel, new PixelObject(pixel, 1));
}
}
return pixelList;
}
private int getDominantPixel(Map<Integer, PixelObject> pixelList) {
int dominantColor = 0;
int largestCount = 0;
for (Map.Entry<Integer, PixelObject> entry : pixelList.entrySet()) {
PixelObject pixelObject = entry.getValue();
if (pixelObject.pixelCount > largestCount) {
largestCount = pixelObject.pixelCount;
dominantColor = pixelObject.pixel;
}
}
return dominantColor;
}
private class GetDominantColor extends AsyncTask<Bitmap, Integer, Integer> {
@Override
protected Integer doInBackground(Bitmap... params) {
int dominantColor = getDominantColorFromBitmap(params[0]);
return dominantColor;
}
@Override
protected void onPostExecute(Integer dominantColor) {
String hexColor = colorToHex(dominantColor);
if (callback != null)
callback.onCompleted(hexColor);
}
private String colorToHex(int color) {
return String.format("#%06X", (0xFFFFFF & color));
}
}
public interface CallbackInterface {
public void onCompleted(String dominantColor);
}
}
PixelObject.java 的
PixelObject.java
public class PixelObject {
public int pixel;
public int pixelCount;
public PixelObject(int pixel, int pixelCount) {
this.pixel = pixel;
this.pixelCount = pixelCount;
}
}
这篇关于什么是实现颜色检测的Android的最佳途径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!