简体   繁体   English

如何在Android中检查图像是纵向还是横向?

[英]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: 这些是这些图像:

image1 图片1

image2 image2

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.

相关问题 如何检查是否使用Android相机以纵向或横向拍摄图像? - How to check whether an image is captured in portrait mode or landscape mode using camera in android? 如何检查照片是在Android的横向还是纵向模式下拍摄的? - How to check whether the photo was taken in landscape or portrait mode in Android? 如何知道图像的方向(纵向还是横向)? - How to know the orientation of image, whether portrait or landscape? 如何在Android OpenCV 3中将风景图像旋转为人像 - How rotate a Landscape Image to Portrait in Android OpenCV 3 如何检测Android设备是纵向还是横向锁定 - How to detect whether Android device is locked in portrait or landscape orientation 如何识别屏幕是在Kivy中是纵向还是横向 - How to identify whether the screen is in Portrait or Landscape in Kivy Android:如何处理Portrait to Reverse Portrait和Landscape to Reverse Landscape的事件 - Android: How to handle the event of Portrait to Reverse Portrait and Landscape to Reverse Landscape Android Camera将图像返回为风景,将其作为人像吗? - Android Camera returns image as landscape, getting it as Portrait? Android Camera将图像返回为人像,使其变为风景? - Android Camera returns image as Portrait, getting it as landscape? 如何在Android中使用横向和纵向连续处理音频? - How to handle Audio continously with Landscape and portrait in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM