1. 程式人生 > >關於android中呼叫系統拍照,返回圖片是旋轉90度..

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

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

解決辦法:

/** 
				 * 獲取圖片的旋轉角度,有些系統把拍照的圖片旋轉了,有的沒有旋轉 
				 */  
				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; }

https://my.oschina.net/techstan/blog/142592

   /**
     * 獲取圖片的旋轉角度
     *
     * @param path 圖片絕對路徑
     * @return 圖片的旋轉角度
     */
    public static int getBitmapDegree(String path) {
        int degree = 0;
        try {
            // 從指定路徑下讀取圖片,並獲取其EXIF資訊
            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 bitmap 需要旋轉的圖片
     * @param degree 指定的旋轉角度
     * @return 旋轉後的圖片
     */
    public static Bitmap rotateBitmapByDegree(Bitmap bitmap, int degree) {
        // 根據旋轉角度,生成旋轉矩陣
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        // 將原始圖片按照旋轉矩陣進行旋轉,並得到新的圖片
        Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
        }
        return newBitmap;
    }

ExifInterface 能拿到的資訊遠遠不止旋轉角度,其他的引數感興趣的童鞋可以看看 API 文件。