安卓摄像机不起作用

时间:2014-03-05 15:34:37

标签: android android-camera

我的Android相机在真实设备上测试时无法拍摄照片并使用此类代码触发闪光灯,不会发生错误。原因何在?

 public class CameraShooting implements SurfaceHolder.Callback {

         Context context;
         Camera camera;
         CameraInfo camerainfo;
         Camera.Parameters paras;
         SurfaceView surfaceview;
         SurfaceHolder surfaceholder;
         PictureCallback picturecallback;

        public CameraShooting (Context context){

           this.context = context;
           }
       public void cameraStart(){
    if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
        surfaceview = new SurfaceView(context);
        surfaceholder = surfaceview.getHolder();
        surfaceholder.addCallback(this);

    };      

};

        @Override
         public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

}
         @Override
        public void surfaceCreated(SurfaceHolder holder) {

    int numbersofCamera = camera.getNumberOfCameras();
    int cameraID = 0;
    camerainfo = new CameraInfo();
    for(int i =1; i < numbersofCamera; i++){
        camera.getCameraInfo(i, camerainfo);
            if (camerainfo.facing == CameraInfo.CAMERA_FACING_FRONT){
                cameraID = i;
                camera = Camera.open(i);
                break;
                }
                if(camerainfo.canDisableShutterSound){

                camera.enableShutterSound(true);
                }
                paras = camera.getParameters();
                paras.setFlashMode(Camera.Parameters.FLASH_MODE_ON);    

                try {
                    camera.setPreviewDisplay(surfaceholder);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                camera.setParameters(paras);
                camera.startPreview();

                camera.takePicture(null, null, mPicture);
                Toast.makeText(context, "Picture taken",                     Toast.LENGTH_SHORT).show();
    };
    }
@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
         private PictureCallback mPicture = new PictureCallback() {
             @Override
          public void onPictureTaken(byte[] data, Camera camera) {

    File pictureFile = new File(context.getFilesDir(), "picture taken");
    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        fos.write(data);
        fos.close();
    } catch (FileNotFoundException e) {

    } catch (IOException e) {
        }
   };
  };}

非常感谢您的帮助! 非常感谢。

1 个答案:

答案 0 :(得分:1)

假设它是带有闪光灯的后置摄像头,而不是前置摄像头。然后有类似

的东西
for(int i =1; i < numbersofCamera; i++){
        camera.getCameraInfo(i, camerainfo);
            if (camerainfo.facing == CameraInfo.CAMERA_FACING_FRONT){
               continue; //NOT break;
               cameraID = i;
                camera = Camera.open(i);
            }
    // ... etc, etc

     }