使用相机拍摄时将图像旋转到移动设备的方向:Android

时间:2014-12-04 05:29:49

标签: java android android-camera image-rotation

我正在构建一个Android应用程序,我从手机的相机中点击一张照片并将其显示在ImageView中,它会自动逆时针旋转90度。我想以它被点击的方向显示它移动而不是让它旋转。

是否有人可以提供相关的代码段或指向某些相关文档的指针?

2 个答案:

答案 0 :(得分:0)

检查ExifInterface

ExifInterface exif = new ExifInterface(imageFilePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
    // rotate the image
    break;

    //....
}

答案 1 :(得分:0)

使用这个方法它会帮助你。

/*   
 * 1 = Horizontal (normal) 
 * 2 = Mirror horizontal 
 * 3 = Rotate 180 
 * 4 = Mirror vertical 
 * 5 = Mirror horizontal and rotate 270 CW 
 * 6 = Rotate 90 CW 
 * 7 = Mirror horizontal and rotate 90 CW 
 * 8 = Rotate 270 CW
 */
public static Bitmap getRotateBitmapImage(Bitmap bm, int newHeight,int newWidth, int exifOrientation) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    switch (exifOrientation) {
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotate = 270;
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotate = 180;
        break;
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotate = 90;
        break;
    case ExifInterface.ORIENTATION_TRANSPOSE:
        rotate = 45;
        break;
    case 0:
        rotate = 360;
        break;
    default:
        break;
    }
    matrix.postRotate(rotate);
    // resizedBitmap = Bitmap.createScaledBitmap(bm, 65, 65, true);
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}