我将图像从YUV(NV21 Android)转换为RGB
然后我尝试通过x,y坐标获取像素的颜色。 例如,我尝试从中心获取像素。但是我从错误的地方弄到了颜色。
收集图像YUV
ByteBuffer yBuff = image.getPlanes()[0].getBuffer();
ByteBuffer uBuff = image.getPlanes()[1].getBuffer();
ByteBuffer vBuff = image.getPlanes()[2].getBuffer();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] yByte = new byte[yBuff.remaining()];
byte[] uByte = new byte[uBuff.remaining()];
byte[] vByte = new byte[vBuff.remaining()];
yBuff.get(yByte);
uBuff.get(uByte);
vBuff.get(vByte);
// Create converting byte[] NV21
try {
outputStream.write(yByte);
for (int i=0; i < uByte.length; i++) {
outputStream.write(vByte[i]);
outputStream.write(uByte[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
byte[] imageBytes = outputStream.toByteArray();
我目前有什么。
// Coordinates
x = width / 2;
y = height / 2;
// Get YUV coordinates for x y position
int YCord = y * width + x;
int UCord = (y >> 1) * (width) + x + total + 1;
int VCord = (y >> 1) * (width) + x + total;
// Get YUV colors
int Y = (0xFF & imageBytes[YCord]) - 16;
int U = (0xFF & imageBytes[UCord]) - 128;
int V = (0xFF & imageBytes[VCord]) - 128;
我希望图像中心的颜色。但是我有来自不同地方的颜色
答案 0 :(得分:0)
https://github.com/lirugo/screenDetector
我找到了解决方案,如果有人想要更多,请参阅下面关于通过x,y坐标获取像素的链接的链接查看我的代码
x = 210; // might be 250
y = height - 215; // might be 150
// Get YUV coordinates for x y position
int YCord = y*width+x;
int UCord = ((y/2)*width+(x&~1)+total+1);
int VCord = ((y/2)*width+(x&~1)+total);