检测从相机拍摄的位图应该是哪个方向

时间:2015-11-06 12:46:08

标签: android bitmap

我有一个应用程序,客户端已要求添加轮换支持。 以下是从相机拍摄图像的当前代码。

camera.takePicture(null, null, new PictureCallback() {
                    public void onPictureTaken(byte[] data, Camera camera) {

                        if (data != null) {

                            Matrix mtx = new Matrix();
                            mtx.postRotate(180);

                            float density           = Resources.getSystem().getDisplayMetrics().density;

                            Bitmap bm = BitmapFactory.decodeByteArray(data, 0, (data != null) ? data.length : 0);
                            Bitmap scaled = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), mtx, true);
                            bm = Bitmap.createScaledBitmap(scaled, (int)(480*density), (int)(320*density), true);       //height and width are backwards because its portrait
                            previewImage(bm);
                        }
                    }


                });

我想要解决的是找出捕获的位图应该处于什么方向的最快方法,即当用户在景观中拍摄照片时我们需要翻转:

Bitmap.createScaledBitmap(scaled, (int)(480*density), (int)(320*density), true);

为:

Bitmap.createScaledBitmap(scaled, (int)(320*density), (int)(480*density), true);

并更改mtx.postRotate

我的问题是找出最佳方法来找出拍摄图像的轮换次数。

1 个答案:

答案 0 :(得分:0)

试试这个

private Bitmap checkForRotation(String filename, Bitmap bitmap) {
    Bitmap myBitmap = bitmap;
    ExifInterface ei = null;
    try {
        ei = new ExifInterface(filename);
        new ExifInterface(filename);
    } catch (IOException e) {
        e.printStackTrace();
    }
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

    switch (orientation) {

     //Here you get the orientation and do what ever you want to do with it as i am rotating the image

    case ExifInterface.ORIENTATION_ROTATE_90:
        myBitmap = rotateImage(bitmap, 90);

        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        myBitmap = rotateImage(bitmap, 180);
        break;
    }
    return myBitmap;
}