当前,我正在处理与处理图片有关的应用程序。我正在根据图像是肖像还是景观来修改图像视图。
我知道我可以通过比较图像的高度和宽度来弄清楚图像是肖像或景观。
但是我面临的问题是一些图像是肖像,但图像的宽度超出了高度。这是这些图像:
image1
image2
上面的图像是肖像
Android中是否有任何方法返回图像是肖像还是景观?
我也有同样的问题。我将位图加载到图像视图中,我从文件,然后查看宽度是否大于高度。
if(bitmap.getWidth() > bitmap.getHeight()){
//meaning the image is landscape view
view.setLayoutParams(new FrameLayout.LayoutParams(400, ViewPager.LayoutParams.WRAP_CONTENT));
}else{
view.setLayoutParams(new FrameLayout.LayoutParams(250, ViewPager.LayoutParams.WRAP_CONTENT));
}
此方法将返回方向&图像所需的旋转,您应该能够用来确定图像是肖像还是景观。
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;
}
如果我正确理解问题,一种方法是做类似使用Picasso
Picasso.with(this).load(url).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
<compare width/height of bitmap to check aspect ratio of image>
}