4

Photo is rotating 90 degree while capturing from camera in samsung mobile rest of other mobiles 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;
            }
        }
Ajay S
  • 48,003
  • 27
  • 91
  • 111

3 Answers3

11

This happens to be a bug in earlier versions of Android.

I solved this issue just getting the orientation angle and rotate the bitmap accordingly.

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;
    }
}
Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • If someone will use your solution I just wanted to mention that I think you calculate the scale the wrong way. As you said in your code comment it will be better if the scale will be a power of two. Increasing scale by one as you do will not promise us that it will be a power of two. If you want to get as close to the proper required width, height you should multiply the scale by two in each iteration. – MikeL Jun 23 '15 at 21:23
0

Also query the MediaStore.Images.Media.ORIENTATION value to get the rotation angle. Then you can rotate the image by yourself or whatever.

Neil
  • 448
  • 2
  • 8
  • Its working well some phones but its not working in some vendor phones how can i recognize this in which should i rotate image – Ajay S Nov 22 '12 at 17:05
  • If the vendor does not implement their code correctly I guess there is nothing you can do... – Neil Nov 22 '12 at 17:07
0

What I am doing : first check the orientation of image taken by camera using its meta data information , and If we found this in portrait then we have to rotate the image by 90 and display otherwise only display.

For getting the Information about orientation of image we can use ExifInterface. That's It!

g4gaj
  • 85
  • 2
  • 11
  • Same answer you are repeating here that does not make more sense, even it is better explanation in my answer – Ajay S Feb 21 '14 at 08:51