gpt4 book ai didi

java - 捕获或从图像库中选取的图像将照片旋转 90 度

转载 作者:行者123 更新时间:2023-12-01 08:54:42 31 4
gpt4 key购买 nike

我实现了捕获/选择图像的功能,它在 HTC 上运行得很好,但是,在三星 Galaxy Note 4(Android 版本为 5.1.1)上它会将图像旋转 90 度。以下是代码的 2 个变体,但仍进行了旋转:

变体 1:

public void captureImageCameraOrGallery() {

Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT, null);
galleryintent.setType("image/*");

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, galleryintent);
chooser.putExtra(Intent.EXTRA_TITLE, "Select from:");

Intent[] intentArray = { cameraIntent };
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooser, REQUEST_PIC);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == REQUEST_PIC && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
Bitmap bmp = null;
try {
if (selectedImageUri != null) {
bmp = getBitmapFromUri(selectedImageUri);
}

if (bmp == null) {
return;
}

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

Log.e("Capture orientation: ", String.valueOf(orientation));
int rotateAngle = 0;
switch(orientation) {

case ExifInterface.ORIENTATION_ROTATE_90:
rotateAngle = 90;
break;

case ExifInterface.ORIENTATION_ROTATE_180:
rotateAngle = 180;
break;

case ExifInterface.ORIENTATION_ROTATE_270:
rotateAngle = 270;
break;


default:
break;
}

bmp = rotateImage(bmp, rotateAngle);
mUserImage.setImageBitmap(bmp);

} catch (IOException e) {
e.printStackTrace();
}
}
}

变体 2:

使用PhotoPicker库编译 'me.iwf.photopicker:PhotoPicker:0.9.5@aar'

public void captureImageCameraOrGallery() {

PhotoPicker.builder()
.setPhotoCount(1)
.setShowCamera(true)
.setShowGif(true)
.setPreviewEnabled(false)
.start(this, PhotoPicker.REQUEST_CODE);
}

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
if (data != null) {
ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
Uri selectedImageUri = Uri.fromFile(new File(photos.get(0)));

Bitmap bmp = null;
try {
if (selectedImageUri != null) {
bmp = getBitmapFromUri(selectedImageUri);
}

if (bmp == null) {
return;
}
mUserImage.setImageBitmap(bmp);


} catch (IOException e) {
e.printStackTrace();
}
}
}
}

但是,它仍在旋转。任何帮助将不胜感激。

最佳答案

如果在第一个变体中您的方向始终为 0,您可以尝试以下操作。 (来自this帖子)

Try to use the information in the content cursor.

float photoRotation = 0;
boolean hasRotation = false;
String[] projection = { Images.ImageColumns.ORIENTATION };
try {
Cursor cursor = getActivity().getContentResolver().query(photoUri, projection, null, null, null);
if (cursor.moveToFirst()) {
photoRotation = cursor.getInt(0);
hasRotation = true;
}
cursor.close();
} catch (Exception e) {}

if (!hasRotation) {
ExifInterface exif = new ExifInterface(photoUri.getPath());
int exifRotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);

switch (exifRotation) {
case ExifInterface.ORIENTATION_ROTATE_90: {
photoRotation = 90.0f;
break;
}
case ExifInterface.ORIENTATION_ROTATE_180: {
photoRotation = 180.0f;
break;
}
case ExifInterface.ORIENTATION_ROTATE_270: {
photoRotation = 270.0f;
break;
}
}
}

关于java - 捕获或从图像库中选取的图像将照片旋转 90 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42120207/

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