Android的图像旋转

问题描述:

我选择相机或画廊采取图像上传。 我使用下面的代码Android的图像旋转

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) { 

    path = new File(path).getAbsolutePath(); 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(path, options); 
} 

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) >= reqHeight 
       && (halfWidth/inSampleSize) >= reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

}

现在,如果这个形象把作为水平视图,然后我应该垂直更改解码图像。由于收据上传。

在我的图片库图像或拍摄图像的宽度>高度I要旋转的图像以垂直视图 此任何建议??

这是我用它为我的代码,

public Bitmap decodeFile(String filePath) { 
    int imageOrientation = 10; 
    ExifInterface ei; 
    try { 
     ei = new ExifInterface(filePath); 
     imageOrientation = ei.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    // Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, o); 

    // The new size we want to scale to 
    final int REQUIRED_SIZE = 1024; 

    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = 1; 
    while (true) { 
     if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
      break; 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale *= 2; 
    } 

    // Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    bitMap = BitmapFactory.decodeFile(filePath, o2); 

    switch (imageOrientation) { 
     case ExifInterface.ORIENTATION_ROTATE_90: 
      rotateImage(bitMap, 90); 
      break; 

     case ExifInterface.ORIENTATION_ROTATE_180: 
      rotateImage(bitMap, 180); 
      break; 

     case ExifInterface.ORIENTATION_ROTATE_270: 
      rotateImage(bitMap, 270); 
      break; 

     default: 
      break; 
    } 


    return bitMap; 

} 

这是rotate方法,

public void rotateImage(Bitmap img, int degree) { 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(degree); 
    bitMap = Bitmap.createBitmap(img, 0, 0, img.getWidth(), 
      img.getHeight(), matrix, true); 
} 
+0

耶是银河7边缘工作。但如果我把水平视图则能不能将它旋转到垂直... – CarinaMj

+1

你不需要到水平图像改变为垂直。在我的情况下,如果我垂直拍照并上传它已水平显示。这就是我使用此代码修复的问题。 – AngelJanniee

你可以设置它在ImageView和旋转角度90度,你希望它是任何方向。

您需要使用矩阵这个如下,它为我工作

Matrix matrix = getRotation(context, Uri.fromFile(f)); 
    b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);

这里是这里的getRotation()方法

public static Matrix getRotation(Context context, Uri selectedImage) { 

     Matrix matrix = new Matrix(); 
     ExifInterface exif; 
     try { 

      exif = new ExifInterface(selectedImage.getPath()); 

      int orientation = exif.getAttributeInt(
        ExifInterface.TAG_ORIENTATION, 
        ExifInterface.ORIENTATION_NORMAL); 
      Utility.log("orientation", orientation + ""); 
      if (orientation == 6) { 
       matrix.postRotate(90); 
      } else if (orientation == 3) { 
       matrix.postRotate(180); 
       matrix.postScale((float) b.getWidth(), (float) b.getHeight()); 
      } else if (orientation == 8) { 
       matrix.postRotate(270); 
      } else { 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return matrix; 

    }

b为您的位图。

+0

怎样在我的方法使用此代码? – CarinaMj

+0

您的decodeSampledBitmapFromFile()方法返回位图。使用在Bitmap.createBitmap该位图(B,0,0,b.getWidth(),b.getHeight(),矩阵,TRUE);它也会给你一个位图返回旋转的位图。 –