2014-01-11 36 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; 
     } 

    }