2012-08-05 120 views
1

我正在嘗試使用前端攝像頭拍攝沒有用戶界面的應用程序,並且我已經成功地做到了這一點,但唯一的問題是圖片始終以橫向模式拍攝,有什麼辦法強制它到肖像模式?Android中的攝像頭拍攝的旋轉圖片

public class TakePicture extends Activity implements SurfaceHolder.Callback 
{ 
    private ImageView iv_image; 
    private SurfaceView sv; 

    private Bitmap bmp; 

    private SurfaceHolder sHolder; 
    private Camera mCamera; 
    private int cameraId = 1; 
    private Parameters parameters; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     iv_image = (ImageView) findViewById(R.id.imageView); 
     sv = (SurfaceView) findViewById(R.id.surfaceView);   
     sHolder = sv.getHolder(); 

     sHolder.addCallback(this); 

     sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 


    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) 
    { 
parameters = mCamera.getParameters(); 
     mCamera.setParameters(parameters); 
     mCamera.startPreview(); 



     //mCamera.setDisplayOrientation(90); 
     //sets what code should be executed after the picture is taken 
     Camera.PictureCallback mCall = new Camera.PictureCallback() 
     { 
       public void onPictureTaken(byte[] data, Camera camera) { 

        File pictureFileDir = getDir(); 

        if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) { 

         Log.d("DEBUG", "Can't create directory to save image."); 
         Toast.makeText(TakePicture.this, "Can't create directory to save image.", 
           Toast.LENGTH_LONG).show(); 
         return; 

        } 

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss"); 
        String date = dateFormat.format(new Date()); 
        String photoFile = "Picture_" + date + ".jpg"; 

        String filename = pictureFileDir.getPath() + File.separator + photoFile; 

        File pictureFile = new File(filename); 

        try { 
         FileOutputStream fos = new FileOutputStream(pictureFile); 
         fos.write(data); 
         fos.close(); 
         Toast.makeText(TakePicture.this, "New Image saved:" + photoFile, 
           Toast.LENGTH_LONG).show(); 
         finish(); 
        } catch (Exception error) { 
         Log.d("DEBUG", "File" + filename + "not saved: " 
           + error.getMessage()); 
         Toast.makeText(TakePicture.this, "Image could not be saved.", 
           Toast.LENGTH_LONG).show(); 
        } 
       } 
     }; 

     mCamera.takePicture(null, null, mCall); 

    } 


    public void surfaceCreated(SurfaceHolder holder) 
    { 
     // The Surface has been created, acquire the camera and tell it where 
     // to draw the preview. 
     //mCamera = Camera.open(); 
     mCamera = Camera.open(cameraId); 
     try { 
      mCamera.setPreviewDisplay(holder); 

     } catch (IOException exception) { 
      mCamera.release(); 
      mCamera = null; 
     } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) 
    { 
     //stop the preview 
     mCamera.stopPreview(); 
     //release the camera 
     mCamera.release(); 
     //unbind the camera from this object 
     mCamera = null; 
    } 


    private File getDir() { 
     File sdDir = Environment 
      .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
     return new File(sdDir, "Camera!"); 
    } 
} 

回答

0

據我記得,當試圖做到這一點,這是有點棘手。

一種方法,我發現是:

public void surfaceCreated(SurfaceHolder holder) { 
    mCamera = Camera.open(); 

    Parameters params = mCamera.getParameters(); 
    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { 
     params.set("orientation", "portrait"); 


     try { 
      //not sure if this block of code had an OR relationship with the previous line params.set("orientation", "portrait"); 
      Method rotateSet = Camera.Parameters.class.getMethod("setRotation", new Class[] { Integer.TYPE }); 
      Object arguments[] = new Object[] { new Integer(90) }; 
      rotateSet.invoke(params, arguments); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    mCamera.setParameters(params); 

    try { 
     mCamera.setPreviewDisplay(holder); 
    } catch (IOException exception) { 
     mCamera.release(); 
     mCamera = null; 
    } 
} 

但它並沒有爲我工作操作(雖然我使用的Android版本比2.0,其中這應該工作更高)。

我還發現一個黑客使用反射,而工作:

public void surfaceCreated(SurfaceHolder holder) { 
    mCamera = Camera.open(); 

    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {} 
    setDisplayOrientation(mCamera, 90); 

    try { 
     mCamera.setPreviewDisplay(holder); 
    } catch (IOException exception) { 
     mCamera.release(); 
     mCamera = null; 
    } 
} 

其中setDisplayOrientation是:

protected void setDisplayOrientation(Camera camera, int angle){ 
    Method setDisplayOrientationMethod; 
    try { 
     setDisplayOrientationMethod = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class }); 
     if (setDisplayOrientationMethod != null) { 
      setDisplayOrientationMethod.invoke(camera, new Object[] {angle}); 
     } 

    } catch (Exception e1) {} 
} 
相關問題