以下是我获得eh RGB颜色并在位图(另一个位图)中的颜色为绿色时在temp(位图)中更改颜色的方法。但表现相当糟糕,时间大约在20秒或更长。有什么方法可以改善它吗?非常感谢你。在android中获取RGB更快
//get the green color of each pixel
color = new int[bitmap.getWidth()][bitmap.getHeight()];
for (int x = 0; x < bitmap.getWidth(); x++) {
for (int y = 0; y < bitmap.getHeight(); y++) {
color[x][y] = temp.getPixel(x, y);
if (bitmap.getPixel(x, y) == Color.GREEN) {
color[x][y] = temp.getPixel(x, y);
float[] hsv = new float[3];
Color.RGBToHSV(Color.red(color[x][y]), Color.green(color[x][y]), Color.blue(color[x][y]), hsv);
hsv[0] = 360;
color[x][y] = Color.HSVToColor(hsv);
}
temp.setPixel(x, y, color[x][y]);
}
}
imageView.setImageBitmap(temp);
更新:
intArray = new int[bitmap.getWidth()*bitmap.getHeight()];
bitmap.getPixels(intArray,0,bitmap.getWidth(),0,0,bitmap.getWidth(),bitmap.getHeight());
int[] tempArray = new int[bitmap.getWidth()*bitmap.getHeight()];
temp.getPixels(tempArray,0,temp.getWidth(),0,0,bitmap.getWidth(),bitmap.getHeight());
//get the color of each pixel
color = new int[bitmap.getWidth()][bitmap.getHeight()];
for (int x = 0; x < bitmap.getWidth(); x++) {
for (int y = 0; y < bitmap.getHeight(); y++) {
if (intArray[x + y * bitmap.getWidth()] == Color.BLUE) {
float[] hsv = new float[3];
Color.RGBToHSV(Color.red(tempArray[x + y * bitmap.getWidth()]), Color.green(tempArray[x + y * bitmap.getWidth()]), Color.blue(tempArray[x + y * bitmap.getWidth()]), hsv);
hsv[0] = 360;
tempArray[x + y * bitmap.getWidth()] = Color.HSVToColor(hsv);
}
}
}
temp.setPixels(tempArray,0,temp.getWidth(),0,0,temp.getWidth(),temp.getHeight());
imageView.setImageBitmap(temp);
谢谢你的回复。 像在二维数组上一样,直接处理像素数据会更好。 < - 我对这个很感兴趣,请介绍一下我的进一步解释吗? – ng2b30
@ ng2b30而不是使用'getPixel()'和'setPixel()',你可以直接访问像素数组,这要快得多:byte [] pixels =((DataBufferByte)bufferedImage.getRaster()。getDataBuffer()) .getData();' - 参见http://stackoverflow.com/questions/6524196/java-get-pixel-array-from-image –
我有一个问题。我怎样才能从画廊加载一个PNG图像,以改变它是一个get bufferedImage?谢谢。 – ng2b30