如何在Android中同步两个摄像头

时间:2015-10-13 08:37:18

标签: android android-camera service-application

我想同时制作两个最小间隔的照相机(如果可能的话)。 我尝试在服务上制作但同步有很多问题。现在我有两个服务,通过前后摄像头拍照。

现在也许有人,如何在一次服务中使用两台摄像机?

 
public int onStartCommand(Intent intent, int flags, int startId) {

        frontCamera = getCameraInstance(1);

        SurfaceTexture surfaceTexture = new SurfaceTexture(0);

        try {
          frontCamera.setPreviewTexture(surfaceTexture);
          frontParameters = frontCamera.getParameters();

          frontCamera.startPreview();

          frontCamera.takePicture(null, null, frontCameraCall);
          surfaceTexture.release();
        } catch (IOException e) {
          appendToFile(log, "Front camera service preview exception message: " + e + "\n");
          e.printStackTrace();
        }
    return START_REDELIVER_INTENT;
  }
  protected Camera getCameraInstance(int cameraId){

    Camera c = null;
    try {
      do{
        c = Camera.open(cameraId); // atte
mpt to get a Camera instance
      }while (c == null);
    }catch (Exception e){
      // Camera is not available (in use or does not exist)
      Log.e("myLogs", "Camera " + cameraId + " not available! " + e.toString());
    }
    return c; // returns null if camera is unavailable
  }

  Camera.PictureCallback frontCameraCall = new Camera.PictureCallback() {
    public void onPictureTaken(final byte[] data, Camera camera) {
      if (null != camera){
        camera.stopPreview();
        camera.release();
        camera = null;
      }
      FileOutputStream outStream = null;
      try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");

        String fileName = "Front" + currentDate + "-" + countFormatter(counter) + ".jpg";
        File photo = new File(Environment.getExternalStorageDirectory() + "/UAV/front", fileName);
        if (!photo.getParentFile().exists()) {
          photo.getParentFile().mkdirs();
        }
        outStream = new FileOutputStream(photo);
        outStream.write(data);
        outStream.close();

        camkapa(frontCamera);
      } catch (FileNotFoundException e) {
      } catch (IOException e) {
      }
    }
  };

    protected void camkapa(Camera camera) {
    if (null != camera) {
      camera.release();
      camera = null;
    }
  }

第二项服务的相同代码。现在我的代码遇到错误:

 W/CameraBase﹕ An error occurred while connecting to camera: 0
10-13 11:17:59.808    2493-2493/com:backService D/AndroidRuntime﹕ Shutting down VM
10-13 11:17:59.809    2493-2493/com:backService E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com:backService, PID: 2493
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.setPreviewTexture(android.graphics.SurfaceTexture)' on a null object reference

0 个答案:

没有答案