我将图片加载到bitmap
并需要知道拍摄照片的方向(来自相机)才能正确显示。使用以下代码的方法很有效(从API级别5开始),但如果android:minSdkVersion="4"
怎么办?还有另一种方式吗?
ExifInterface exif = new ExifInterface(SourceFileName); //Since API Level 5
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
答案 0 :(得分:7)
Matrix matrix = new Matrix();
ExifInterface exifReader = new ExifInterface(filePath);
int orientation = exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
if (orientation ==ExifInterface.ORIENTATION_NORMAL) {
// Do nothing. The original image is fine.
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
matrix.postRotate(90);
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
matrix.postRotate(180);
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
matrix.postRotate(270);
}
答案 1 :(得分:2)
轻松实现自己的exif reader
然后
Metadata metadata = JpegMetadataReader.readMetadata(new File(imagePath));
Directory jpegDirectory = metadata.getDirectory(JpegDirectory.class);
int height = jpg.GetImageHeight();
int width = jpg.GetImageWidth();