我使用Camera 2 API,我需要获取图像元数据(完全是ISO)才能将图像保存在设备上。
此时我正在使用ExifInterface
,但此类的构造函数仅获取File
或从api24 InputStream
开始。
如何在保存之前从图像中获取ISO?
修改
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
= new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(final ImageReader reader) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
new ImageSaver(context, reader.acquireNextImage()).writeImageToStorage();
return null;
}
}
}
};
public final class ImageSaver {
private Context context;
private static File imageFile;
private final Image image;
public ImageSaver(Context context, Image image) {
this.context = context;
this.image = image;
imageFile = getImageFile(UtilClass.getAvatarPhotosDirFile(context));
}
private File getImageFile(File path) {
return new File(path.getPath() + File.separator + getStamp() + ".jpeg");
}
public void writeImageToStorage() {
final ByteBuffer buffer = getByteBuffer();
final byte[] bytes = getBytes(buffer);
writeFile(imageFile, bytes);
}
@Nullable
private ByteBuffer getByteBuffer() {
ByteBuffer buffer = null;
if (image != null) {
buffer = image.getPlanes()[0].getBuffer();
}
return buffer;
}
private byte[] getBytes(ByteBuffer buffer) {
byte[] bytes = new byte[0];
if (buffer != null) {
bytes = new byte[buffer.remaining()];
}
if (buffer != null) {
buffer.get(bytes);
}
return bytes;
}
private void writeFile(File file, byte[] bytes) {
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (image != null) {
image.close();
}
if (null != bos) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@NonNull
private String getStamp() {
final int pictureAmount = PreferencesHelper.getIntShared(context, States.PICTURE_AMOUNT) + 1;
String stamp;
switch (pictureAmount){
case 1:
stamp = "pose1";
break;
case 2:
stamp = "pose3";
break;
case 3:
stamp = "pose5";
break;
case 4:
stamp = "pose7";
break;
default:
stamp = "BG";
break;
}
return stamp;
}
public static String getImageFilePath() {
return imageFile.getPath();
}
}
答案 0 :(得分:0)
ByteArrayinputStream bais = new ByteArrayInputStream(bytes);
bytes
包含jpeg。
将bais
用作InputStream
用于Android 7.0的新ExifInterface。
否则使用不同的Exifinterface类。不止一个。