如何在没有预览的情况下从前摄像头捕获图片并将其保存到SD卡。
请帮助我的源代码。

最佳答案

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

07-28 04:39