我实现了捕获/选择图像的功能,它在HTC上运行良好,但是,在三星Galaxy Note 4(Android版本为5.1.1)上,它将图像旋转了90度。 以下是2种代码变体,但仍然可以旋转:
VARIANT 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();
}
}
}
VARIANT 2:
使用PhotoPicker lib 编译'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 :(得分:1)
如果您的第一个变体中的方向总是为0,则可以尝试以下方法。 (来自this帖子)
尝试使用内容光标中的信息。
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;
}
}
}
答案 1 :(得分:0)
在某些设备中,samsumg设备图像中的最大值会旋转90度。 为此你需要在exif文件中检查它的确切方向,并根据它你必须工作。
int rotateDegree = 0;
try {
File imageFile = new File(sourcepath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotateDegree = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotateDegree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotateDegree = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.postRotate(rotateDegree );
bitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);