简体   繁体   中英

Android Camera Orientation ISsue

pictures taken in vertical format are saved in landscape format and vice-versa. I am using Android camera by using this intent

Intent captureImage = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureImage.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);               
startActivityForResult(captureImage, CAMERA_PIC_REQUEST);

onActivityResult() I am just saving image URL to my database and displaying it in a listview. but there orientatiuon changes. The same will happen if I choose image from gallery and save it.

I want the orientation in which photo has been taken. I dont want to change it. Is anybody have a solutin on this.

Some devices doesn't rotate image after it was taken but just write its orientation information into Exif data. So before using taken photo you should call method like :

private int resolveBitmapOrientation(File bitmapFile) throws IOException {
        ExifInterface exif = null;
        exif = new ExifInterface(bitmapFile.getAbsolutePath());

        return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    }

to check its orientation. Then apply:

private Bitmap applyOrientation(Bitmap bitmap, int orientation) {
        int rotate = 0;
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            default:
                return bitmap;
        }

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix mtx = new Matrix();
        mtx.postRotate(rotate);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
    }

and use this new bitmap in your listview. Or it's even better to call this methods just after your photo was taken and override it with new rotated one.

In case if you are receiving Bitmap data as Uri the following method can be used to retrieve its filepath:

public static String getPathFromURI(Context context, Uri contentUri) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT &&
            DocumentsContract.isDocumentUri(context, contentUri)) {
        return getPathForV19AndUp(context, contentUri);
    } else {
        return getPathForPreV19(context, contentUri);
    }
}

private static String getPathForPreV19(Context context, Uri contentUri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null);
    if (cursor != null && cursor.moveToFirst()) {
        try {
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            return cursor.getString(columnIndex);
        } finally {
            cursor.close();
        }
    }

    return null;
}

@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getPathForV19AndUp(Context context, Uri contentUri) {
    String documentId = DocumentsContract.getDocumentId(contentUri);
    String id = documentId.split(":")[1];

    String[] column = { MediaStore.Images.Media.DATA };
    String sel = MediaStore.Images.Media._ID + "=?";
    Cursor cursor = context.getContentResolver().
            query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    column, sel, new String[]{ id }, null);

    if (cursor != null) {
        try {
            int columnIndex = cursor.getColumnIndex(column[0]);
            if (cursor.moveToFirst()) {
                return cursor.getString(columnIndex);
            }
        } finally {
            cursor.close();
        }
    }

    return null;
}

You can also follow by this way:

static Uri image_uri;
static  Bitmap taken_image=null;

                image_uri=fileUri; // file where image has been saved

          taken_image=BitmapFactory.decodeFile(image_uri.getPath());
          try
            {
                ExifInterface exif = new ExifInterface(image_uri.getPath()); 
//Since API Level 5
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);


                switch(orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
                        RotateBitmap(taken_image, 90);
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
                        RotateBitmap(taken_image, 180);

                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
                        RotateBitmap(taken_image, 270);

                        break;
                    case ExifInterface.ORIENTATION_NORMAL:
                        taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
                        RotateBitmap(taken_image, 0);

                        break;
                }

            }
            catch (OutOfMemoryError e)
            {
                Toast.makeText(getActivity(),e+"\"memory exception occured\"",Toast.LENGTH_LONG).show();


            }



public Bitmap RotateBitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);

    round_Image = source;
    round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);


    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM