捕获的图片旋转90度自己

时间:2014-01-11 08:35:18

标签: java android eclipse

我正在尝试从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但没有效果。

4 个答案:

答案 0 :(得分:0)

见这个帖子

Photo rotate 90 degree while capture in some phones

您必须从exif代码中检查它才能解决此问题。有些设备有虫子将捕捉照片旋转90度。

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

答案 1 :(得分:0)

答案 2 :(得分: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);
}

答案 3 :(得分: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;
        }

    }