Android Camera2 api 和 exif

标签 android android-camera

我正在尝试使用camera2 api,并且我制作了一个可以从相机拍摄照片的应用程序。现在我想将 exif 数据添加到捕获的图像中。我有一个关于在哪里或如何放置 exif 信息 的问题。

我应该在 onCaptureCompleted() 函数中创建 Exif 接口(interface)吗?最好的方法是什么?

final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {

    @Override
    public void onCaptureCompleted(CameraCaptureSession session,
                                   CaptureRequest request, TotalCaptureResult result) {

        super.onCaptureCompleted(session, request, result);
        Toast.makeText(MainActivity.this, "Saved:"+file, Toast.LENGTH_SHORT).show();


        ExifInterface exifTags = null;
        try {
            exifTags = new ExifInterface(file.getCanonicalPath());

            exifTags.setAttribute(ExifInterface.TAG_GPS_LATITUDE, Double.toString(cur_lat));
            exifTags.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, Double.toString(cur_long));

            exifTags.saveAttributes();

        } catch (IOException e) {
            e.printStackTrace();
        }
        //System.out.println(file.getCanonicalPath());
        System.out.println("Exif Test: " + Double.toString(cur_lat) + " " + Double.toString(cur_lat));

    }

};

当我这样做时,我收到一个错误:

"ImageReader_JNI﹕ Unable to acquire a lockedBuffer, very likely client tries to lock more than maxImages buffers"

执行此操作的最佳方法是什么?任何建议都会非常有帮助。

最佳答案

您尝试捕捉什么图像格式?如果是 JPEG,那么所有 Exif 标签都应该已经写入图像中。 结果图像在 OnImageAvailableListener.onImageAvailable() 中传递,而不是在 CameraCaptureSession 中传递。 onCaptureCompleted()。 尝试在 onImageAvailable 方法中添加自定义标签。

EDIT:

@Override
    public void onImageAvailable(ImageReader reader) {
        Log.e("TAG", System.currentTimeMillis() + "");
        Image mImage = reader.acquireNextImage();
        ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        FileOutputStream output = null;
        try {
            output = new FileOutputStream(mFile);
            output.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            mImage.close();
            if (null != output) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            ExifInterface exif = new ExifInterface(mFile.getAbsolutePath());
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, "10");
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, "10");
            exif.saveAttributes();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

关于Android Camera2 api 和 exif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28554546/

相关文章:

java - Android 图形 - 拖动位图时清除 Canvas

android - 如何在android中的自定义圆角 ImageView 中设置图像

android - 相机尺寸和4 :3 aspect ratio is not matching

Java 运行时异常 "Fail to connect to camera service"

android - 我无法使用 httpUrlConnection 将 json 从 Android 应用程序发布到 NodeJs 服务器

android - Android 中获取用户当前所在城市

android - 可以在连接到相机的 SurfaceTexture 上绘制吗?

android - 在 Debug模式 Android 下连接时找不到 SD 卡

android - Android Studio中gradle项目手动下载超时的依赖文件

android - 测试GPS数据是否被删除