Android 相機在Portrait模式下照相儲存照片

來源:互聯網
上載者:User

在使用android的camera的時候會遇到兩個問題,一個是camera在preview的時候orientation的問題,第二個就是在takePicture之後回遇到儲存下來的圖片旋轉90度的問題

先解決第一個preview的orientation的問題,第一:在android2.2與以後的sdk版本中camera的orientation都是用的landscape,如果你的activity的screenOrientation設定成landscape的話,就不會有這個問題;第二:如果你的activity必須用portrait,那麼可以在調用camera.open()方法之後調用下面這個方法來解決這個問題,
[java]  
camera.setDisplayOrientation(90); 

如果你用的sdk是2.2之前的話,可以這樣解決:
[java] 
Method rotateMethod = android.hardware.Camera.class.getMethod("setDisplayOrientation", int.class); 
rotateMethod.invoke(camera, 90); 


下面就是如何解決儲存Portrait模式片旋轉90度的問題
下面的程式在onPictureTaken方法裡面執行
[java] 
BitmapFactory.Options options = new BitmapFactory.Options(); 
            options.inSampleSize = 6; 
            options.inDither = false; 
            options.inPurgeable = true; 
            options.inInputShareable = true; 
[java] 
<span style="white-space:pre">      </span>options.inTempStorage = new byte[32 * 1024]; 
[java] 
       options.inPreferredConfig = Bitmap.Config.RGB_565; 
       Bitmap bMap; 
       bMap = BitmapFactory.decodeByteArray(imgData[0], 0, imgData[0].length, options); 
       if(bMap.getHeight() < bMap.getWidth()){ 
           orientation = 90; 
       } else { 
           orientation = 0;   www.2cto.com
       } 
 
       Bitmap bMapRotate; 
       if (orientation != 0) { 
           Matrix matrix = new Matrix(); 
           matrix.postRotate(orientation); 
           bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), 
                   bMap.getHeight(), matrix, true); 
       } else 
           bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(), 
                   bMap.getHeight(), true); 
 
 
       FileOutputStream out; 
       try { 
        File imgFile = new File("/xxxx/xxx/snap.jpeg"); 
           out = new FileOutputStream(imgFile); 
           bMapRotate.compress(Bitmap.CompressFormat.JPEG, 90, out); 
           if (bMapRotate != null) { 
               bMapRotate.recycle(); 
               bMapRotate = null; 
           } 
<span style="white-space:pre">  </span>camera.startPreview(); 
       } catch (FileNotFoundException e) { 
           e.printStackTrace(); 
       } 


作者:hundsong

聯繫我們

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