關於android中呼叫系統拍照,返回圖片是旋轉90度..

yangxi_001發表於2014-07-11

由於專案的需要,沒有自定義拍照功能,僅僅呼叫了系統的拍照程式..但是出現了一個問題,就是拍照完成顯示圖片居然是被旋轉的圖片....

解決辦法:

/** 
				 * 獲取圖片的旋轉角度,有些系統把拍照的圖片旋轉了,有的沒有旋轉 
				 */  
				int degree = readPictureDegree(f.getAbsolutePath());  
				
				BitmapFactory.Options opts=new BitmapFactory.Options();//獲取縮圖顯示到螢幕上
				opts.inSampleSize=2;
				Bitmap cbitmap=BitmapFactory.decodeFile(f.getAbsolutePath(),opts);
				
				/** 
				 * 把圖片旋轉為正的方向 
				 */  
				Bitmap newbitmap = rotaingImageView(degree, cbitmap);  
				iv.setImageBitmap(newbitmap);

 

01 /**
02      * 讀取圖片屬性:旋轉的角度
03      * @param path 圖片絕對路徑
04      * @return degree旋轉的角度
05      */
06     public static int readPictureDegree(String path) {
07         int degree  = 0;
08         try {
09                 ExifInterface exifInterface = new ExifInterface(path);
10                 int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
11                 switch (orientation) {
12                 case ExifInterface.ORIENTATION_ROTATE_90:
13                         degree = 90;
14                         break;
15                 case ExifInterface.ORIENTATION_ROTATE_180:
16                         degree = 180;
17                         break;
18                 case ExifInterface.ORIENTATION_ROTATE_270:
19                         degree = 270;
20                         break;
21                 }
22         catch (IOException e) {
23                 e.printStackTrace();
24         }
25         return degree;
26     }
27    /*
28     * 旋轉圖片
29     * @param angle
30     * @param bitmap
31     * @return Bitmap
32     */ 
33    public static Bitmap rotaingImageView(int angle , Bitmap bitmap) { 
34        //旋轉圖片 動作  
35        Matrix matrix = new Matrix();; 
36        matrix.postRotate(angle); 
37        System.out.println("angle2=" + angle); 
38        // 建立新的圖片  
39        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 00
40                bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
41        return resizedBitmap; 
42    }

    

 

/**
	 * 讀取圖片屬性:旋轉的角度
	 * @param path 圖片絕對路徑
	 * @return degree旋轉的角度
	 */
    public static int readPictureDegree(String path) {
        int degree  = 0;
        try {
                ExifInterface exifInterface = new ExifInterface(path);
                int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                        degree = 90;
                        break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                        degree = 180;
                        break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                        degree = 270;
                        break;
                }
        } catch (IOException e) {
                e.printStackTrace();
        }
        return degree;
    }
   /*
    * 旋轉圖片 
    * @param angle 
    * @param bitmap 
    * @return Bitmap 
    */  
   public static Bitmap rotaingImageView(int angle , Bitmap bitmap) {  
       //旋轉圖片 動作   
       Matrix matrix = new Matrix();;  
       matrix.postRotate(angle);  
       System.out.println("angle2=" + angle);  
       // 建立新的圖片   
       Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,  
               bitmap.getWidth(), bitmap.getHeight(), matrix, true);  
       return resizedBitmap;  
   }

相關文章