0

我想在将图片拍摄为 potrait 时旋转位图。我想了解图像是肖像还是风景?我使用这段代码:

   Bitmap  photo   = BitmapFactory.decodeFile(path_img,options); 
   int imageHeight = photo.getHeight();
   int imageWidth  = photo.getWidth();

当我将图像作为 portroit 和 lanscape 时,它​​总是这样并不重要: imageHeight =390; 图像宽度=520;

我如何理解一张照片是横向或纵向拍摄的。

谢谢

4

2 回答 2

1

取自 nigels 链接的代码并针对提供的代码片段对其进行了更改

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;    
}

据我了解,这段代码片段和 2 种方法应该自己完成所有工作。

于 2014-01-14T14:58:01.347 回答
0

我如何理解一张照片是横向拍摄的还是纵向拍摄的?

假设图像当前存储在文件中,您可以从 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_180ExifInterface.ORIENTATION_ROTATE_270

于 2014-01-14T14:57:55.403 回答