2014-02-14 49 views
2

照片成功地用相機拍攝,但在三星Galaxy S3的肖像模式下,圖片旋轉。我該如何解決這個問題。 相機意圖如下:以相機意圖旋轉照片在人像模式下旋轉android

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(xdestination)); 
     startActivityForResult(intent, CAMERA_PIC_REQUEST); 

在活動結果

if (requestCode==CAMERA_PIC_REQUEST){ 

      // Bitmap bm = (Bitmap) data.getExtras().get("data"); 

       Uri photo_uri = Uri.fromFile(xdestination); 

       Editer.PHOTO_FROM=11; 

       Bitmap decodeSampledBitmapFromFile=null; 
       try { 
        decodeSampledBitmapFromFile = decodeUri(photo_uri); 
       } catch (FileNotFoundException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 

       ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
       decodeSampledBitmapFromFile.compress(Bitmap.CompressFormat.JPEG,100, bytes); 

       File f = new File(Environment.getExternalStorageDirectory(), "user_image.jpg"); 

       try { 

        if(f.exists()) 
         f.delete(); 

        f.createNewFile(); 

        FileOutputStream fo = new FileOutputStream(f); 
        fo.write(bytes.toByteArray()); 
        fo.close(); 

       } catch (IOException e) { 

        e.printStackTrace(); 
        Log.d("ERROR", e.toString()); 
       } 

      } 
+0

我也有一個相同的問題,我捕捉它後旋轉圖片。 – InnocentKiller

+0

你能告訴我你是如何旋轉圖片的嗎?發佈你的代碼如果可能或部分 – user3115198

+0

嘗試我的下面的代碼,讓我知道它是否適合你。 – InnocentKiller

回答

0

嘗試它像下面。

File photo = new File(Environment.getExternalStorageDirectory(), "user_image.jpg"); 
     if (photo.exists()) { 
      Bitmap myBitmap = BitmapFactory.decodeFile(photo 
        .getAbsolutePath()); 
      imageView.setImageBitmap(myBitmap); 
      imageView.setRotation(90); 
     } 

第1步:給你的照片的完整路徑類似File photo = new File(Environment.getExternalStorageDirectory() , "Face.jpg");

第2步:查看圖片是否存在與否,如果是,那麼將它設置爲圖像視圖,並將其旋轉到90度。

0

用途:

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

通過你拍攝的照片和圖片變成下面的方法,這將返回正確的導向畫面的SD卡路徑...

private Bitmap imageOreintationValidator(Bitmap bitmap, String path) { 

    ExifInterface ei; 
    try { 
     ei = new ExifInterface(path); 
     int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 
     switch (orientation) { 
     case ExifInterface.ORIENTATION_ROTATE_90: 
      bitmap = rotateImage(bitmap, 90); 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_180: 
      bitmap = rotateImage(bitmap, 180); 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_270: 
      bitmap = rotateImage(bitmap, 270); 
      break; 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return bitmap; 
} 

private Bitmap rotateImage(Bitmap source, float angle) { 

    Bitmap bitmap = null; 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 
    try { 
     bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), 
       matrix, true); 
    } catch (OutOfMemoryError err) { 
     err.printStackTrace(); 
    } 
    return bitmap; 
} 
+0

它來自我的代碼嗎? –

+0

對不起,我確實\t decodeSampledBitmapFromFile = imageOreintationValidator(decodeSampledBitmapFromFile,f.getPath()); \t \t \t \t decodeSampledBitmapFromFile.compress(Bitmap.CompressFormat.JPEG,100,bytes);但沒有效果圖像仍然在景觀 – user3115198

+0

感謝它解決我的我的更大的問題:) – SAndroidD