Java 使用YUV格式从图像中获取中心像素
我把图像从YUV(NV21安卓)转换成RGB 我试着用x,y坐标测量像素的颜色。 例如,我尝试从中心获取像素。但我从错误的地方得到颜色 收集图像Java 使用YUV格式从图像中获取中心像素,java,image,yuv,Java,Image,Yuv,我把图像从YUV(NV21安卓)转换成RGB 我试着用x,y坐标测量像素的颜色。 例如,我尝试从中心获取像素。但我从错误的地方得到颜色 收集图像 ByteBuffer yBuff = image.getPlanes()[0].getBuffer(); ByteBuffer uBuff = image.getPlanes()[1].getBuffer(); ByteBuffer vBuff = image.getPlanes()[2].getBuffer(); ByteArrayOutputStr
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();
我希望图像的中心有颜色。但是我有不同地方的颜色
我找到了解决方案,如果有人想要更多,请参阅下面我的代码,链接关于获取像素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);
在这一行中,
intucord=(y>>1)*(宽度)+x+总计+1
您考虑到y分辨率通过移位减半,但对于x
您不这样做。我认为您需要使用x>>1
而不是x
。
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);