【问题标题】:Captured picture rotated 90 degree own拍摄的图片自己旋转了 90 度
【发布时间】:2014-01-11 08:35:18
【问题描述】:

我正在尝试从 android 中的相机捕获图像。

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

视图不是纵向和直的,但是当我拍照时,它总是旋转 90 度。

我试过parameters.setRotation(90);到0、180但没有效果。

【问题讨论】:

  • 你的意思是你捕获的图像会变成旋转的吗?
  • 是的,它总是在旋转@N2P
  • @MuhammadUmar 看看我的回答,肯定能解决你的问题

标签: java android eclipse


【解决方案1】:

查看此帖

Photo rotate 90 degree while capture in some phones

您必须从 exif 代码中检查它才能解决此问题。某些设备存在将捕获照片旋转 90 度的错误。

在帖子中阅读我的答案,它将解决您的问题。

【讨论】:

  • 我之前试过你的代码,但问题是,我的图像返回方向 == 正常。您的代码中不包含哪个。我现在该怎么办。
  • 只需检查方位角。正如你告诉我的,它返回 Normal,这意味着角度值是 0 或 90。所以只需根据角度进行操作
【解决方案2】:

【讨论】:

    【解决方案3】:

    我的申请中也遇到了同样的问题。下面的解决方案对我来说很好,希望它也能帮助你。

    OnActivityResultmethod 中添加以下代码,

                    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);
    }
    

    【讨论】:

      【解决方案4】:
      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;
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 2018-03-06
        • 2012-02-10
        • 2012-03-08
        • 1970-01-01
        • 2012-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多