0

I am using the camera API and flash mode as torch but after taking one picture the flash is turning off.How can i turn on the flash again.?I am using android 2.3,How can I use flash mode as torch

      public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // <15>
  camera = Camera.open();
  params = camera.getParameters();
  List<Size> sizes = params.getSupportedPictureSizes();
  Camera.Size size = sizes.get(0);
  params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
  params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
       params.setSceneMode(Camera.Parameters.SCENE_MODE_LANDSCAPE);
      params.setFlashMode("torch");
     params.setJpegThumbnailQuality(100);
     params.setExposureCompensation(0);
     params.setJpegQuality(100);
 //  params.setPreviewSize(PreviewSizeWidth,PreviewSizeHeight);
   //now that you have the list of supported sizes, pick one and set it back to   the parameters...
   //int w=0,h=0;
   for(int i=0;i<sizes.size();i++)
   {
       if(sizes.get(i).width > size.width)
           size = sizes.get(i);
   }
   params.setPictureSize(size.width, size.height);
   Toast.makeText(getContext(), size.width+"and"+size.height,Toast.LENGTH_SHORT).show();

  camera.setParameters(params);

  if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
  {
  params.set("orientation", "portrait");
  camera.setDisplayOrientation(90);
  }
  camera.startPreview();
  try
  {
  camera.setPreviewDisplay(holder);
  }
  catch (IOException exception)
  {
  camera.release();
  camera = null;
  }

  }

}

4

3 回答 3

0

检查这个

私人相机相机;

        if (camera == null) {

        } else {
            // Set the torch flash mode
            Parameters param = camera.getParameters();
            param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            try {
                camera.setParameters(param);
                camera.startPreview();

            } catch (Exception e) {

            }
        }
于 2013-06-03T10:12:14.833 回答
0

拍完照片后怎么处理?您是否移动到不同的活动/片段,然后回到拍照片段?您是否只是在不离开当前活动/片段的情况下存储照片?

如果您不离开活动/片段,我建议您在最后一个 Camera.PictureCallback 结束时尝试重新启动手电筒。请记住,拍照后相机预览会停止,因此您必须重新开始预览。也许是这样的:

mPictureCallback = new Camera.PictureCallback(){
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        // ... process your byte data ...

        if(mCamera != null){

            Camera.Parameters params = mCamera.getParameters();
            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            mCamera.setParameters(params);

            try{
                mCamera.startPreview();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
};
于 2013-07-24T19:38:24.980 回答
0

您需要在调用 mCamera.setParameters(params) 之前调用 mCamera.stopPreview();如下所示。

                mCamera.stopPreview();
                mCamera.setParameters(cameraParameters);
                mCamera.startPreview();
于 2018-11-22T09:35:23.913 回答