Java 如何有效地遍历位图?

Java 如何有效地遍历位图?,java,android,image-processing,nested-loops,divide-and-conquer,Java,Android,Image Processing,Nested Loops,Divide And Conquer,我需要遍历位图图像的每个像素,这是我的代码,但是速度太慢了 Bitmap img=......; int imgWidth = img.getWidth(); int imgHeight = img.getHeight(); for (int i = 0; i < imgWidth; i++) { for (int j = 0; j < imgHeight; j++) { int color = img.getP

我需要遍历位图图像的每个像素,这是我的代码,但是速度太慢了

    Bitmap img=......;
    int imgWidth = img.getWidth();
    int imgHeight = img.getHeight();
    for (int i = 0; i < imgWidth; i++) {
        for (int j = 0; j < imgHeight; j++) {
            int color = img.getPixel(i, j);
            int R = android.graphics.Color.red(color);
            int G = android.graphics.Color.green(color);
            int B = android.graphics.Color.blue(color);
            // do something
        }
    }
位图img=。。。。。。;
int imgWidth=img.getWidth();
int imgHeight=img.getHeight();
for(int i=0;i
我已经解决了这个问题。这是我的新代码

    int[] colors = new int[img.getWidth() * img.getHeight()];
    img.getPixels(colors, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());
    for (int i = 0; i < colors.length; i++) {
            int y = (int) (i / img.getWidth());
            int x = i % img.getWidth();
            int R = android.graphics.Color.red(colors[i]);
            int G = android.graphics.Color.green(colors[i]);
            int B = android.graphics.Color.blue(colors[i]);
        }
    }
int[]colors=newint[img.getWidth()*img.getHeight()];
getPixels(颜色,0,img.getWidth(),0,0,img.getWidth(),img.getHeight());
for(int i=0;i

新的解决方案大约需要8秒,旧的解决方案需要27秒。

8秒仍然是遍历图像的很长时间。遗憾的是,我对Java一无所知,无法帮助您解决这个问题。