0

How to capture continuous images using Camera 2 api's. I wrote a simple application and a thread in it to capture continuous images but it is not working

1 Answer 1

3

It would be nice if you provided your code so we could see what's wrong.

In general terms, you'll want to create a CameraDevice object and call the CameraDevice.createCaptureSession(List <Surface>, CameraCaptureSession.StateCallback, Handler) method by specifying which surfaces you might like to output to (maybe just 1). Once the CameraCaptureSession.StateCallback (that you specified in the createCaptureSession method) calls the onConfigured(CameraCaptureSession) method, call the CameraDevice.createCaptureRequest(int) method, which returns a CaptureRequest.Builder object. With this, you can use the CaptureRequest.Builder.addTarget(Surface) method to specify which of the pre-specified surface(s) you want to output to (probably all of them). Once you're done adding targets, call the CaptureRequest.Builder.build() method, which returns a CaptureRequest object. You can then use the CameraCaptureSession object that was provided to you by the onConfigured(CameraCaptureSession) method to finally pass your CaptureRequest object to the CameraCaptureSession.setRepeatingRequest(CaptureRequest, CameraCaptureSession.CaptureCallback, Handler) method. This will start continuous output to the surfaces that you specified.

Seriously, this api is so complicated, you'd think they didn't want you to use it. If you need more detailed information about what these classes and methods do, the Android documentation is very good.

Sign up to request clarification or add additional context in comments.

5 Comments

I am able to capture a single image using Camera2 api which you have shared above but not able to capture continuous images.I put a "For loop" to capture continuous images in my code but it is not working. My app code is bit lengthy unable to share it here. Is there any other way to share it here
You don't want to use a for loop, as this stops Android's back end management code from running (because it gets stuck in your for loop). Notice how I specified the call 'CameraCaptureSession.setRepeatingRequest' instead of 'CameraCaptureSession.capture'. This should continuosly capture and send data to the surface(s) that you specify. As for the code, you could try codeshare.io.
Thanks by adding setreping request , i am able to capture images one after another
Thanks by using setrepeating request able to capture images one by one. Shared .Now my intention is to put it in a loop . eg: Say i press one button and it capture 5 images one after another. Shared the code @ codeshare.io/Exx5y . I have added a thread at the end which should capture 5 images one after another
If the goal is to capture only a few images at a time (rather than to capture a continuous stream of images), then you can use the CameraCaptureSession.captureBurst method instead. I urge you to read through the documentation; often, something you want to implement will be part of the api (such as in this instance) developer.android.com/reference/android/hardware/camera2/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.