我正在使用 Ar core 并在会话期间拍摄图像。 直到现在我都使用最低分辨率选项并将其转换为 NV21 格式。一切都很好。现在我想采取更好的解决方案。它适用于大多数设备。但是对于一个(Galaxy S10e)由于某种原因我得到了一个损坏的图像。
这就是我选择相机配置的方式:
List<CameraConfig> cameraConfigList = session.getSupportedCameraConfigs(filter);
int maxSizeConfig = 0;
int maxHeight = 0;
for (int i = 0; i < cameraConfigList.size(); i++) {
if (cameraConfigList.get(i).getImageSize().getHeight()> maxHeight){
maxHeight = cameraConfigList.get(i).getImageSize().getHeight();
maxSizeConfig = i;
}
}
session.setCameraConfig(cameraConfigList.get(maxSizeConfig));
我得到的图像是:
image = frame.acquireCameraImage();
这就是我将其转换为 NV21 的方式
public static byte[] YUV_420_888toNV21(Image image) {
byte[] nv21;
ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
ByteBuffer uBuffer = image.getPlanes()[1].getBuffer();
ByteBuffer vBuffer = image.getPlanes()[2].getBuffer();
int ySize = yBuffer.remaining();
int uSize = uBuffer.remaining();
int vSize = vBuffer.remaining();
nv21 = new byte[ySize + uSize + vSize];
//U and V are swapped
yBuffer.get(nv21, 0, ySize);
vBuffer.get(nv21, ySize, vSize);
uBuffer.get(nv21, ySize + vSize, uSize);
return nv21;
}
稍后我用它把它转换成 JPEG
public static byte[] NV21toJPEG(byte[] nv21, int width, int height) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuv = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
yuv.compressToJpeg(new Rect(0, 0, width, height), 100, out);
return out.toByteArray();
}