Java 从imagelibrary旋转90度拍摄或拾取的图像
我实现了捕获/选择图像的功能,它在HTC上工作得很好,但是在三星Galaxy Note 4的Android版本是5.1.1,它将图像旋转90度。 以下是两种仍在旋转的代码变体: 备选案文1:Java 从imagelibrary旋转90度拍摄或拾取的图像,java,android,image-capture,imagelibrary,Java,Android,Image Capture,Imagelibrary,我实现了捕获/选择图像的功能,它在HTC上工作得很好,但是在三星Galaxy Note 4的Android版本是5.1.1,它将图像旋转90度。 以下是两种仍在旋转的代码变体: 备选案文1: public void captureImageCameraOrGallery() { Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT, null); galleryintent.setType("im
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:
使用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();
}
}
}
}
然而,它仍然在旋转。任何帮助都将不胜感激。在某些设备中,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);
如果在第一个变体中,方向始终为0,则可以尝试以下方法。邮寄 尝试使用内容光标中的信息
对不起,有点和国旗混在一起了!哈斯旋转。我们是否进行旋转,以防发生以下情况!hasRotation{//rotate}否则{//shoulld在这里不旋转地加载图像?}有一个双重检查-首先我们检查内容是否使用光标旋转,如果是,那么我们设置一个标志,不检查exif信息,如果不是,那么检查exif信息并从那里获取旋转数据。好的,收到了,谢谢!还有一个问题,我应该这样设置smth:intent.putExtraMediaStore.EXTRA_OUTPUT,mCapturedImageURI;何时调用new Intent并将mCapturedImageURI用作全局变量?MediaStore.EXTRA_输出是否有很大影响?或者我可以只得到uriphotouri=data.getData;在ONACTIVITY中直接得到结果?谢谢您的回答。然而,旋转仍然发生。无论如何,谢谢。
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;
}
}
}