2014-01-29 25 views
2

我沒有找到我真正需要谷歌和計算器, 我想明白了,一個照片,我剛剛從攝像頭捕捉是垂直或水平, 代碼:如何理解位圖照片是垂直還是水平?

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) 謝謝

+0

然後你必須從位圖中創建另一個位圖,你從相機 –

回答

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

,並請給我一些反饋。

希望taht幫助。

+0

問題是我如何理解垂直或horiontal?因爲我捕獲的圖像持有我的手機垂直或水平位圖說始終相同的值, – user3086226

+0

請chek我更新的答案 –

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) 
} 
+1

imageHeight和imageWidth總是相同的值我的意思是他們是1280x960我的意思是如果我捕捉圖像imageWidth = 1280和重量是960總是 – user3086226