0

I am trying to capture an image from camera in android.

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

View is not portrait and straight but When I take picture, it is always 90 deg rotated.

I have tried parameters.setRotation(90); to 0, 180 but no effect.

Ajay S
  • 48,003
  • 27
  • 91
  • 111
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193

4 Answers4

0

See this thread

Photo rotate 90 degree while capture in some phones

You have to check it from the exif code to resolve this. Some device which have bug which rotate the capture photo 90 degree.

Read my answer in the thread and it will solve your problem.

Community
  • 1
  • 1
Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • i tried your code earlier, But the problem is,my image returns Orientation == Normal. Which is NOT INCLUDED in your code. What should i do now. – Muhammad Umar Jan 13 '14 at 08:20
  • Simply check the orientation angle. As you told to me it returned Normal it means Angle value is either for 0 or 90. So simply do on basis of angle – Ajay S Jan 15 '14 at 14:42
0

You can check the following link for your problem Picture taken from camera or gallery when using in imageview its orientation getting changed, and sometimes vertically stretched in Android There is photo orientation property which makes it rotated.

Community
  • 1
  • 1
Abhishek Agarwal
  • 1,907
  • 12
  • 21
0

I also had the same issue in my application. Below solution worked fine for me, hope it will help you too.

Add following code in your 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.
                }

Here, mImageCaptureUri is the Uri for the image captured by camera.

And also add method rotateImage in your activity,

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);
}
AndyN
  • 1,742
  • 1
  • 15
  • 30
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;
        }

    }
Amsheer
  • 7,046
  • 8
  • 47
  • 81