Performance 在android中更快地获得RGB

Performance 在android中更快地获得RGB,performance,android-studio,bitmap,android-canvas,rgb,Performance,Android Studio,Bitmap,Android Canvas,Rgb,下面是我获取eh RGB颜色并在位图(另一个位图)中的颜色为绿色时更改temp(位图)中的颜色的方法。但性能相当差,所用时间约为20秒或更长。我有什么办法可以改进它吗?多谢各位 //get the green color of each pixel color = new int[bitmap.getWidth()][bitmap.getHeight()]; for (int x = 0; x < bitmap.getWidth(); x++) { for

下面是我获取eh RGB颜色并在位图(另一个位图)中的颜色为绿色时更改temp(位图)中的颜色的方法。但性能相当差,所用时间约为20秒或更长。我有什么办法可以改进它吗?多谢各位

 //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);
//获取每个像素的绿色
color=newint[bitmap.getWidth()][bitmap.getHeight()];
对于(int x=0;x
更新:

 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);
intArray=newint[bitmap.getWidth()*bitmap.getHeight()];
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());
//获取每个像素的颜色
color=newint[bitmap.getWidth()][bitmap.getHeight()];
对于(int x=0;x
我想我要做的是确定每个像素的大小。最有可能是32位ARGB。所以你有一个WxH长数组。循环遍历该数组,若值是G=255的值,则替换它。我也会忽略ALPHA通道

有几种方法可以加快速度。让我们从最简单的开始:


float[]hsv=新浮点[3]

使用
new
在堆上分配内存,这很慢而且不利于缓存(并且需要在之后释放(取决于语言,这可能是自动的))。您可以(重新)使用本地浮点[3]


无需在此处执行第二次
temp.getPixel()
(可能会很慢)。数据已为
颜色[x][y]


如果像素不变,则无需设置像素。因此,您可以在
if
块内移动
temp.setPixel
。实际上
color[x][y]=temp.getPixel(x,y)
也可以进入
if
块内部(或者只移除外部的一个,因为
if
块内部已经有了外部的代码)


首先,您可能想在这里使用
unsigned int
,但实际上不需要第三个数组。您只需使用本地
无符号int-color
变量即可


for(int x=0;x
根据语言的不同,您可能希望颠倒循环的顺序,使其更便于缓存(请参阅)。确保这一点的最简单方法是对这两个顺序进行快速测试



getPixel()
setPixel()
可能会很慢(同样取决于语言)。最好像处理二维数组一样直接处理像素数据。大多数语言都提供获取指针或访问原始像素数据的函数。

谢谢您的回复。最好像处理二维数组一样直接处理像素数据。@ng2b30而不是使用
getPixel()
setPixel()
您可以直接访问de pixels数组,这要快得多:
byte[]pixels=((DataBufferByte)BuffereImage.getRaster().getDataBuffer()).getData()
-看,我有一个问题。如何从gallery加载png图像以将其更改为get BuffereImage?谢谢。此外,我需要为图像绘制一些部分,然后获取彩色区域的位置。检测后我是否会使用相同的方法检测图像颜色?哦,我使用getPixels()找到另一种方法和setPixels()。我可以在1-2秒内完成任务。顺便说一句,谢谢你的回复
    color[x][y] = temp.getPixel(x, y);
    if (bitmap.getPixel(x, y) == Color.GREEN) {
        color[x][y] = temp.getPixel(x, y);
    color[x][y] = temp.getPixel(x, y);
    if (bitmap.getPixel(x, y) == Color.GREEN) {
        // ...
    }
    temp.setPixel(x, y, color[x][y]);
color = new int[bitmap.getWidth()][bitmap.getHeight()];
for (int x = 0; x < bitmap.getWidth(); x++) {
    for (int y = 0; y < bitmap.getHeight(); y++) {