3

I want to make both photo cameras with a minimum interval or (if it's possibte) in same time. I try to make it on a service but have a lot problem with synchronize. Now I have two services, which make photo by front and back cameras.

Maybe somebody now, how use two cameras in one service?

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

Same code for second service. And now My code catch error:

 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
Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53
VKostenc
  • 1,140
  • 14
  • 19
  • Some devices [allow two camera instances to be open simultaneously](http://stackoverflow.com/a/28811277/192373). Others, like Samsung S4 and S5, support dual camera through private API, not easily available to 3rd party apps. – Alex Cohn Oct 13 '15 at 09:49
  • Does your code run correctly with one camera? I am afraid it may crash even without the second instance being involved. – Alex Cohn Oct 13 '15 at 09:55
  • If I leave one camera, service work normally. Now I make test, I try to know how long it will run. – VKostenc Oct 13 '15 at 10:17
  • See a recent [discussion](http://stackoverflow.com/questions/32779706/take-a-photo-using-a-service-on-oneplus-one-using-windowmanager-hack) and [sample code](https://github.com/alexcohn/CameraInService) for taking picture from a service. It does not cover dual camera, though. – Alex Cohn Oct 13 '15 at 10:23
  • First time I try create this solution with SurfaceHolder, and my services work are working steadily (on my HTC One M7) with interval between photos in 2 second. But after I start testing on other devices, my service crash because can't connect to camera. – VKostenc Oct 13 '15 at 10:32
  • Your example nice, and gave the stable operation of the service, but the problem remained with synchronization. If I try to do a photo every 3 seconds the camera falls with an error   An error occurred while connecting to camera: – VKostenc Oct 13 '15 at 14:02
  • Quite natural. Camera open-takePicture-onPictureTaken-release may be quite slow, and there is no way you can work faster than this cycle allows – Alex Cohn Oct 13 '15 at 14:05
  • Thanks for Your example! But I steel can't synchronize normaly work of cameras. Maybe somebody now how catch this steps "open-takePicture-onPictureTaken-release"... – VKostenc Oct 14 '15 at 07:28

0 Answers0