我想将相机拍摄的照片中除黑色以外的所有颜色更改为白色。但是,物体的黑色随着光线和白平衡的变化而变化,无法检测为黑色然后也变成白色
public void subColor(Mat src, String timeStamp, File mediaStorageDir) throws FileNotFoundException {
Bitmap output = Bitmap.createBitmap(src.width(), src.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(src, output);
for (int x = 0; x < output.getWidth(); x++)
for (int y = 0; y < output.getHeight(); y++) {
int pixel = output.getPixel(x, y);
if (pixel != Color.BLACK)
output.setPixel(x, y, Color.WHITE);
}
String mImageName= timeStamp + "_EDIT" + ".jpg";
File mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
FileOutputStream fos = new FileOutputStream(mediaFile);
output.compress(Bitmap.CompressFormat.JPEG, 100, fos);
}
我想知道像素的颜色是否可以与黑色而不是 Color.BLACK 的范围进行比较。
谢谢。
最佳答案
希望以下步骤对您有所帮助: 1. 从像这样的像素计算 RGB:
int r = (pixel >> 16) & 0xff;
int g = (pixel >> 8) & 0xff;
int b = (pixel >> 0) & 0xff;
2.通过以下公式将RGB颜色值转换为计算亮度
Y = 0.2126*r + 0.7152*g + 0.0722*b
检查它是否是黑色阴影:
如果( Y < 128){ //可以作为黑色阴影 }别的{ //可以作为白色阴影 }
关于Android - 改变照片像素的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36328803/