以下是我獲得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