gpt4 book ai didi

Android从图库中获取图像会旋转

转载 作者:IT王子 更新时间:2023-10-28 23:42:20 26 4
gpt4 key购买 nike

我试图让用户从图库中选择个人资料图片。我的问题是有些图片向右旋转。

我像这样启动图像选择器:

Intent photoPickerIntent = new Intent();
photoPickerIntent.setType("image/*");
photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(photoPickerIntent, "Select profile picture"), Global.CODE_SELECT_PICTURE);

我像这样从 onActivityResult 获取图像:

Uri selectedPicture = data.getData();
profilePic = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), selectedPicture);

我怎样才能让图像不被旋转?

更新:

根据我收到的一些有用的答案,我设法提出了以下工作解决方案(这只是一个工作代码,写得不好)。我很想得到您关于如何改进它的反馈!

public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == Activity.RESULT_OK && requestCode == Global.CODE_SELECT_PICTURE) {

// Get selected gallery image
Uri selectedPicture = data.getData();
// Get and resize profile image
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = activity.getContentResolver().query(selectedPicture, filePathColumn, null, null, null);
cursor.moveToFirst();

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

Bitmap loadedBitmap = BitmapFactory.decodeFile(picturePath);

ExifInterface exif = null;
try {
File pictureFile = new File(picturePath);
exif = new ExifInterface(pictureFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}

int orientation = ExifInterface.ORIENTATION_NORMAL;

if (exif != null)
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
loadedBitmap = rotateBitmap(loadedBitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
loadedBitmap = rotateBitmap(loadedBitmap, 180);
break;

case ExifInterface.ORIENTATION_ROTATE_270:
loadedBitmap = rotateBitmap(loadedBitmap, 270);
break;
}
}
}

public static Bitmap rotateBitmap(Bitmap bitmap, int degrees) {
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

最佳答案

您可以使用 ExifInterface修改方向:

public static Bitmap modifyOrientation(Bitmap bitmap, String image_absolute_path) throws IOException {
ExifInterface ei = new ExifInterface(image_absolute_path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotate(bitmap, 90);

case ExifInterface.ORIENTATION_ROTATE_180:
return rotate(bitmap, 180);

case ExifInterface.ORIENTATION_ROTATE_270:
return rotate(bitmap, 270);

case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
return flip(bitmap, true, false);

case ExifInterface.ORIENTATION_FLIP_VERTICAL:
return flip(bitmap, false, true);

default:
return bitmap;
}
}

public static Bitmap rotate(Bitmap bitmap, float degrees) {
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

public static Bitmap flip(Bitmap bitmap, boolean horizontal, boolean vertical) {
Matrix matrix = new Matrix();
matrix.preScale(horizontal ? -1 : 1, vertical ? -1 : 1);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

要从 uri 中获取图像的绝对路径,请检查 this answer

关于Android从图库中获取图像会旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31925712/

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