栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Android Camera2 API YUV_420_888为JPEG

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android Camera2 API YUV_420_888为JPEG

我通过使用
YUV_420_888
图像格式并将其手动转换为
JPEG
图像格式解决了此问题。

imageReader = ImageReader.newInstance(MAX_PREVIEW_WIDTH, MAX_PREVIEW_HEIGHT,      ImageFormat.YUV_420_888, 5);imageReader.setonImageAvailableListener(this, null);

Surface imageSurface = imageReader.getSurface();List<Surface> surfaceList = new ArrayList<>();//...add other surfacespreviewRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);previewRequestBuilder.addTarget(imageSurface); surfaceList.add(imageSurface);cameraDevice.createCaptureSession(surfaceList,         new CameraCaptureSession.StateCallback() {//...implement onConfigured, onConfigureFailed for StateCallback}, null);

@Overridepublic void onImageAvailable(ImageReader reader) {    Image image = reader.acquireLatestImage();    if (image != null) {        //converting to JPEG        byte[] jpegData = ImageUtils.imageToByteArray(image);        //write to file (for example ..some_path/frame.jpg)        FileManager.writeframe(FILE_NAME, jpegData);        image.close();    }}

public final class ImageUtil {    public static byte[] imageToByteArray(Image image) {        byte[] data = null;        if (image.getFormat() == ImageFormat.JPEG) { Image.Plane[] planes = image.getPlanes(); ByteBuffer buffer = planes[0].getBuffer(); data = new byte[buffer.capacity()]; buffer.get(data); return data;        } else if (image.getFormat() == ImageFormat.YUV_420_888) { data = NV21toJPEG(         YUV_420_888toNV21(image),         image.getWidth(), image.getHeight());        }        return data;    }    private 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;    }    private 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();    }}

public final class FileManager {    public static void writeframe(String fileName, byte[] data) {        try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName)); bos.write(data); bos.flush(); bos.close();// Log.e(TAG, "" + data.length + " bytes have been written to " + filesDir + fileName + ".jpg");        } catch (IOException e) { e.printStackTrace();        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/452871.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号