我使用以下代码将普通图像转换为灰度和棕褐色彩色图像。
对于棕褐色颜色转换:
public static Bitmap createSepiaToningEffect(Bitmap src, int depth,
double red, double green, double blue) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// constant grayscale
final double GS_RED = 0.3;
final double GS_GREEN = 0.59;
final double GS_BLUE = 0.11;
// color information
int A, R, G, B;
int pixel;
// 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);
// get color on each channel
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// apply grayscale sample
B = G = R = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
// apply intensity level for sepid-toning on each channel
R += (depth * red);
if (R > 255) {
R = 255;
}
G += (depth * green);
if (G > 255) {
G = 255;
}
B += (depth * blue);
if (B > 255) {
B = 255;
}
// set new pixel color to output image
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
// return final image
return bmOut;
}
上面的代码工作正常,但问题是它需要更多时间(超过 60 秒)。我怎样才能减少时间消耗。当我将图像转换为灰度时,它需要不到 2 秒。谁能帮我解决这个问题。