简体   繁体   English

如何使用Android / Java加快图像处理速度?

[英]How do you speed up Image Processing with Android/Java?

I was following some Java code to do image processing on bitmaps and found that it was really time consuming for the user. 我遵循一些Java代码在位图上进行图像处理,发现这对于用户而言确实很耗时。

How could this be done faster in code? 如何在代码中更快地做到这一点?

Right now it's taking too long pixel by pixel 现在需要一个像素一个像素

I read somewhere about storing it into an 2d array first? 我读过有关先将其存储到2d数组中的内容? Matrix ? 矩阵

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

I propose using getPixels and setPixels so that you can manipulate the array directly. 我建议使用getPixels和setPixels,以便您可以直接操作数组。 I haven't run this, but it should give you the idea: 我还没有运行它,但是它应该给你一个主意:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM