旋转从相机或画廊拍摄的图像

时间:2016-10-21 09:02:10

标签: java android image exif android-camera-intent

我从相机捕捉图像并从图库中选择图像。在三星设备中,图像在捕获后会旋转。

如果要旋转图像,我想将图像旋转成直线。

我试图这样做,但它不起作用。尽管图像旋转,但我的EXIF方向始终为0。

private void onCaptureImageResult(Intent data) {

    try {

    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

    Uri tempUri = getImageUri(getApplicationContext(), thumbnail);

    // CALL THIS METHOD TO GET THE ACTUAL PATH
    File finalFile = new File(getRealPathFromURI(tempUri));

        ExifInterface ei = new ExifInterface(String.valueOf(finalFile));
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(finalFile));

        Bitmap rotatedBitmap = null;

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotatedBitmap = rotateImage(bitmap, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotatedBitmap = rotateImage(bitmap, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotatedBitmap = rotateImage(bitmap, 270);
                break;
            case ExifInterface.ORIENTATION_NORMAL:
            default:
                rotatedBitmap = bitmap;
                break;
        }

        if(rotatedBitmap != null)
        {
            profile_image.setImageBitmap(rotatedBitmap);
            thumbnail = rotatedBitmap;
        }
    }
    catch (IOException ex) {     }


  public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}

public static Bitmap rotateImage(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,
            true);
}

这里出了什么问题?请帮忙......谢谢..

2 个答案:

答案 0 :(得分:1)

替换

int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

告诉它是否有效

答案 1 :(得分:0)

在我的项目中,我也遇到过同样的问题,并通过以下代码得到了它:

//imageFileUri is fileUri.getPath() that is being passed
private Bitmap getImageBitmap(String imageFileUri) {

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = 3;
    Bitmap bm = BitmapFactory.decodeFile(imageFileUri, opts);

    ExifInterface exif;
    String orientString = null;
    try {
        exif = new ExifInterface(imageFileUri);
        orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    } catch (IOException e) {
        if (BuildConfig.DEBUG) {
            e.printStackTrace();
        }
    }

    int orientation = (orientString != null) ? Integer.parseInt(orientString)
            : ExifInterface.ORIENTATION_NORMAL;
    int rotationAngle = 0;
    if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
        rotationAngle = 90;
    if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
        rotationAngle = 180;
    if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
        rotationAngle = 270;

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