[英]How to check whether the image is Portrait or Landscape in Android?
Currently, I am working on app which is related to handle pictures. 目前,我正在开发与处理图片有关的应用程序。 I am modifying the imageview depending on the whether the image is portrait or landscape. 我正在根据图像是纵向还是横向来修改imageview。
I know I can figure out image is portrait or landscape by comparing the height and width of images. 我知道我可以通过比较图像的高度和宽度来确定图像是纵向还是横向。
But the problem I am facing is some images are portrait but the width of image is more than height. 但是我面临的问题是某些图像是纵向的,但是图像的宽度大于高度。 Here are those images: 这些是这些图像:
Above images are portrait but if you calculate height and width you will find that width is more than height. 上面的图像是纵向的,但是如果您计算高度和宽度,您会发现宽度大于高度。
Is there any method in Android which returns whether the image is portrait or landscape? Android中是否有任何方法返回图像是纵向还是横向的?
This method will return the Orientation & rotation required for your image, which you should be able to use to determine whether the image is portrait or landscape. 此方法将返回图像所需的方向和旋转,您应该可以使用该方向来确定图像是纵向还是横向。
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;
}
If I'm understanding question correctly, one approach is to do something like following using Picasso
如果我正确理解问题,一种方法是使用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>
}
I had the same problem. 我有同样的问题。 I load a bitmap into the image view, I read the bitmap from the file, and then see if the width is bigger than the height. 我将位图加载到图像视图中,我从文件中读取了位图,然后查看宽度是否大于高度。
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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.