Java 拍摄的照片旋转了90度

Java 拍摄的照片旋转了90度,java,android,eclipse,Java,Android,Eclipse,我正在尝试用安卓系统从相机中捕捉图像 Camera.Parameters parameters = camera.getParameters(); parameters.set("orientation", "portrait"); camera.setDisplayOrientation(90); parameters.setRotation(90); camera.setParameters(parameters); 视图不是纵向的和笔直的,但当我拍照时,它总是旋转90度 我尝试了参数设

我正在尝试用安卓系统从相机中捕捉图像

Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setDisplayOrientation(90); 
parameters.setRotation(90);
camera.setParameters(parameters);
视图不是纵向的和笔直的,但当我拍照时,它总是旋转90度

我尝试了参数设置。设置旋转90;到0,180,但没有效果。

查看此线程

您必须从exif代码中进行检查才能解决此问题。有些设备有缺陷,可以将拍摄的照片旋转90度


阅读线程中的我的答案,它将解决您的问题。

您可以查看以下链接了解您的问题
照片方向属性使其旋转。

我的应用程序中也有同样的问题。下面的解决方案对我来说很好,希望它也能帮助你

在OnActivityResultmethod中添加以下代码

这里,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);
}

你的意思是说你拍摄的图像会变成旋转的吗?是的,它总是处于旋转状态@N2P@MuhammadUmar看到我的答案,它肯定会解决你的问题我之前试过你的代码,但问题是,我的图像返回方向==正常。您的代码中不包含这些内容。我现在该怎么办?只需检查方向角。正如你们告诉我的,它返回正常值,这意味着角度值为0或90。所以只需根据角度来做
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);
}
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;
        }

    }