【问题标题】:How do you speed up Image Processing with Android/Java?如何使用 Android/Java 加速图像处理?
【发布时间】:2012-06-18 01:38:52
【问题描述】:

我正在关注一些 Java 代码来对位图进行图像处理 发现对用户来说真的很耗时。

如何在代码中更快地做到这一点?

现在逐个像素花费的时间太长了

我在某处读到过关于先将其存储到二维数组中的信息? 矩阵

public static Bitmap createContrast(Bitmap src, double value) {
    // image size
    int width = src.getWidth();
    int height = src.getHeight();
    // create output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    // color information
    int A, R, G, B;
    int pixel;
    // get contrast value
    double contrast = Math.pow((100 + value) / 100, 2);

    // scan through all pixels
    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            // get pixel color
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            // apply filter contrast for every channel R, G, B
            R = Color.red(pixel);
            R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
            if(R < 0) { R = 0; }
            else if(R > 255) { R = 255; }

            G = Color.red(pixel);
            G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
            if(G < 0) { G = 0; }
            else if(G > 255) { G = 255; }

            B = Color.red(pixel);
            B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
            if(B < 0) { B = 0; }
            else if(B > 255) { B = 255; }

            // set new pixel color to output bitmap
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }

    // return final image
    return bmOut;
}

【问题讨论】:

  • 如果面向 android 3.0+,请使用 RenderScript。
  • 我看到一个 O(n^2) 循环;对于大图片,这总是很慢。您的优化可能来自重新审视您的算法。
  • 如果您愿意学习 OpenGL/GLSL,您可以将算法的速度提高几个数量级。

标签: java android arrays


【解决方案1】:

我建议使用 getPixels 和 setPixels 以便您可以直接操作数组。我没有运行它,但它应该给你的想法:

int[] pixels = new int[width * height];
bmOut.getPixels(pixels, 0, width, 0, 0, width, height);
// scan through all pixels
for(int i = 0; i < width * height; i++) {
    // get pixel color
    // pixel = src.getPixel(x, y);
    pixel = pixels[i];
    A = Color.alpha(pixel);
    // apply filter contrast for every channel R, G, B
    R = Color.red(pixel);
    R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
    if(R < 0) { R = 0; }
    else if(R > 255) { R = 255; }

    G = Color.green(pixel);
    G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
    if(G < 0) { G = 0; }
    else if(G > 255) { G = 255; }

    B = Color.blue(pixel);
    B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
    if(B < 0) { B = 0; }
    else if(B > 255) { B = 255; }

    // set new pixel color to output bitmap
    pixels[i] = Color.argb(A, R, G, B);
}
bmOut.setPixels(pixels, 0, width, 0, 0, width, height);

【讨论】:

  • 我也会用预先计算的数组查找替换(int)(((((RGB / 255.0) - 0.5) * contrast) + 0.5) * 255.0)
猜你喜欢
  • 2015-01-12
  • 2016-11-26
  • 2016-12-18
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 2010-11-12
  • 2023-03-25
相关资源
最近更新 更多