天天看點

三星手機在android中拍照時,照片反轉的問題

最近在項目裡,發現使用三星手機拍照時照片被反轉

解覺方式:

在拍照後調用:(onActivityResult)

* 擷取圖檔的旋轉角度,有些系統把拍照的圖檔旋轉了,有的沒有旋轉 
*/  
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);
           
/**
	 * 讀取圖檔屬性:旋轉的角度
	 * @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;  
   }