2015-11-06 39 views
0

我有一個客戶端要求添加旋轉支持的應用程序。 以下是從相機拍攝圖像的當前代碼。檢測從相機拍攝的位圖應爲哪個方向

camera.takePicture(null, null, new PictureCallback() { 
        public void onPictureTaken(byte[] data, Camera camera) { 

         if (data != null) { 

          Matrix mtx = new Matrix(); 
          mtx.postRotate(180); 

          float density   = Resources.getSystem().getDisplayMetrics().density; 

          Bitmap bm = BitmapFactory.decodeByteArray(data, 0, (data != null) ? data.length : 0); 
          Bitmap scaled = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), mtx, true); 
          bm = Bitmap.createScaledBitmap(scaled, (int)(480*density), (int)(320*density), true);  //height and width are backwards because its portrait 
          previewImage(bm); 
         } 
        } 


       }); 

什麼我試圖找出是制定出捕獲的位圖應該是什麼方向,即當用戶在橫向拍照與設備,我們需要翻轉的最快方法:

Bitmap.createScaledBitmap(scaled, (int)(480*density), (int)(320*density), true); 

到:

Bitmap.createScaledBitmap(scaled, (int)(320*density), (int)(480*density), true); 

,改變它的mtx.postRotate

我的問題是鍛鍊的最佳途徑找出輪換圖像被欺騙。

回答

0

試試這個

private Bitmap checkForRotation(String filename, Bitmap bitmap) { 
    Bitmap myBitmap = bitmap; 
    ExifInterface ei = null; 
    try { 
     ei = new ExifInterface(filename); 
     new ExifInterface(filename); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

    switch (orientation) { 

    //Here you get the orientation and do what ever you want to do with it as i am rotating the image 

    case ExifInterface.ORIENTATION_ROTATE_90: 
     myBitmap = rotateImage(bitmap, 90); 

     break; 
    case ExifInterface.ORIENTATION_ROTATE_180: 
     myBitmap = rotateImage(bitmap, 180); 
     break; 
    } 
    return myBitmap; 
} 
相關問題