[英]Convert image from byte array to bitmap with planar 4:2:0 YUV full scale pixel format
[英]Get center pixel from image with format YUV
我将图像从 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;
我期待图像中心的颜色。 但我有不同地方的颜色
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.