java - 如何理解位图照片是垂直还是水平?

标签 java android bitmap android-bitmap

我在 google 和 stackoverflow 上没有找到我真正需要的东西, 我想了解我刚刚从相机拍摄的照片是垂直的还是水平的, 代码:

path_img= ("image_url");   
Bitmap  photo = null ;
photo = BitmapFactory.decodeFile(path_img );
int imageHeight = photo.getHeight();
int imageWidth = photo.getWidth();

当我在手机上拍摄照片时,imageHeight 和 imageWidht 始终相同,我的意思是,如果我拍摄垂直或水平照片,它始终为 960x1280,我想要水平照片尺寸为(宽度:960 高度:1280),垂直尺寸为(宽度) :1280 高度:960) 谢谢

最佳答案

使用ExifInterface获得如下方向:

File imageFile = new File(imagePath);

            ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
                            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            boolean isVertical = true ; 

            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_270:
                    isVertical = false ; 
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    isVertical =false ; 
                    break;
            }

依此类推,您可以从 ExifInterface 文档中的常量中获取所需的所有内容。

然后您可以尝试使用 Picasso 执行以下操作API:

int imageWidth , imageHeight = 0 ; 

if(isVertical ) {
imageWidth =1280;
imageHeight=960;
}else if (!isVertical ){
imageWidth =960;
imageHeight=1280;
}

Picasso.with(getActivity()) 
            .load(imageUrl) 
            .placeholder(R.drawable.placeholder) 
            .error(R.drawable.error) 
            .resize(imageWidth , imageHeight)
            .centerInside() 
            .into(imageView);

请给我一些反馈。

希望有帮助。

关于java - 如何理解位图照片是垂直还是水平?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21424846/

相关文章:

android - 在 Android Studio 中自动生成 @String 引用

c# - System.Drawing - 参数无效

c - 位图点处理

java - 通话录音,通话多个(重复)电话阶段并创建多个音频文件

java - 在 RESTful Web 服务中将 URL 作为参数传递

java - ARM 无法正确关闭文件,最终导致打开文件过多

java - Firestore 好友列表未显示正确的方式

android - 未检测到键盘。 MediaQuery.of(context).viewInsets.bottom 始终返回 0.0

android - 如何以组织良好的格式获取地址

c# - 用鼠标绘图会导致像素之间出现间隙