2014-01-11 37 views
0

我想從android中的相機捕獲圖像。捕獲的圖片旋轉90度自己

Camera.Parameters parameters = camera.getParameters(); 
parameters.set("orientation", "portrait"); 
camera.setDisplayOrientation(90); 
parameters.setRotation(90); 
camera.setParameters(parameters); 

查看不是肖像和直,但是當我拍照時,它總是旋轉90度。

我試過parameters.setRotation(90);爲0,180但沒有效果。

+0

你的意思是你拍攝的圖像結果爲旋轉嗎? – AndyN

+0

是它始終在旋轉@ N2P –

+0

@MuhammadUmar看到我的答案,它肯定會解決您的問題 –

回答

0

看到這個線程

Photo rotate 90 degree while capture in some phones

你必須從EXIF代碼檢查,以解決這個問題。一些有捕捉照片旋轉90度的bug的設備。

在線程中閱讀我的答案,它將解決您的問題。

+0

我以前嘗試過您的代碼,但問題是,我的圖像返回Orientation == Normal。這不包含在你的代碼中。我現在應該怎麼做。 –

+0

只需檢查方向角度。正如你告訴我的,它返回正常值意味着角度值是0或90.所以簡單地根據角度來做 –

0

我的應用程序中也有同樣的問題。下面的解決方案對我來說工作得很好,希望它也能幫助你。

添加按照您的OnActivityResult方法的代碼,

   Bitmap imgTemp = null; 


       String path = mImageCaptureUri.getPath(); 

       if(imgTemp != null && !imgTemp.isRecycled()) 
       { 
        imgTemp.recycle();    
        imgTemp = null; 
       } 

       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inDither = false; 
       options.inPurgeable = true; 
       options.inInputShareable = true; 
       options.inTempStorage = new byte[10*1024]; 
       imgTemp = BitmapFactory.decodeFile(path,options); 
       imgTemp = Bitmap.createScaledBitmap(imgTemp, 480, 800, true); 

       ExifInterface ei = new ExifInterface(path); 
       int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

       switch(orientation) { 
        case ExifInterface.ORIENTATION_ROTATE_90: 
         imgTemp = rotateImage(imgTemp, 90); 
         break; 
        case ExifInterface.ORIENTATION_ROTATE_180: 
         imgTemp = rotateImage(imgTemp, 180); 
         break; 
        // etc. 
       } 

這裏,mImageCaptureUri是URI由攝像機拍攝的圖像。

而且也是你的活動添加方法rotateImage

public Bitmap rotateImage(Bitmap source, float angle) 
{ 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(angle); 
     return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); 
} 
0
Bitmap bm = decodeSampledBitmapFromUri(file.getAbsolutePath()); 
try 
          { 
           ExifInterface ei = new ExifInterface(file.getAbsolutePath()); 
           int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
           switch (orientation) 
           { 
           case ExifInterface.ORIENTATION_ROTATE_90: 
            rotate_angle = 90; 
           break; 
           case ExifInterface.ORIENTATION_ROTATE_180: 
            rotate_angle = 180; 
           break; 
           case ExifInterface.ORIENTATION_ROTATE_270: 
            rotate_angle = 270; 
           break; 
           default: 
           break; 
           } 
          } 
          catch (IOException e) 
          { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
Matrix matrix = new Matrix(); 
          matrix.postRotate(rotate_angle); 

public Bitmap decodeSampledBitmapFromUri(String path) 
     { 
      Bitmap bm = null; 
      // First decode with inJustDecodeBounds=true to check dimensions 
      final BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(path, options); 

      // Calculate inSampleSize 
      Display display = getWindowManager().getDefaultDisplay(); 
      DisplayMetrics outMetrics = new DisplayMetrics(); 
      display.getMetrics(outMetrics); 
      float density = getResources().getDisplayMetrics().density; 
      int dpHeight = (int) ((outMetrics.heightPixels/density) * .8); // 80% 
                       // width 
                       // and 
                       // height 
      int dpWidth = (int) ((outMetrics.widthPixels/density) * .8); 
      options.inSampleSize = calculateInSampleSize(options, dpWidth, dpHeight); 

      // Decode bitmap with inSampleSize set 
      options.inJustDecodeBounds = false; 
      bm = BitmapFactory.decodeFile(path, options); 
      return bm; 
     } 

     public int calculateInSampleSize(

     BitmapFactory.Options options, int reqWidth, int reqHeight) 
     { 
      // Raw height and width of image 
      final int height = options.outHeight; 
      final int width = options.outWidth; 
      int inSampleSize = 1; 
      if (height > reqHeight || width > reqWidth) 
      { 
       if (width > height) 
       { 
        inSampleSize = Math.round((float) height /(float) reqHeight); 
       } 
       else 
       { 
        inSampleSize = Math.round((float) width/(float) reqWidth); 
       } 
      } 
      return inSampleSize; 
     } 

    }