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

时间:2016-12-01 12:29:20

标签: android android-imageview android-image

目前,我正在开发与处理图片相关的应用程序。我正在修改图像视图,具体取决于图像是纵向还是横向。

我知道我可以通过比较图像的高度和宽度来确定图像是纵向还是横向。

但我面临的问题是一些图像是肖像,但图像的宽度超过高度。以下是这些图片:

image1

image2

上面的图像是纵向的,但如果你计算高度和宽度,你会发现宽度超过高度。

在Android中是否有任何方法可以返回图像是纵向还是横向?

3 个答案:

答案 0 :(得分:0)

此方法将返回Orientation&图像所需的旋转,您应该可以使用它来确定图像是纵向还是横向。

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;
}

答案 1 :(得分:0)

如果我正确理解问题,一种方法是使用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>
                }

答案 2 :(得分:0)

我有同样的问题。我将位图加载到图像视图中,然后从 文件,然后查看宽度是否大于高度。

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));
            }