2015-01-12 38 views
1

我準備的視圖(使用setDisplayOrientation())處於肖像模式,但生成的圖像(使用takePicture()拍攝,使用SaveImageTask保存)處於橫向模式。如何旋轉使用takePicture拍攝的圖像?

這是我的預覽&捕捉代碼:

public void startPreview() { 
    this.mCamera.setDisplayOrientation(90); 
    try { 
     this.mCamera.setPreviewDisplay(this.mSurfaceHolder); 
     this.mCamera.startPreview(); 
    } catch (Exception e) { 
     Log.d("camera" , "in startPreview Catch"); 
    } 
} 

public void captureNow(View view) { 
    this.mCamera.takePicture(shutterCallback, rawCallback, jpegCallback); 
    this.startPreview(); 
} 

這是我拍攝的圖像保存代碼:

public class SaveImageTask extends AsyncTask<byte[], Void, Void> { 
    @Override 
    protected Void doInBackground(byte[]... data) { 
     FileOutputStream outStream = null; 
     // Write to SD Card 
     try { 
      File dir = new File(
      Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES), "camTest"); 
      dir.mkdirs(); 
      String fileName = String.format("%d.jpg", System.currentTimeMillis()); 
      File outFile = new File(dir, fileName); 
      outStream = new FileOutputStream(outFile); 
      outStream.write(data[0]); 
      outStream.flush(); 
      outStream.close(); 

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

我如何可以旋轉拍攝的圖像?似乎傳感器始終處於橫向模式。(注:API 21)

回答

0

我張貼我的代碼某些部分,可能是它會幫助你的感謝

 File f = new File(path + "image" + imagecount + ".jpg"); 
     ExifInterface exif = new ExifInterface(f.getPath()); 
     int orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 

     int angle = 0; 

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

     Matrix mat = new Matrix(); 
     mat.postRotate(angle); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 3; 

     Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), 
       null, options); 
     correctBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), 
       bmp.getHeight(), mat, true); 
     ByteArrayOutputStream outstudentstreamOutputStream =       new ByteArrayOutputStream(); 
     correctBmp.compress(Bitmap.CompressFormat.PNG, 100, 
       outstudentstreamOutputStream); 
     capturedImageIV.setImageBitmap(correctBmp); 
    } catch (IOException e) { 
     Log.w("TAG", "-- Error in setting image"); 
    } catch (OutOfMemoryError oom) { 
     Log.w("TAG", "-- OOM Error in setting image"); 
    } 
0

如果你想帶你必須配置圖片你之前改變取向首先攝像頭。

public void onOrientationChanged(int orientation) { 
    if (orientation == ORIENTATION_UNKNOWN) return; 
    android.hardware.Camera.CameraInfo info = 
     new android.hardware.Camera.CameraInfo(); 
    android.hardware.Camera.getCameraInfo(cameraId, info); 
    orientation = (orientation + 45)/90 * 90; 
    int rotation = 0; 
    if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 
    rotation = (info.orientation - orientation + 360) % 360; 
    } else { // back-facing camera 
    rotation = (info.orientation + orientation) % 360; 
    } 
    mParameters.setRotation(rotation); 
} 

AFAIK,在寫入光盤之前,您無法更改EXIF數據。所以,如果你想稍後改變方向,那麼你可以通過阿倫安東尼的方式做到這一點。