问题描述
我使用了
如果我已将 ImageReader
更改为使用 ImageFormat.JPEG
而是通过执行以下更改在 Camera2
:
mPreviewImageReader = ImageReader.newInstance(previewSize.getWidth( ),
previewSize.getHeight(),ImageFormat.JPEG,/ * maxImages * / 2);
mCamera.createCaptureSession(Arrays.asList(surface,mPreviewImageReader.getSurface()),
mSessionCallback,null);
图像正常运行而没有任何失真但帧速率显着下降且视图开始滞后。因此我认为 ImageUtil
类没有正确转换。
解决方案由@ volodymyr-kulyk提供的没有考虑图像中平面的行间距。下面的代码是诀窍(图像
是 android.media.Image
类型):
data = NV21toJPEG(YUV420toNV21(image),image.getWidth(),image.getHeight(),100);
实施:
private static byte [] NV21toJPEG(byte [] nv21,int width,int height,int quality){
ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuv = new YuvImage(nv21,ImageFormat.NV21,width,height,null);
yuv.compressToJpeg(new Rect(0,0,width,height),quality,out);
返回out.toByteArray();
}
private static byte [] YUV420toNV21(图片图片){
Rect crop = image.getCropRect();
int format = image.getFormat();
int width = crop.width();
int height = crop.height();
Image.Plane [] planes = image.getPlanes();
byte [] data = new byte [width * height * ImageFormat.getBitsPerPixel(format)/ 8];
byte [] rowData = new byte [planes [0] .getRowStride()];
int channelOffset = 0;
int outputStride = 1;
for(int i = 0; i< planes.length; i ++){
switch(i){
case 0:
channelOffset = 0;
outputStride = 1;
休息;
案例1:
channelOffset = width * height + 1;
outputStride = 2;
休息;
案例2:
channelOffset = width * height;
outputStride = 2;
休息;
}
ByteBuffer buffer = planes [i] .getBuffer();
int rowStride = planes [i] .getRowStride();
int pixelStride = planes [i] .getPixelStride();
int shift =(i == 0)? 0:1;
int w = width>>转移;
int h = height>>转移;
buffer.position(rowStride *(crop.top>> shift)+ pixelStride *(crop.left>> shift));
for(int row = 0; row< h; row ++){
int length;
if(pixelStride == 1&& outputStride == 1){
length = w;
buffer.get(data,channelOffset,length);
channelOffset + = length;
} else {
length =(w - 1)* pixelStride + 1;
buffer.get(rowData,0,length);
for(int col = 0; col< w; col ++){
data [channelOffset] = rowData [col * pixelStride];
channelOffset + = outputStride;
}
}
if(row< h - 1){
buffer.position(buffer.position()+ rowStride - length);
}
}
}
返回数据;
}
方法来自以下。
I've used the ImageUtil
class provided in https://stackoverflow.com/a/40152147/2949966 within my git repo: https://github.com/ahasbini/cameraview/tree/camera_preview_imp (note the implementation is in camera_preview_imp
branch) to implement a frame preview callback. An ImageReader
is set to preview frames in the ImageFormat.YUV_420_888
format which will be converted into ImageFormat.JPEG
using the ImageUtil
class and send it to the frame callback. The demo app saves a frame from the callback to a file every 50 frames. All of the saved frame images are coming out distorted similar to below:
If I've changed the ImageReader
to use ImageFormat.JPEG
instead by doing the following changes in Camera2
:
mPreviewImageReader = ImageReader.newInstance(previewSize.getWidth(),
previewSize.getHeight(), ImageFormat.JPEG, /* maxImages */ 2);
mCamera.createCaptureSession(Arrays.asList(surface, mPreviewImageReader.getSurface()),
mSessionCallback, null);
the image is coming properly without any distortions however the frame rate drops significantly and the view starts to lag. Hence I believe the ImageUtil
class is not converting properly.
Solution provided by @volodymyr-kulyk does not take into consideration the row stride of the planes within the image. Below code does the trick (image
is of android.media.Image
type):
data = NV21toJPEG(YUV420toNV21(image), image.getWidth(), image.getHeight(), 100);
And the implementations:
private static byte[] NV21toJPEG(byte[] nv21, int width, int height, int quality) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuv = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
yuv.compressToJpeg(new Rect(0, 0, width, height), quality, out);
return out.toByteArray();
}
private static byte[] YUV420toNV21(Image image) {
Rect crop = image.getCropRect();
int format = image.getFormat();
int width = crop.width();
int height = crop.height();
Image.Plane[] planes = image.getPlanes();
byte[] data = new byte[width * height * ImageFormat.getBitsPerPixel(format) / 8];
byte[] rowData = new byte[planes[0].getRowStride()];
int channelOffset = 0;
int outputStride = 1;
for (int i = 0; i < planes.length; i++) {
switch (i) {
case 0:
channelOffset = 0;
outputStride = 1;
break;
case 1:
channelOffset = width * height + 1;
outputStride = 2;
break;
case 2:
channelOffset = width * height;
outputStride = 2;
break;
}
ByteBuffer buffer = planes[i].getBuffer();
int rowStride = planes[i].getRowStride();
int pixelStride = planes[i].getPixelStride();
int shift = (i == 0) ? 0 : 1;
int w = width >> shift;
int h = height >> shift;
buffer.position(rowStride * (crop.top >> shift) + pixelStride * (crop.left >> shift));
for (int row = 0; row < h; row++) {
int length;
if (pixelStride == 1 && outputStride == 1) {
length = w;
buffer.get(data, channelOffset, length);
channelOffset += length;
} else {
length = (w - 1) * pixelStride + 1;
buffer.get(rowData, 0, length);
for (int col = 0; col < w; col++) {
data[channelOffset] = rowData[col * pixelStride];
channelOffset += outputStride;
}
}
if (row < h - 1) {
buffer.position(buffer.position() + rowStride - length);
}
}
}
return data;
}
Method was gotten from the following link.
这篇关于将YUV_420_888转换为JPEG并保存文件会导致图像失真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!