【问题标题】:How to efficiently traverse Bitmap?如何高效遍历Bitmap?
【发布时间】:2019-11-05 00:52:46
【问题描述】:

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

    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
        }
    }

【问题讨论】:

    标签: java android image-processing nested-loops divide-and-conquer


    【解决方案1】:

    我已经解决了这个问题。这是我的新代码。

        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]);
            }
        }
    

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

    【讨论】:

    • 8 秒仍然是一个很长的时间来遍历图像。遗憾的是,我对 Java 一无所知,无法帮助您解决此问题。
    猜你喜欢
    • 2017-05-31
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多