gpt4 book ai didi

安卓 : How to detect the image orientation (portrait or landscape) picked from gallery while setting on an imageview?

转载 作者:太空狗 更新时间:2023-10-29 14:45:48 26 4
gpt4 key购买 nike

我在从图库(相册)中挑选的 ImageView 上设置图像。如果拾取的图像是横向的,它会完美显示,但如果图像处于纵向模式(即在纵向模式下单击图像),它会以 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();

portrait mode landscape mode

最佳答案

使用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 result 方法中并获取旋转图像的值...

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

希望这有帮助..

关于安卓 : How to detect the image orientation (portrait or landscape) picked from gallery while setting on an imageview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40526219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com