ExifInterface 不显示凸轮捕获图像的旋转
ExifInterface doesn't show rotation of cam captured image
我从我的 cam 捕获图像并创建位图并将路径传递给 ExifInterface
以确定旋转,因为 cam 捕获的图像一直旋转 90 度。但是下面的代码总是显示对应于 ORIENTATION_UNDEFINED
旋转类型的值 0
。
确定旋转的其他方法还是我这里做错了什么?
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = Utils.createImageFile(getContext());
String authorities = context.getPackageName() + ".fileprovider";
uriForFile = FileProvider.getUriForFile(context, authorities, photoFile);
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "handleCaptureImage: " + uriForFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
startActivityForResult(intent, CAPTURE_IMAGE);
if (requestCode == CAPTURE_IMAGE) {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uriForFile);
saveBitmap(bitmap);
}
public static File saveBitmap(Bitmap bitmap) {
File file = null;
String imageFileName = "JPEG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";
String path = App.getAppContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + imageFileName;
if (bitmap != null) {
file = new File(path);
try {
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(path);
//this bitmap is rotated when I observe in debug mode.
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
ExifInterface exifInterface = new ExifInterface(path);
//still always returns 0 I expect 6 which is for 90 degrees
Log.d(TAG, "saveBitmap: "+exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
}
I capture image from my cam
鉴于您的代码引用了 requestCode
.
,我猜这是来自 ACTION_IMAGE_CAPTURE
在那种情况下,你拥有:
- 从文件中读入图像
- 再次将图像保存到另一个文件,同时擦除 EXIF headers
你不需要这些。
您知道图像的位置,因为您(大概)在 ACTION_IMAGE_CAPTURE
请求中通过 EXTRA_OUTPUT
提供了一个位置。因此,使用 ExifInterface
从该位置读取。摆脱位图的东西,特别是写回位图。 JPEG 文件可能有 EXIF headers。 Bitmap
不会,从 Bitmap
创建的 JPEG 文件不会(除非您自己添加)。
我从我的 cam 捕获图像并创建位图并将路径传递给 ExifInterface
以确定旋转,因为 cam 捕获的图像一直旋转 90 度。但是下面的代码总是显示对应于 ORIENTATION_UNDEFINED
旋转类型的值 0
。
确定旋转的其他方法还是我这里做错了什么?
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = Utils.createImageFile(getContext());
String authorities = context.getPackageName() + ".fileprovider";
uriForFile = FileProvider.getUriForFile(context, authorities, photoFile);
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "handleCaptureImage: " + uriForFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
startActivityForResult(intent, CAPTURE_IMAGE);
if (requestCode == CAPTURE_IMAGE) {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uriForFile);
saveBitmap(bitmap);
}
public static File saveBitmap(Bitmap bitmap) {
File file = null;
String imageFileName = "JPEG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";
String path = App.getAppContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + imageFileName;
if (bitmap != null) {
file = new File(path);
try {
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(path);
//this bitmap is rotated when I observe in debug mode.
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
ExifInterface exifInterface = new ExifInterface(path);
//still always returns 0 I expect 6 which is for 90 degrees
Log.d(TAG, "saveBitmap: "+exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
}
I capture image from my cam
鉴于您的代码引用了 requestCode
.
ACTION_IMAGE_CAPTURE
在那种情况下,你拥有:
- 从文件中读入图像
- 再次将图像保存到另一个文件,同时擦除 EXIF headers
你不需要这些。
您知道图像的位置,因为您(大概)在 ACTION_IMAGE_CAPTURE
请求中通过 EXTRA_OUTPUT
提供了一个位置。因此,使用 ExifInterface
从该位置读取。摆脱位图的东西,特别是写回位图。 JPEG 文件可能有 EXIF headers。 Bitmap
不会,从 Bitmap
创建的 JPEG 文件不会(除非您自己添加)。