2012-10-04 20 views
26

我正在從圖庫(相冊)中選取的圖像視圖上設置圖像。如果選取的圖像具有橫向方向,則可以完美顯示,但如果圖像處於縱向模式(即以縱向模式點擊該圖像),它將以90度旋轉顯示圖像。現在我正試圖在設置imageview之前找出方向,但所有圖像都具有相同的方向和相同的寬度高度。這裏是我的代碼:Android:如何檢測在圖像視圖上設置時從圖庫中選取的圖像方向(縱向或橫向)?

Uri selectedImage = intent.getData(); 
if (selectedImage != null) { 
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage); 

    int str = new ExifInterface(selectedImage.getPath()).getAttributeInt("Orientation", 1000); 
    Toast.makeText(this, "value:" + str, Toast.LENGTH_LONG).show(); 
    Toast.makeText(this, "width:" + bitmap.getWidth() + "height:" + bitmap.getHeight(), Toast.LENGTH_LONG).show(); 

portrait mode landscape mode

+0

誰能幫助我,我有相同的issue..http://stackoverflow.com/questions/28379130/how-to-set-camera-image-orientation – 2015-02-07 12:02:22

回答

47

使用ExifInterface用於旋轉圖像。使用此方法可以獲得正確的值以便從相機旋轉捕獲的圖像。

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

,放在活動結果的方法,此代碼,並獲得價值旋轉圖像...

String selectedImage = data.getData(); 
String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
cursor.moveToFirst(); 

int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
filePath = cursor.getString(columnIndex); 
cursor.close(); 

int rotateImage = getCameraPhotoOrientation(MyActivity.this, selectedImage, filePath); 

希望這有助於..

+2

exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);對於任何方向圖像總是返回值0。 – sarabhai05

+0

對不起,我犯了一個錯誤。你的方法正在爲我工​​作。此外data.getData()返回Uri不是字符串,請看看我們的代碼。 – sarabhai05

+6

@Deepak爲什麼我必須調用'context.getContentResolver()。notifyChange(imageUri,null)'? –

4

這也爲我工作:

String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION}; 
Cursor cur = getContentResolver().query(imageUri, orientationColumn, null, null, null); 
int orientation = -1; 
if (cur != null && cur.moveToFirst()) { 
    orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0])); 
} 
Matrix matrix = new Matrix(); 
matrix.postRotate(orientation); 
+0

可以添加完整的代碼嗎?thanx –

+0

你想要什麼部分代碼? – sarabhai05

-1

這份工作適合我:

private String getOrientation(Uri uri){ 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    String orientation = "landscape"; 
    try{ 
     String image = new File(uri.getPath()).getAbsolutePath(); 
     BitmapFactory.decodeFile(image, options); 
     int imageHeight = options.outHeight; 
     int imageWidth = options.outWidth; 
     if (imageHeight > imageWidth){ 
      orientation = "portrait"; 
     } 
    }catch (Exception e){ 
     //Do nothing 
    } 
    return orientation; 
} 
+3

在某些設備上(如s3和s4),即使圖像是縱向,寬度也會大於高度。那就是爲什麼我們不使用這個解決方案,即使它看起來乾淨和完美。 –

3
     if(bm.getWidth() > bm.getHeight()) 
         { 
          Bitmap bMapRotate=null; 
          Matrix mat=new Matrix(); 
          mat.postRotate(90); 
         bMapRotate = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), mat, true); 
         bm.recycle(); 
         bm=null; 
         imageDisplayView.setImageBitmap(bMapRotate); 
         }else 
         imageDisplayView.setImageBitmap(bm); 
+1

在某些設備(如s3和s4)上,即使圖像是縱向,寬度也比高度大。那就是爲什麼我們不使用這個解決方案,即使它看起來乾淨和完美。 –

+0

@JesusDimrix是的,它可能是一個案例。但是,如果圖像的標題中沒有方向信息,那麼我們可以使用這種方式。 – ultimate

+0

我瘋了試圖讓這工作你的方式對我來說足夠好galaxy s是什麼? –

1

這裏是一個很好的解決方案,我碰到了這一點:>https://stackoverflow.com/a/34241250/8033090

一號線的解決方案:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 

Picasso.with(context).load("file:" + photoPath).into(imageView); 
用最少的內存使用複雜的圖像轉換:

這將在正確的方向

畢加索是在你的應用程序處理圖像非常強大的庫包括自動檢測旋轉和地方的形象。它可能需要一秒鐘才能加載,但我只是在圖像視圖後面放置了一些說「加載圖像」的文本,以及圖像加載時是否覆蓋了文本。

相關問題