2

我在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) 谢谢

4

2 回答 2

4

使用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);

请给我一些反馈。

希望有帮助。

于 2014-01-29T07:47:36.733 回答
0

我想你可以对高度和宽度做一个简单的测试,比如:

if(imageHeight > imageWidth){
    //Image is horizontal (according to your comments, horizontal photos are usually wider than the hight)
}
elseif(imageHeight > imageWidth){
    //Image is horizontal (again, according to your comments)
}
于 2014-01-29T07:44:28.957 回答