我设置从画廊(相册)挑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();
使用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;
}
并把在活动结果的方法,此代码,并获得价值旋转图像...
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);
希望这可以帮助..
这也为我工作:
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = getContentResolver().query(imageUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
if(bm.getWidth() > bm.getHeight())
{
Bitmap bMapRotate=null;
Matrix mat=new Matrix();
mat.postRotate(90);
bMapRotate = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), mat, true);
bm.recycle();
bm=null;
imageDisplayView.setImageBitmap(bMapRotate);
}else
imageDisplayView.setImageBitmap(bm);
下面是我碰到为这个伟大的解决方案:> https://stackoverflow.com/a/34241250/8033090
一号线的解决方案:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
要么
Picasso.with(context).load("file:" + photoPath).into(imageView);
这将自动检测在正确的方向旋转和地方形象
毕加索是在你的应用程序处理的图像包括一个非常强大的库:用最少的内存使用复杂的图像转换。 它可以采取第二个加载,但我只是把上面写着“加载图像”,并在它覆盖文本的图像加载图像查看后面的一些文字。
另一种解决方案是使用从支持库的ExifInterface: 从支持库ExifInterface
这项工作对我来说:
private String getOrientation(Uri uri){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
String orientation = "landscape";
try{
String image = new File(uri.getPath()).getAbsolutePath();
BitmapFactory.decodeFile(image, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
if (imageHeight > imageWidth){
orientation = "portrait";
}
}catch (Exception e){
//Do nothing
}
return orientation;
}
文章来源: Android : How to detect the image orientation (portrait or landscape) picked from gallery while setting on an imageview?