android 選擇圖片或拍照時旋轉了90度問題

來源:互聯網
上載者:User

標籤:

由於前面的博文中忽略了點內容,所以在這裡補上,下面內容就是解決拍照或者選擇圖片顯示的時候圖片旋轉了90度或者其他度數問題,以便照片可以正面顯示:具體如下:

首先直接看上面博文下的拍完照或者選完圖後處理部分:

@Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        switch (resultCode) {        case 1:            if (data != null) {                // 取得返回的Uri,基本上選擇照片的時候返回的是以Uri形式,但是在拍照中有得機子呢Uri是空的,所以要特別注意                Uri mImageCaptureUri = data.getData();                // 返回的Uri不為空白時,那麼圖片資訊資料都會在Uri中獲得。如果為空白,那麼我們就進行下面的方式擷取                if (mImageCaptureUri != null) {                    setImage(mImageCaptureUri);// 根據Uri處理並顯示圖片                }            }            break;        default:            break;        }    }

第二:處理90度問題並顯示:

    private void setImage(Uri mImageCaptureUri) {        // 不管是拍照還是選擇圖片每張圖片都有在資料中儲存也儲存有對應旋轉角度orientation值        // 所以我們在取出圖片是把角度值取出以便能正確的顯示圖片,沒有旋轉時的效果觀看        ContentResolver cr = this.getContentResolver();        Cursor cursor = cr.query(mImageCaptureUri, null, null, null, null);// 根據Uri從資料庫中找        if (cursor != null) {            cursor.moveToFirst();// 把遊標移動到首位,因為這裡的Uri是包含ID的所以是唯一的不需要迴圈找指向第一個就是了            String filePath = cursor.getString(cursor.getColumnIndex("_data"));// 擷取圖片路徑            String orientation = cursor.getString(cursor                    .getColumnIndex("orientation"));// 擷取旋轉的角度            cursor.close();            if (filePath != null) {                Bitmap bitmap = BitmapFactory.decodeFile(filePath);//根據Path讀取資源圖片                int angle = 0;                if (orientation != null && !"".equals(orientation)) {                    angle = Integer.parseInt(orientation);                }                if (angle != 0) {                    // 下面的方法主要作用是把圖片轉一個角度,也可以放大縮小等                    Matrix m = new Matrix();                    int width = bitmap.getWidth();                    int height = bitmap.getHeight();                    m.setRotate(angle); // 旋轉angle度                    bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,                            m, true);// 從新產生圖片                                    }                photo.setImageBitmap(bitmap);            }        }    }

 

android 選擇圖片或拍照時旋轉了90度問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.