【问题标题】:Bitmap getWith() and getHeighth() always return same size?位图 getWith() 和 getHeighth() 总是返回相同的大小?
【发布时间】:2014-02-02 16:12:38
【问题描述】:
当图片被拍成 potrait 时,我想旋转位图。
我想了解图像是肖像还是风景?
我使用此代码:
Bitmap photo = BitmapFactory.decodeFile(path_img,options);
int imageHeight = photo.getHeight();
int imageWidth = photo.getWidth();
当我将图像作为 portroit 和 lanscape 时,这并不重要,它总是这样:
图像高度 = 390;图像宽度 =520;
我如何理解一张照片是横向还是纵向拍摄的。
谢谢
【问题讨论】:
标签:
android
android-camera
bitmapfactory
android-bitmap
【解决方案1】:
取自 nigels 链接的代码并将其更改为提供的代码 sn-p
Bitmap photo = BitmapFactory.decodeFile(path_img,options);
ExifInterface exif = new ExifInterface(path_img);
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
Matrix matrix = new Matrix();
if (rotation != 0f) {
matrix.preRotate(rotationInDegrees);
}
Bitmap adjustedBitmap = Bitmap.createBitmap(photo, 0, 0, width, height, matrix, true);
private static int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }
return 0;
}
据我了解,这段代码 sn-p 和 2 个方法应该自己完成所有工作。
【解决方案2】:
我如何理解一张照片是横向还是纵向?
假设图像当前存储在文件中,您可以从 Exif 元数据对象中获取它的方向:
File f = new File(capturedImageFilePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
请注意,此信息不一定可用 - 由捕获图像的应用程序将元数据存储在文件中。
如果此信息不存在,getAttributeInt 将返回 ExifInterface.ORIENTATION_NORMAL
方向可以是以下之一:ExifInterface.ORIENTATION_ROTATE_90 / ExifInterface.ORIENTATION_ROTATE_180 / ExifInterface.ORIENTATION_ROTATE_270