安卓相机方向问题

9

以竖向格式拍摄的照片会以横向格式保存,反之亦然。我正在使用 Android 相机,采用该意图进行操作。

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

onActivityResult() 函数中,我只是将图像URL保存到我的数据库并在列表视图中显示。但是如果方向发生更改,则会出现相同的情况。如果我从图库选择图像并保存它,也会出现这种情况。

我想要拍摄照片时的方向。我不想改变它。有人有解决办法吗?


这个答案可能会对你有所帮助相机方向 - John J Smith
2个回答

17

有些设备在拍摄照片后不会旋转图像,而只是将其方向信息写入Exif数据中。因此,在使用拍摄的照片之前,您应该调用类似于以下方法的方法:

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

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

检查其方向。然后应用:

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);
    }

在你的ListView中使用这个新位图。或者更好的做法是在拍摄照片后立即调用此方法,并将其覆盖为新旋转的照片。

如果你接收到的Bitmap数据是Uri类型,可以使用以下方法获取其文件路径:

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;
}

嗨myx,这个int orientation是什么意思?它的值会是多少?我得到的值是6。这正确吗? - Sumedh Deshpande
1
它是来自android.media.ExifInterface的方向常量值。6 == ORIENTATION_ROTATE_90。我已经编辑了我的答案。 - amukhachov
太棒了!你救了我的下午,小鸟。 - dubmojo
我实际上花了很多时间来解决这个问题,我必须说这个解决方案修复了问题,而且它也是一个快速简便的解决方法。谢谢 Birdy! - Apostrofix

1

您也可以按照以下方式进行关注:

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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接