簡體   English   中英

在 ImageView 中旋轉相機圖像

[英]Rotate camera image in ImageView

我的圖像視圖有問題。 當我在其中添加一張照片的方向(度數)不是0°時,它會自動將方向更改為0°。 這意味着它將例如 90° 照片(垂直)轉換為 0° 照片(水平)。 示例可以在我的屏幕截圖中看到。 我該如何解決?

這是原始照片: 1

這是旋轉的照片: 2

我可以使用這段代碼:

iv_add.setRotation(degree);

但這並不是我真正需要的,因為它旋轉圖像視圖而不是照片。

這是我的代碼:

public String getRealPathFromURI(Uri contentUri) {

    String [] proj={MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery( contentUri,
            proj,
            null,
            null,
            null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();

    return cursor.getString(column_index);
}


public int getCameraPhotoOrientation(Context context, 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;
        }

        Log.i("RotateImage", "Exif orientation: " + orientation);
        Log.i("RotateImage", "Rotate value: " + rotate);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == IMAGE_PICK_COD) {
        Uri imageUri = data.getData();
        try {
            int rotateImage = getCameraPhotoOrientation(this, imageUri, getRealPathFromURI(imageUri));
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
            Matrix matrix = new Matrix();
            matrix.preRotate(rotateImage);
            Bitmap rotBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            iv_add.setImageBitmap(rotBitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

有些照片實際上是完美的,但其中一些消失了,甚至沒有出現在我的圖像視圖中

使用 ExifInterface 獲取方向並使用 Matrix prerotate() 相應地旋轉圖像。 Exif接口矩陣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM