7

Photo is rotating 90 degree while capturing from camera in samsung mobile rest of other mobiles(HTC) its working fine. Please help me for this.

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, IMAGE_CAPTURE); 

@Override 
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
    super.onActivityResult(requestCode, resultCode, data);    
      try
    {
    if (requestCode == IMAGE_CAPTURE) {
       if (resultCode == RESULT_OK){

       Uri contentUri = data.getData();
       if(contentUri!=null)
       {
        String[] proj = { MediaStore.Images.Media.DATA };         
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);         
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
        cursor.moveToFirst();         
        imageUri = Uri.parse(cursor.getString(column_index));
       }

       tempBitmap = (Bitmap) data.getExtras().get("data"); 
       mainImageView.setImageBitmap(tempBitmap);
       isCaptureFromCamera = true;
    }
 }
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • Do you expect portrait image orientation? – Alex Cohn Nov 17 '12 at 16:09
  • No i expect same orientation of image as i capture for ex i capture photo in portrait mode then it should be in portrait and same for landscape..please help me – Ajay S Nov 17 '12 at 17:37
  • 1
    There are numerous different bugs with portrait orientation of camera on different Android devices, including Samsung. If only possible, use landscape orientation and fake portrait mode by using rotated UI elements, as the stock camera app does. – Alex Cohn Nov 17 '12 at 17:54
  • @ Alex Cohn is there any solution for this problem – Ajay S Nov 22 '12 at 16:45
  • I had that issue as well, take a look at http://stackoverflow.com/questions/8450539/images-taken-with-action-image-capture-always-returns-1-for-exifinterface-tag-or that sorted it out for me. – ekatz Jan 31 '13 at 22:27
  • Samsung devices sucks. Testing particulary on samsung devices is horrible. It captures video and images in HD resolutions only. – Mohanish Apr 16 '15 at 07:21

4 Answers4

11

Some device rotate image according to device orientation .

here i have write one common method to get orientation and get image in right scale

    public  Bitmap decodeFile(String path) {//you can provide file path here 
        int orientation;
        try {
            if (path == null) {
                return null;
            }
            // decode image size 
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 0;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE
                        || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
            scale++;
            }
            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            Bitmap bm = BitmapFactory.decodeFile(path, o2);
            Bitmap bitmap = bm;

            ExifInterface exif = new ExifInterface(path);

            orientation = exif
                    .getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

            Log.e("ExifInteface .........", "rotation ="+orientation);

//          exif.setAttribute(ExifInterface.ORIENTATION_ROTATE_90, 90);

            Log.e("orientation", "" + orientation);
            Matrix m = new Matrix();

            if ((orientation == ExifInterface.ORIENTATION_ROTATE_180)) {
                m.postRotate(180);
//              m.postScale((float) bm.getWidth(), (float) bm.getHeight());
                // if(m.preRotate(90)){
                Log.e("in orientation", "" + orientation);
                bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                        bm.getHeight(), m, true);
                return bitmap;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                m.postRotate(90); 
                Log.e("in orientation", "" + orientation);
                bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                        bm.getHeight(), m, true);
                return bitmap;
            }
            else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                m.postRotate(270);
                Log.e("in orientation", "" + orientation);
                bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                        bm.getHeight(), m, true);
                return bitmap;
            } 
            return bitmap;
        } catch (Exception e) {
            return null;
        }

    }

EDIT:

This code is not optimized , i just show the logic code from my one of the test project.

dharmendra
  • 7,835
  • 5
  • 38
  • 71
  • it work fine but it give "Out of memory Exception" while i am capturing continuous snapshot in Samsung Galaxy tab2(7"). Please can you give some solution. – Hasmukh Mar 19 '13 at 07:51
  • @Hasmukh you can change the scale size bitmap , in this code i made it dependable – dharmendra Mar 19 '13 at 07:56
  • yes, If i give scale type 8 then it work fine but i need show large as preview image and also need to upload that large image as byte64. – Hasmukh Mar 19 '13 at 08:02
  • @Hasmukh memory error depends on device heap size sometimes and sometimes we have to be careful with those bitmaps, its needful to recycle bitmap after use it . – dharmendra Mar 19 '13 at 13:59
  • I keep getting orientation = 0 when I use this code no matter how I hold my HTC phone. Anyone having the same issue? – PaperThick Jun 25 '13 at 07:48
  • @PaperThick your device might took the picture in right orientation , or your device orientation mode might off . – dharmendra Jun 25 '13 at 09:11
2

Another thing you can add to the above solutions is "samsung".contentEquals(Build.MANUFACTURER). If you know that your problem is only with Samsung devices you could be reasonably sure that you need to rotate the image returned (only) if ("samsung".contentEquals(Build.MANUFACTURER) && getActivity().getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT // && width > height//) // here you know you need to rotate

You could be "reasonably" confident that the rotation is warranted then.

astryk
  • 1,256
  • 13
  • 20
  • its not solution. Photo from samsung always have WIDTH>HEIGHT. Bug only with portrate mode. – Peter Nov 14 '17 at 10:04
0
public static Bitmap rotateBitmap(Bitmap b, float degrees) {
    Matrix m = new Matrix();
    if (degrees != 0) {
        // clockwise
        m.postRotate(degrees, (float) b.getWidth() / 2,
                (float) b.getHeight() / 2);
    }

    try {
        Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                b.getHeight(), m, true);
        if (b != b2) {
            b.recycle();
            b = b2;
        }
    } catch (OutOfMemoryError ex) {
        // We have no memory to rotate. Return the original bitmap.
    }
    return b;
}
alistair
  • 1,164
  • 10
  • 27
0

If it's really a bug then you may have to manually rotate it back to landscape. Bitmap data always has a width and a height, just take a look at the numbers and if the width is less than the height rotate the image as per alistair3408's answer.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236