在android中以编程方式捕获图像

时间:2015-05-14 11:16:17

标签: android android-intent android-activity

我如何使用相机以编程方式捕捉图片,而无需使用蓝牙标志触摸Android中的捕捉按钮。

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

public void takePictureNoPreview(Context context){
    // open back facing camera by default
    Camera myCamera=Camera.open();

    if(myCamera!=null){
        try{
            //set camera parameters if you want to
            //...

            // here, the unused surface view and holder
            SurfaceView dummy=new SurfaceView(context)
            myCamera.setPreviewDisplay(dummy.getHolder());    
            myCamera.startPreview(); 

            myCamera.takePicture(null, null, getJpegCallback()):

        } finally {
            myCamera.close();
        }      

    } else {
        //booo, failed!
    }


  private PictureCallback getJpegCallback(){
      PictureCallback jpeg=new PictureCallback() {   
          @Override
          public void onPictureTaken(byte[] data, Camera camera) {
              FileOutputStream fos;
              try {
                  fos = new FileOutputStream("test.jpeg");
                  fos.write(data);
                  fos.close();
              }  catch (IOException e) {
                 //do something about it
              }
          }
      };
   }
}

或尝试使用此帖子中的一些解决方案:Taking pictures with camera on Android programmatically