我正在尝试使用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/