2011-07-25 70 views
13

我在一個應用程序在Android平臺,它使用相機拍攝photos.For啓動相機我使用的是intentACTION_IMAGE_CAPTURE這樣的:組定位入門意圖ACTION_IMAGE_CAPTURE

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     File image=new File(Environment.getExternalStorageDirectory(),"PhotoContest.jpg"); 
     camera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(image)); 
     imageUri=Uri.fromFile(image); 
     startActivityForResult(camera,1); 

public void onActivityResult(int requestCode, int resultCode, Intent data){ 
    super.onActivityResult(requestCode, resultCode, data); 
    switch(requestCode){ 
     case 1: 
      if (resultCode == Activity.RESULT_OK) { 
        selectedImage = imageUri; 
        getContentResolver().notifyChange(selectedImage, null); 
        image= (ImageView) findViewById(R.id.imageview); 
        ContentResolver cr = getContentResolver(); 
        Bitmap bitmap; 
        try { 
         bitmap = android.provider.MediaStore.Images.Media 
         .getBitmap(cr, selectedImage); 
         image.setImageBitmap(bitmap); 
         Toast.makeText(this, selectedImage.toString(), 
           Toast.LENGTH_LONG).show(); 
        } catch (Exception e) { 
         Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT) 
           .show(); 
         Log.e("Camera", e.toString()); 
        } 
       } 
      else 

     if(resultCode == Activity.RESULT_CANCELED) { 
        Toast.makeText(EditPhoto.this, "Picture could not be taken.", Toast.LENGTH_SHORT).show(); 
       } 
     } 
} 

的問題是所拍攝的所有照片都旋轉90度 - 水平對齊。

我也要把它放到我的清單文件:

<activity android:name=".EditPhoto"> 
    android:screenOrientation="portrait" 
    </activity> 

但還是沒有結果那麼任何人都可以幫我嗎?

+1

您發佈的代碼是用於在圖像被捕獲後接收圖像。我不認爲你可以簡單地把肖像或風景。這些更多地涉及視圖的方向。但是攝像頭可以安裝並旋轉,它要求用戶旋轉設備能在看起來「正常」給他們看世界的方式。所以即使你說肖像,也可以用另一種方式投影圖像。在SDK的更高版本中,Camera上有一個setRotation(使用必須修改舊版本的參數)。圖像中可能會有EXIF標題來告訴您方向。 –

+0

那麼解決方案是什麼!? – adrian

+0

沒有您使用的捕獲代碼很難分辨。你可以發佈嗎? –

回答

29

http://developer.android.com/reference/android/media/ExifInterface.html

http://developer.android.com/reference/android/media/ExifInterface.html#TAG_ORIENTATION

因此,如果

Activity.onActivityResult(data, request, result) { 
if (request == PHOTO_REQUEST && result == RESULT_OK) { 
    ... 
    Uri imageUri = ... 
    File imageFile = new File(imageUri.toString()); 
    ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath()); 
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
    int rotate = 0; 
    switch(orientation) { 
    case ExifInterface.ORIENTATION_ROTATE_270: 
     rotate-=90; 
    case ExifInterface.ORIENTATION_ROTATE_180: 
     rotate-=90; 
    case ExifInterface.ORIENTATION_ROTATE_90: 
     rotate-=90; 
    } 
    Canvas canvas = new Canvas(bitmap); 
    canvas.rotate(rotate); 
} 

這是否幫助呢?


只需添加到格雷格的偉大答案,這裏是一個整體的「類別」來完成這項工作:

public static int neededRotation(File ff) 
     { 
     try 
      { 

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

      if (orientation == ExifInterface.ORIENTATION_ROTATE_270) 
       { return 270; } 
      if (orientation == ExifInterface.ORIENTATION_ROTATE_180) 
       { return 180; } 
      if (orientation == ExifInterface.ORIENTATION_ROTATE_90) 
       { return 90; } 
      return 0; 

      } catch (FileNotFoundException e) 
      { 
      e.printStackTrace(); 
      } catch (IOException e) 
      { 
      e.printStackTrace(); 
      } 
     return 0; 
     } 

你或多或少像這樣使用它...

public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
    if (requestCode == REQUEST_IMAGE_CAPTURE) // && resultCode == RESULT_OK) 
     { 
     try 
      { 
      Bitmap cameraBmp = MediaStore.Images.Media.getBitmap(
        State.mainActivity.getContentResolver(), 
        Uri.fromFile(Utils.tempFileForAnImage()) ); 

      cameraBmp = ThumbnailUtils.extractThumbnail(cameraBmp, 320,320); 
      // NOTE incredibly useful trick for cropping/resizing square 
      // http://stackoverflow.com/a/17733530/294884 

      Matrix m = new Matrix(); 
      m.postRotate(Utils.neededRotation(Utils.tempFileForAnImage())); 

      cameraBmp = Bitmap.createBitmap(cameraBmp, 
        0, 0, cameraBmp.getWidth(), cameraBmp.getHeight(), 
        m, true); 

      yourImageView.setImageBitmap(cameraBmp); 

      // to convert to bytes... 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      cameraBmp.compress(Bitmap.CompressFormat.JPEG, 75, baos); 
      //or say cameraBmp.compress(Bitmap.CompressFormat.PNG, 0, baos); 
      imageBytesRESULT = baos.toByteArray(); 

      } catch (FileNotFoundException e) 
      { 
      e.printStackTrace(); 
      } catch (IOException e) 
      { 
      e.printStackTrace(); 
      } 

     return; 
     } 

    } 

希望這樣可以節省有人在未來的一些打字。

+0

我建立了自己的相機,格雷格。謝謝你 – adrian

+0

我不羨慕你。 :)很高興聽到它現在全部暢通無阻。只是想我會張貼這個完成。 –

+0

在Canvas中給予我nullpointer exeption canvas = new Canvas(bitmap); – CoronaPintu

2

以上回答是很透徹,但我發現我不得不做一點點,以便任何情況下工作,特別是如果你使用其他來源的圖像處理,如畫廊或谷歌的照片。這是我的DetermineOrientation方法。我有一個工具類,其中此位於所以我在活動傳遞,以便使用managedQuery(其順便說一句已棄用所以使用小心)。我必須使用兩種方法的原因是,根據圖像的來源,ExifInterface不起作用。例如,如果我拍攝相機照片,Exif可以正常工作。但是,如果我還從畫廊或谷歌驅動器選擇圖像的Exif不工作,總是返回0。希望這可以幫助別人。

public static int DetermineOrientation(Activity activity, Uri fileUri) 
{ 
    int orientation = -1; 
    int rotate = 0; 
    try { 

     ExifInterface exif = new ExifInterface(fileUri.getPath()); 
     orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
     rotate = 0; 
     switch (orientation) { 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       rotate = 270; 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       rotate = 180; 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       rotate = 90; 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    if(rotate == 0) 
    { 
     String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION}; 
     Cursor cur = activity.managedQuery(fileUri, orientationColumn, null, null, null); 
     orientation = -1; 
     if (cur != null && cur.moveToFirst()) { 
      orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0])); 
     } 

     if(orientation != -1) 
     { 
      rotate = orientation; 
     } 
    } 

    return rotate; 
} 
相關問題