39

我正在从画廊(相册)挑选的图像视图上设置图像。如果选择的图像是横向的,它会完美显示,但如果图像是纵向模式(即图像是在纵向模式下单击的),它会以 90 度旋转显示图像。现在我试图在设置 imageview 之前找出方向,但所有图像都给出相同的方向和相同的宽度高度。这是我的代码:

Uri selectedImage = intent.getData();
if (selectedImage != null) {
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);

    int str = new ExifInterface(selectedImage.getPath()).getAttributeInt("Orientation", 1000);
    Toast.makeText(this, "value:" + str, Toast.LENGTH_LONG).show();
    Toast.makeText(this, "width:" + bitmap.getWidth() + "height:" + bitmap.getHeight(), Toast.LENGTH_LONG).show();

人像模式 横向模式

4

8 回答 8

59

用于ExifInterface旋转图像。使用此方法获取正确的值以旋转从相机捕获的图像。

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

并将此代码放入 Activity 结果方法中并获取旋转图像的值...

String selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();

int rotateImage = getCameraPhotoOrientation(MyActivity.this, selectedImage, filePath);

希望这可以帮助..

于 2012-10-04T12:13:58.783 回答
6

这也对我有用:

String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = getContentResolver().query(imageUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
    orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
于 2012-10-05T07:27:44.863 回答
3
                      if(bm.getWidth() > bm.getHeight())
                        {
                            Bitmap bMapRotate=null;
                            Matrix mat=new Matrix();
                            mat.postRotate(90);
                        bMapRotate = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), mat, true);
                        bm.recycle();
                        bm=null;
                        imageDisplayView.setImageBitmap(bMapRotate);
                        }else
                        imageDisplayView.setImageBitmap(bm);
于 2014-01-08T10:17:47.883 回答
3

这是我遇到的一个很好的解决方案:> https://stackoverflow.com/a/34241250/8033090

一线解决方案:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

或者

Picasso.with(context).load("file:" + photoPath).into(imageView);

这将自动检测旋转并将图像放置在正确的方向

Picasso 是一个非常强大的库,用于处理应用程序中的图像,包括: 使用最少内存的复杂图像转换。加载可能需要一秒钟,但我只是在图像视图后面放了一些文本,上面写着“正在加载图像”,并且当图像加载时它会覆盖文本。

于 2017-05-28T03:35:58.510 回答
2

使用 kotlin 并考虑新 android 版本中 uri 和 exif 的变化:

 var exif: ExifInterface? = null;
 try {
    when (Build.VERSION.SDK_INT) {
        in Int.MIN_VALUE..24 -> exif = ExifInterface(imageUri.path)
        else -> exif = ExifInterface(getContentResolver().openInputStream(data.extras.get("data")))
     }
 } catch (e: IOException) {
     e.printStackTrace();
 }
 val orientation = exif?.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
 bmp = rotateBitmap(bmp, orientation ?: ExifInterface.ORIENTATION_NORMAL)

而rotateBitmap函数是:

un rotateBitmap(bitmap: Bitmap, orientation: Int): Bitmap {
val matrix = Matrix()
when (orientation) {
    ExifInterface.ORIENTATION_NORMAL -> return bitmap
    ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> matrix.setScale(-1f, 1f)
    ExifInterface.ORIENTATION_ROTATE_180 -> matrix.setRotate(180f)
    ExifInterface.ORIENTATION_FLIP_VERTICAL -> {
        matrix.setRotate(180f)
        matrix.postScale(-1f, 1f)
    }
    ExifInterface.ORIENTATION_TRANSPOSE -> {
        matrix.setRotate(90f)
        matrix.postScale(-1f, 1f)
    }
    ExifInterface.ORIENTATION_ROTATE_90 -> matrix.setRotate(90f)
    ExifInterface.ORIENTATION_TRANSVERSE -> {
        matrix.setRotate(-90f)
        matrix.postScale(-1f, 1f)
    }
    ExifInterface.ORIENTATION_ROTATE_270 -> matrix.setRotate(-90f)
    else -> return bitmap
}
try {
    val bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
    bitmap.recycle()
    return bmRotated
} catch (e: OutOfMemoryError) {
    e.printStackTrace()
    return null
}

}

于 2019-08-07T08:04:35.147 回答
1

在某些安卓设备上使用 EXIF 信息并不可靠。(ExifInterface 方向总是返回 0)

这对我有用:

旋转位图

public static Bitmap rotateBitmap(Context context, Uri photoUri, Bitmap bitmap) 
{
    int orientation = getOrientation(context, photoUri);
    if (orientation <= 0) {
        return bitmap;
    }

    Matrix matrix = new Matrix();
    matrix.postRotate(orientation);
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    return bitmap;
}

从 MediaStore 获取方向

private static int getOrientation(Context context, Uri photoUri) 
{
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, null, null, null);

    if (cursor.getCount() != 1) {
        cursor.close();
        return -1;
    }

    cursor.moveToFirst();
    int orientation = cursor.getInt(0);
    cursor.close();
    cursor = null;
    return orientation;
}
于 2019-10-18T07:04:58.930 回答
0

另一种解决方案是使用支持库中的 ExifInterface: ExifInterface from support library

于 2017-05-10T18:19:22.203 回答
-2

这对我有用:

private String getOrientation(Uri uri){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    String orientation = "landscape";
    try{
        String image = new File(uri.getPath()).getAbsolutePath();
        BitmapFactory.decodeFile(image, options);
        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;
        if (imageHeight > imageWidth){
            orientation = "portrait";
        }
    }catch (Exception e){
        //Do nothing
    }
    return orientation;
}
于 2013-12-22T08:02:05.260 回答