編輯:關於 Android 程式設計
需求背景
在開發一個仿新浪微博發文的專案時,需要實現圖片上傳功能;另外一個專案則用到了手機拍攝圖片的功能。這兩個專案都有同一個訴求——把圖片正確地上傳到伺服器。
遇到的問題
問題恰恰出在圖片的方向上:有的手機拍出來的圖片會被旋轉 90 度,有的會旋轉 180 度,還有的手機一切正常。可伺服器端要求的是正的圖片,於是麻煩來了——使用者發一張照片到微博上,看到的卻是躺倒的,這體驗實在說不過去;而另一個專案裡,旋轉過的圖片直接導致配對出錯,問題就更嚴重了。
解決思路
一開始上網翻遍了 Google 和百度都沒找到理想方案,於是想到兩種思路。第一種是:手機拍照結束後,在回傳結果的處理裡立刻跳轉到一個新頁面,讓使用者手動把圖片旋轉到正確角度,再按確定,這樣上傳上去的就是正的圖片了。第二種則是在論壇裡發現的方法——直接讀取圖片的屬性,取得其旋轉角度,由程式自動校正。
第一種方式:手動旋轉
在 onActivityResult 裡處理頁面跳轉:
<span > Intent intent = new Intent(SendMicoBlog.this, RotaingActivity.class); MyApp myApp = (MyApp)getApplication(); myApp.setName(bitmap);//修改之後的名稱 myApp.setPhoto(isPhoto); startActivityForResult(intent, 1);</span>
在 RotaingActivity 裡就可以進行相應的旋轉處理了:
<span > //獲取bitmap MyApp myApp = (MyApp)getApplication(); bitmap = myApp.getName(); System.out.println("RotaingActivity w = " + bitmap.getWidth() + "H = " + bitmap.getHeight()); myBitmap = bitmap; myWidth = bitmap.getWidth(); myHeight = bitmap.getHeight(); // 創建操作圖片用的matrix對象 matrix = new Matrix(); Drawable drawable = ImageDispose.bitmapToDrawable(bitmap); imageView.setBackgroundDrawable(drawable); btnCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { RotaingActivity.this.finish(); } }); btnRightRotaing.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myBitmap = rotaingImageView(-90); } }); btnLeftRotaing.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myBitmap = rotaingImageView(+90); } }); btnOK.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { MyApp myApp = (MyApp)getApplication(); myApp.setName(myBitmap);//修改之後 System.out.println("OK resultcoder"); Intent intent = new Intent(RotaingActivity.this, SendMicoBlog.class); setResult(6, intent); RotaingActivity.this.finish(); } }); } private Bitmap rotaingImageView(int angle2) { //旋轉圖片 動作 matrix.postRotate(angle2); System.out.println("angle2=" + angle2); // 創建新的圖片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, myWidth, myHeight, matrix, true); Drawable drawable = ImageDispose.bitmapToDrawable(resizedBitmap); imageView.setBackgroundDrawable(drawable); return resizedBitmap; }</span> 第二種方式:自動讀取角度校正(推薦)
這種方式簡單多了,程式會自動把照片校正到正確方向,完全不需要使用者介入,體驗明顯更好。
在 onActivityResult 裡處理:
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 8; File file = new File(SD_CARD_TEMP_DIR); /** * 獲取圖片的旋轉角度,有些系統把拍照的圖片旋轉了,有的沒有旋轉 */ int degree = ImageDispose.readPictureDegree(file.getAbsolutePath());
<span >Bitmap cameraBitmap = BitmapFactory.decodeFile(SD_CARD_TEMP_DIR, bitmapOptions); bitmap = cameraBitmap; /** * 把圖片旋轉為正的方向 */ bitmap = ImageDispose.rotaingImageView(degree, bitmap); upload(bitmap);</span>
<span > /** * 旋轉圖片 * @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; }</span>
/** * 讀取圖片屬性:旋轉的角度 * @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; } 這樣一來問題就搞定了,此時你上傳的照片方向就是正確的了。
第一種方式的效果如下:

以上就是解決 Android 部分手機拍照上傳後圖片被旋轉問題的完整思路,希望對大家有所幫助。
仿微信模糊搜索:EditText 動態過濾 ListView 並高亮匹配
模糊搜索框是 App 中很常見的需求,本文以安卓端為例,實現一個仿微信的模糊搜索效果。 先看上圖,效果還不錯。圖一為未點擊狀態,圖二為點擊之後的狀態,圖三為輸入之後的狀態
Android動態加載Activity原理與實作
動態加載Activity是Android插件化中的常見主題。它的核心問題是:系統預設只會用目前應用的ClassLoader加載本應用內的Activity,因此若要啟動外部
九宮格切圖(.9.png)完全指南:讓背景自適應又不失真
自適應且失真的背景圖,是 UI 開發中最常見的痛點之一,也是介面設計師最希望被解決的問題。本文把 .9.png 的原理、製作流程與進階技巧一次整理清楚。 一、問題從哪裡
Android 系統 OTA 升級全流程解析:從 Recovery 到腳本執行
Android 裝置的系統升級有兩條路徑。一條是透過介面把升級包路徑傳給系統,自動完成升級後重啟,多指手機廠商的客戶端線上升級;另一條是手動進入 Recovery 模式挑