如何使用Android/Java加速图像处理?
我遵循一些Java代码对位图进行图像处理 并发现这对用户来说确实很耗时 如何在代码中更快地实现这一点 现在一个像素接一个像素的时间太长了 我在哪里读到过关于先将其存储到2d数组中的内容矩阵如何使用Android/Java加速图像处理?,java,android,arrays,Java,Android,Arrays,我遵循一些Java代码对位图进行图像处理 并发现这对用户来说确实很耗时 如何在代码中更快地实现这一点 现在一个像素接一个像素的时间太长了 我在哪里读到过关于先将其存储到2d数组中的内容矩阵 public static Bitmap createContrast(Bitmap src, double value) { // image size int width = src.getWidth(); int height = src.getHeight(); //
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;
}
公共静态位图创建对比度(位图src,双值){
//图像大小
int width=src.getWidth();
int height=src.getHeight();
//创建输出位图
Bitmap bmOut=Bitmap.createBitmap(宽度、高度,src.getConfig());
//颜色信息
int A,R,G,B;
整数像素;
//获取对比度值
双对比度=数学功率((100+值)/100,2);
//扫描所有像素
对于(int x=0;x255){R=255;}
G=颜色。红色(像素);
G=(int)((G/255.0)-0.5)*对比度+0.5)*255.0);
如果(G<0){G=0;}
如果(G>255){G=255;}
B=颜色。红色(像素);
B=(int)((B/255.0)-0.5)*对比度+0.5)*255.0);
如果(B<0){B=0;}
如果(B>255){B=255;}
//设置新像素颜色以输出位图
设置像素(x,y,Color.argb(A,R,G,B));
}
}
//返回最终图像
返回bmOut;
}
我建议使用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[]像素=新的int[宽度*高度];
getPixels(像素,0,宽度,0,0,宽度,高度);
//扫描所有像素
对于(int i=0;i255){R=255;}
G=颜色。绿色(像素);
G=(int)((G/255.0)-0.5)*对比度+0.5)*255.0);
如果(G<0){G=0;}
如果(G>255){G=255;}
B=颜色。蓝色(像素);
B=(int)((B/255.0)-0.5)*对比度+0.5)*255.0);
如果(B<0){B=0;}
如果(B>255){B=255;}
//设置新像素颜色以输出位图
像素[i]=Color.argb(A,R,G,B);
}
设置像素(像素,0,宽度,0,0,宽度,高度);
如果目标是android 3.0+,请使用RenderScript。我看到一个O(n^2)循环;对于大图片来说,这总是很慢。您的优化可能是通过重新访问您的算法来实现的。如果您愿意学习OpenGL/GLSL,您可能会将算法的速度提高几个数量级。我还将使用预先计算的数组查找替换(int)(((((((RGB/255.0)-0.5)*对比度)+0.5)*255.0)
。