我正在尝试使用红色、绿色、蓝色的平均值将彩色图像转换为灰度图像。但它出现错误。
这是我的代码
imgWidth = myBitmap.getWidth();
imgHeight = myBitmap.getHeight();
for(int i =0;i<imgWidth;i++) {
for(int j=0;j<imgHeight;j++) {
int s = myBitmap.getPixel(i, j)/3;
myBitmap.setPixel(i, j, s);
}
}
ImageView img = (ImageView)findViewById(R.id.image1);
img.setImageBitmap(myBitmap);
但是当我在模拟器上运行我的应用程序时,它会强制关闭。任何想法?
我已经使用以下代码解决了我的问题:
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)(0.299 * R + 0.587 * G + 0.114 * B);
// set new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
你也可以这样做 :
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
imageview.setColorFilter(new ColorMatrixColorFilter(matrix));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)