【发布时间】:2014-07-21 17:38:00
【问题描述】:
我将图像加载到Bitmap 对象中。然后我想做的是对存储在Bitmap 对象中的图像进行灰度化处理。
我使用以下函数来做到这一点:
public static Bitmap grayscale(Bitmap src)
{
// constant factors
final double GS_RED = 0.299;
final double GS_GREEN = 0.587;
final double GS_BLUE = 0.114;
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
// pixel information
int A, R, G, B;
int pixel;
// get image size
int width = src.getWidth();
int height = src.getHeight();
// scan through every single pixel
for(int x = 0; x < width; ++x)
{
for(int y = 0; y < height; ++y)
{
// get one pixel color
pixel = src.getPixel(x, y);
// retrieve color of all channels
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// take conversion up to one single value
R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
// set new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
// return final image
return bmOut;
}
它运行良好,但速度非常慢。不适用于像640x480 这样的“小”图像。但在我的情况下,图像是3264x2448。这实际上需要几秒钟才能完成操作...
所以我想知道像现在这样扫描每个像素是否真的是最好的方法?有没有更好更快的方法来转换图片的颜色?
【问题讨论】:
标签: android performance image-processing bitmap