camera2如何在图像阅读器侦听器中从YUV_420_888图像获取Exif数据

时间:2019-07-06 09:48:41

标签: android exif android-camera2 yuv

我正在尝试从YUV_420_888图像获取Exif数据,但是它不起作用。我尝试了几种解决方案,例如将图像以jpeg格式保存到磁盘,将其转换为输入流,但似乎无济于事。

我使用android camera2 API捕获YUV_420_888图像。然后在OnImageAvailableListener中获取图像,并尝试使用ExifInterface API读取其EXIF数据。但是它总是空的。我尝试了使用此link的所有方法来获取正确的字节数组。

这是我的代码:

@Override
public void onImageAvailable(ImageReader imageReader) {
    if (!isRecording) {
        return;
    }
    Image image = imageReader.acquireNextImage();
    File file = Util.getImagePath(context);

    OutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(file);
        outputStream.write(data); 
//// This byte array I am making using all the approaches given in this link 
https://stackoverflow.com/questions/44022062/converting-yuv-420-888-to-jpeg-and-saving-file-results-distorted-image

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    try {
        ExifInterface exifInterface = new ExifInterface(file.getAbsolutePath()); /// This is always empty
        int currentIso = (int)exifInterface.getAttributeDouble(ExifInterface.TAG_ISO_SPEED_RATINGS, 0); /// Always 0
    } catch (Exception e) {
        e.printStackTrace();
    }
    image.close();
}

编辑:用于捕获图像的代码:

captureRequest = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureRequest.addTarget(preview);
captureRequest.addTarget(imageReader.getSurface());
captureRequest.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF);
captureRequest.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_OFF);
captureRequest.set(CaptureRequest.SENSOR_SENSITIVITY, <MANUAL_ISO>);
captureRequest.set(CaptureRequest.SENSOR_EXPOSURE_TIME, <MANUAL_EXPOSURE>);
mSession.capture(captureRequest.build(), new CameraCaptureSession.CaptureCallback() {
        @Override
        public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
            int capturedISO = result.get(CaptureResult.SENSOR_SENSITIVITY);
            long timeStamp = result.get(CaptureResult.SENSOR_TIMESTAMP);
/// Save somewhere to be used later
            super.onCaptureCompleted(session, request, result);
        }
    }, backgroundHandler);

1 个答案:

答案 0 :(得分:0)

Exif信息放在CaptureResult中的CaptureCallback中,可用onCaptureCompleted方法放置-您应该“记住”一个类的结果。当图像在onImageAvailableListener中可用时,应保存图像,并“转换”结果以适合exif格式。类似地,您可以查看DngCreator类,该类使用来自CaptureResult的exif数据保存图像。如果您要突如其来,那就会更复杂。