使用CameraCaptureSession的Android Capture图片将图片旋转为横向

时间:2015-11-05 09:58:02

标签: java android mobile

在某些设备上,CaptureSession拍摄的照片正在转向风景图。

我的问题是我会检测图片是否以横向保存并将其旋转并保存。因为现在结果是用户为某些模型旋转了轮廓图像。

现在我尝试了很多解决方案,包括

getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

总是返回0。

我在想是否因为某些原因没有设置JPEG文件中的Exif标记,但我没有找到任何关于它的内容。

到目前为止,我的解决方案是最好的解决方案。这只是检查高度和宽度。这总是不起作用。很多设备都有旋转图片的问题。

public static Bitmap rotate90ifneeded(Bitmap bitmap, int width, int height) {
    Bitmap bitmap2 = bitmap;
    if(bitmap.getHeight() < bitmap.getWidth()) {
        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        bitmap2 = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
    return bitmap2;
}

1 个答案:

答案 0 :(得分:0)

使用此方法确定图像旋转。

public int getCameraPhotoOrientation(Uri imageUri, String imagePath) {
        int rotate = 0;
        try {
            _context.getContentResolver().notifyChange(imageUri, null);
            File imageFile = new File(imagePath);
            ExifInterface exif = new ExifInterface(
                    imageFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

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


        } catch (Exception e) {
        }
        return rotate;
    }

如果方法不返回零,则旋转图像。

int degree=getCameraPhotoOrientation(Uri.fromFile(tempFile), fPath);
if (degree!= 0) {
      bitmap = tools.rotateOrientationCall(bitmap, degree);
 }

旋转位图。

public Bitmap rotateOrientationCall(Bitmap src, float degree) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
    }