2012-06-14 30 views
8

我點擊一個按鈕打開相機應用程序。並在下一個活動中顯示拍攝的照片。但拍攝的照片旋轉90度。當我在捕獲圖像後在視圖中顯示圖像時,它的方向始終是風景。爲什麼在縱向模式下拍攝照片時照片不會以縱向顯示?捕獲的照片方向正在改變android

的onClick按鈕:

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(APP_DIR+"/latest.png")));  
startActivityForResult(i, CAPTURE_PHOTO_CONSTANT); 

內onActvityresult:

bmp = BitmapFactory.decodeFile(APP_DIR+"/latest.png"); 
startActivity(new Intent(this, DisplayActivity.class)); 

顯示拍攝的照片:

photoViewRelativeLayout.setBackgroundDrawable(new BitmapDrawable(getResources(), CaptureActivity.bmp)); 
+3

可能重複([爲什麼使用相機意圖拍攝圖像被在機器人某些設備旋轉] http://stackoverflow.com/questions/14066038/why-image-captured-using-camera -intent-gets-rotated-on-some-devices-in-android) – bummi

+0

在這裏回答http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on- some-devices-in-android –

回答

15

我有同樣的問題大多與三星handsets.Apparently三星手機設置的EXIF方向標籤,而不是個別旋轉的pixels.Reading位圖使用BitmapFactory不支持這個標籤。我發現解決這個問題的方法是在activity的onActivityResult方法中使用ExifInterface。它檢查與來自攝像頭的捕獲圖像的URI相關的方向。

     int rotate = 0; 
         try { 
          getContentResolver().notifyChange(imageUri, null); 
          File imageFile = new File(imagePath); 
          ExifInterface exif = new ExifInterface(
            imageFile.getAbsolutePath()); 
          int orientation = exif.getAttributeInt(
            ExifInterface.TAG_ORIENTATION, 
            ExifInterface.ORIENTATION_NORMAL); 

          switch (orientation) { 
          case ExifInterface.ORIENTATION_ROTATE_270: 
           rotate = 270; 
           break; 
          case ExifInterface.ORIENTATION_ROTATE_180: 
           rotate = 180; 
           break; 
          case ExifInterface.ORIENTATION_ROTATE_90: 
           rotate = 90; 
           break; 
          } 
          Log.v(Common.TAG, "Exif orientation: " + orientation); 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 

         /****** Image rotation ****/ 
         Matrix matrix = new Matrix(); 
         matrix.postRotate(orientation); 
         Bitmap cropped = Bitmap.createBitmap(scaled, x, y, width, height, matrix, true); 
+0

我是否需要在最後旋轉位圖? – Santhosh

+1

你需要在最後旋轉位圖。 –

+0

謝謝。有用。但我有一個小小的懷疑。捕獲後我們正在旋轉圖像。它是否也旋轉在三星以外的其他設備拍攝的照片?如果在XXXX設備中正確捕獲照片會怎麼樣? – Santhosh

0

你可以simpaly旋轉它

得到使用onActvityresult此代碼所拍攝的圖像orietation ....

File imageFile = new File(imageUri.toString()); 
     ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath()); 
     int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
     int rotate = 0; 
     switch(orientation) { 
     case ORIENTATION_ROTATE_270: 
      rotate-=90;break; 
     case ORIENTATION_ROTATE_180: 
      rotate-=90;break 
     case ORIENTATION_ROTATE_90: 
      rotate-=90;break 
     } 
+0

ORIENTATION_ROTATE_270,ORIENTATION_ROTATE_180和ORIENTATION_ROTATE_90的值是什麼,我無法解決所有這些符號錯誤。 – NarendraJi

1

我知道它有點工作。當我在風景模式下拍攝照片時,一切正常。如果我以肖像模式拍攝照片,我需要將相機翻轉過來,照片看起來不錯。如果您注意到我通過在每個方向前添加「ExifInterface」來改變您的代碼。我使用下面的代碼:

try { 
     ExifInterface exif = new ExifInterface(filename); 
     int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
          ExifInterface.ORIENTATION_NORMAL); 
     int rotate = 0; 
     switch(orientation) { 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       rotate-=90;break; 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       rotate-=90;break; 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       rotate-=90;break; 
      } 
       Log.d("Fragment", "EXIF info for file " + filename + ": " + rotate); 
     } catch (IOException e) { 
      Log.d("Fragment", "Could not get EXIF info for file " + filename + ": " + e); 
     } 
0

請使用Glide代替。它修復了方向本身,而且與您使用的移動設備無關。這裏\ SA樣品

Glide.with(_c).load(horizontalList.get(position).imagePath).into(holder.img_injury); 
相關問題