解決三星手機拍照後,圖片旋轉。

來源:互聯網
上載者:User

標籤:

最近項目中遇到用三星手機拍照,圖片會自動旋轉,應該是三星內部系統的功能,然後需要是不讓他旋轉,找到了方法。

原理就是,擷取到圖片,判斷它的旋轉角度,然後相應的旋轉回來。

在拍照的返回結果中,擷取到圖片的路徑。

 path = filePath + fileName;  //path 為拍照返回的路徑
                    File file = new File(path);
                    int degree = readPictureDegree(file.getAbsolutePath());
                    Bitmap smallBitmap=FileUtils.compressSize(path, 800, 800);
                    smallBitmap = rotaingImageView(degree, smallBitmap);
                saveAllPath = FileUtils.saveBitmap(smallBitmap, System.currentTimeMillis() +(Math.random() * 10000) + ".png");//品質壓縮並儲存得到path
ImageItem takePhoto = new ImageItem();
takePhoto.setBitmap(smallBitmap);
takePhoto.setImagePath(saveAllPath);
Bimp.tempSelectBitmap.add(takePhoto);

/**

     * 尺寸壓縮
     * @param path 圖片絕對路徑
     * @return degree旋轉的角度
     */

 public static Bitmap compressSize(final String path,final int w,final int h){
                     //Looper.prepare();
                   
    // TODO Auto-generated method stub
    BitmapFactory.Options opts = new BitmapFactory.Options();
           // 設定為ture只擷取圖片大小
           opts.inJustDecodeBounds = true;//唯讀邊,不讀內容
           opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
           // 返回為空白
           BitmapFactory.decodeFile(path, opts);
           int width = opts.outWidth;
           int height = opts.outHeight;
        // 判斷尾碼名
                   String suffix = "";
                   CompressFormat format = null;
                   if (path.endsWith(".jpg")) {
                       suffix = ".jpg";
                       format = CompressFormat.JPEG;
                   } else if (path.endsWith(".jpeg")) {
                       suffix = ".jpeg";
                       format = CompressFormat.JPEG;
                   } else if (path.endsWith(".png")) {
                       suffix = ".png";
                       format = CompressFormat.PNG;
                   } else {
                       suffix = ".jpg";
                       format = CompressFormat.JPEG;
                   }
           float scaleWidth = 0.f, scaleHeight = 0.f;
           if (width > w || height > h) {//如果寬度大於 傳入的寬度  或者 高度大於 傳入的高度大於
               // 縮放
               scaleWidth = ((float) width) / w;
               scaleHeight = ((float) height) / h;
           }
           opts.inJustDecodeBounds = false;
           //縮放後的高度和寬度取最大值
           float scale = Math.max(scaleWidth, scaleHeight);
           opts.inSampleSize = (int) scale;//此處是最後的寬高值
          Bitmap bMapRotate = BitmapFactory.decodeFile(path, opts);
          if (bMapRotate!=null) {
return bMapRotate;
}
          return null;
    }


 /**

     * 讀取圖片屬性:旋轉的角度
     * @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;
    }
    /*
    *
    * 旋轉圖片
    * */
    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;

    }

  /**
     * 儲存圖片,返迴路徑
     * @param bm
     * @param picName
     * @return
     */
    public static String saveBitmap(Bitmap bm, String picName) {
        try {
            if (!isFileExist(picName)) {
                File tempf = createSDDir("");
            }
            File f = new File(SDPATH, picName);


            if (f.exists()) {
                f.delete();


            }
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return SDPATH + picName;
    }

解決三星手機拍照後,圖片旋轉。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.