2013-01-14 213 views
0

我發現了一個非常有趣的問題。拍攝照片後(我將設備保持縱向模式,不旋轉)後,給定的照片有時會旋轉,但並非總是如此。我知道,某些設備會給出始終旋轉的照片,但可以使用exif或mediastore信息進行旋轉。但在這種情況下,exif和mediastore表示方向爲0,但圖像是旋轉的。最令人沮喪的事情是完全隨機的。代碼非常簡單:Android相機圖像隨機旋轉

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, currentFileUri); 
startActivityForResult(intent, RequestCodeCollection.CAMERA_IMAGE_CAPTURE); 

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     try { 
      oldImageExifInterface = new ExifInterface(currentFileUri.getPath()); 
     } 
} 

有沒有人看到這個問題?在操作系統更新後,我經歷了Galaxy Nexus(4.1.1)

+0

的可能的複製([爲什麼圖像捕捉通過相機獲取的意圖在機器人某些設備旋轉] http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets -rotate-on-some-devices-in-android) –

+0

Answere present here - http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-設備功能於安卓 –

回答

0

試試這個。

try { 
     File f = new File(imagePath); 
     ExifInterface exif = new ExifInterface(f.getPath()); 
     int orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 

     int angle = 0; 

     if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { 
      angle = 90; 
     } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { 
      angle = 180; 
     } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { 
      angle = 270; 
     } 

     Matrix mat = new Matrix(); 
     mat.postRotate(angle); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 2; 

     Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), 
       null, options); 
     bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), 
       bmp.getHeight(), mat, true); 
     ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, 
       outstudentstreamOutputStream); 
     imageView.setImageBitmap(correctBmp); 

    } catch (IOException e) { 
     Log.w("TAG", "-- Error in setting image"); 
    } catch (OutOfMemoryError oom) { 
     Log.w("TAG", "-- OOM Error in setting image"); 
    }