Android Tutorial Camera2 - Still images and videos

Not open for further replies.
Camera2 library is based on the Camera2 API introduced in Android 5 (API 21).
It is designed to work together with CamEx2 class.
It is supported by B4A v7.3+. The library will be included as an internal library in the next update.

Both image capturing and video recording are supported.
CamEx2 class is built to be extended. Camera2 native API is huge.


The configuration steps:

1. Open the camera when the activity is resumed.
2. Prepare the surface and start preview. The preview size and capture size are set at this point.
3. Stop the camera when the activity is paused.

Take pictures with FocusAndTakePicture or TakePictureNow.

Video capturing is done by calling StartVideoRecording and StopVideoRecording.
The surface needs to be recreated after the video is recorded.


When you open the camera you receive a number called TaskIndex:
Wait For (cam.OpenCamera(front)) Complete (TaskIndex As Int)
If TaskIndex > 0 Then
   MyTaskIndex = TaskIndex 'hold this index. It will be required in later calls.
   Wait For(PrepareSurface) Complete (Success As Boolean)
End If
If the value of TaskIndex is 0 then the camera failed to open. Otherwise you need to store it in a global variable and pass it to other camera methods.

The camera can be stopped or reopened while other asynchronous tasks are running. The task index is used to cancel running tasks in such cases.

Notes & tips

- The RECORD_AUDIO is not added automatically as it is only required when recording video. In such case you need to add it with the manifest editor:

CamEx2 class is is included in the attached example.
It depends on Camera2 v1.10+.

Change log

- V1.31 - Changes the behavior of PreviewCropRegion to affect the captured image as well (previously it was inconsistent).
- V1.30 - Fixes an issue with video orientation.
- V1.20
  • Example updated and targetSdkVersion is set to 26. Permissions are handled at runtime.
  • Digital zoom feature. Note that it only affects the preview.
  • Fixed typo in getSupportedVideoSizes method.

New example:
  • Based on B4XPages (there are instructions inside if you want to use it in an Activity module).
  • Preview image is not stretched.
  • Captured image is shown with the correct orientation.


    22.4 KB · Views: 935
Last edited:


Well-Known Member
Licensed User
Longtime User
I can't compile this app.
Camera2 lib not found.
I have updated APi, but still not work.




Active Member
Licensed User
Longtime User
The sample seems to set the video capture size to 1920x1080 (CaptureSize.Initialize(1920, 1080)) but my resulting file is 640x480. How do I set the video capture size? My phone is a Sony Z3 that supports that resolution.

Also, how can I set:
  1. Video bit rate (e.g. 1000kbps or 500kbps)
  2. Video frame rate (e.g. 30fps or 12 fps)


Well-Known Member
Licensed User
Longtime User
Dear friends,

I get this error when I am trying to do picture:
2-(ServiceSpecificException) android.os.ServiceSpecificException: submitRequestList:283: Camera 0: Got error Invalid argument (-22) after trying to submit capture request (code 10)

I have taskindex >0

This I have if I push button for example five times and I get this error. If I push the button for example sixth time I get picure withou error.
Thank you very much


Well-Known Member
Licensed User
Longtime User
I'm getting this error when trying to compile your example:
I've struggled with Manifest changes...etc. but nothing seems to fix this. I'm running version 8.50
Any ideas?


Well-Known Member
Licensed User
Longtime User
I changed it to b4a.cameratest and still get the error.
Any other ideas? BTW, I'm installing it to BlueStacks emulator and have been using it for a variety of apps over the past 4 or 5 years...just FYI

UPDATE: After explaining that I was using BlueStacks, I moved to a real device on which the application works fine... :)
Last edited:


Well-Known Member
Licensed User
Longtime User
There is a Gothcah with Focus, at least on my Insignia tablet:

There are TWO OFFS in the list so it never gets past that because 1 is always returned:p


B4X founder
Staff member
Licensed User
Longtime User
Change the code in CamEx2 to:
'Returns the supported focus modes. Best mode in most cases is CONTINUOUS_PICTURE.
Public Sub getSupportedFocusModes As List
   Return RemoveDuplicates(IntsToConstsList(GetFromCameraCharacteristic("CONTROL_AF_AVAILABLE_MODES"), AF_MODE))
End Sub

Private Sub RemoveDuplicates(Raw As List) As List
   Dim unique As Map
   For Each f As String In Raw
       unique.Put(f, "")
   For Each f As String In unique.Keys
   Return Raw
End Sub


Licensed User
Longtime User

I try CameraEx2 ver 1.20 in my Samsung galaxy s8, but when I swith in camera mode the software stopped with this following error:

Error occurred on line: 156 (CamEx2)
java.lang.IllegalArgumentException: CaptureRequest contains unconfigured Input/Output Surface!
at android.hardware.camera2.CaptureRequest.convertSurfaceToStreamId(
at android.hardware.camera2.impl.CameraDeviceImpl.submitCaptureRequest(
at android.hardware.camera2.impl.CameraDeviceImpl.setRepeatingRequest(
at android.hardware.camera2.impl.CameraCaptureSessionImpl.setRepeatingRequest(
at anywheresoftware.b4a.objects.Camera2.SetRepeatingRequest(
at b4a.example3.camex2._startpreview(
at b4a.example3.main$ResumableSub_PrepareSurface.resume(
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(
at anywheresoftware.b4a.BA.raiseEvent(
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(
at anywheresoftware.b4a.ShellBA.raiseEvent2(
at anywheresoftware.b4a.BA.raiseEvent(
at anywheresoftware.b4a.keywords.Common$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)

any suggestions.

Not open for further replies.