Capture photo rotate 90 degree in samsung mobile

11,081

Solution 1

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.

Solution 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.

Share:
11,081

Related videos on Youtube

Ajay S
Author by

Ajay S

"If you see scary things, look for the helpers - you'll always see people helping" - Fred Rogers LinkedIn: https://www.linkedin.com/in/ajay-saini-88a22728?ppe=1 Medium: https://medium.com/@ajaysaini.official GitHub: https://github.com/ajaysaini-sgvu

Updated on September 16, 2022

Comments

  • Ajay S
    Ajay S over 1 year

    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;
        }
     }
    
    • Alex Cohn
      Alex Cohn over 11 years
      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.
  • Hasmukh
    Hasmukh about 11 years
    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.
  • dharmendra
    dharmendra about 11 years
    @Hasmukh you can change the scale size bitmap , in this code i made it dependable
  • Hasmukh
    Hasmukh about 11 years
    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.
  • dharmendra
    dharmendra about 11 years
    @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 .
  • PaperThick
    PaperThick almost 11 years
    I keep getting orientation = 0 when I use this code no matter how I hold my HTC phone. Anyone having the same issue?
  • dharmendra
    dharmendra almost 11 years
    @PaperThick your device might took the picture in right orientation , or your device orientation mode might off .
  • Peter
    Peter over 6 years
    its not solution. Photo from samsung always have WIDTH>HEIGHT. Bug only with portrate mode.