Android Tutorial Camera2 - Still images and videos

Status
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.

SS-2017-09-14_17.27.38.png


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.

TaskIndex

When you open the camera you receive a number called TaskIndex:
B4X:
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:
B4X:
AddPermission(android.permission.RECORD_AUDIO)

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.
 

Attachments

  • CamEx2.zip
    22.4 KB · Views: 1,274
Last edited:

Lello1964

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.
upload_2017-11-19_21-21-57.png


upload_2017-11-19_21-22-41.png


Tanks
 

biometrics

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)
 

petr4ppc

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
p4ppc
 

Rusty

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

Rusty

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:

MrKim

Well-Known Member
Licensed User
Longtime User
There is a Gothcah with Focus, at least on my Insignia tablet:
upload_2018-10-21_17-34-5.png

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

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change the code in CamEx2 to:
B4X:
'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
   unique.Initialize
   For Each f As String In Raw
       unique.Put(f, "")
   Next
   Raw.Clear
   For Each f As String In unique.Keys
       Raw.Add(f)
   Next
   Return Raw
End Sub
 

mtselection

Member
Licensed User
Longtime User
Hi,

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(CaptureRequest.java:582)
at android.hardware.camera2.impl.CameraDeviceImpl.submitCaptureRequest(CameraDeviceImpl.java:938)
at android.hardware.camera2.impl.CameraDeviceImpl.setRepeatingRequest(CameraDeviceImpl.java:984)
at android.hardware.camera2.impl.CameraCaptureSessionImpl.setRepeatingRequest(CameraCaptureSessionImpl.java:238)
at anywheresoftware.b4a.objects.Camera2.SetRepeatingRequest(Camera2.java:359)
at b4a.example3.camex2._startpreview(camex2.java:217)
at b4a.example3.main$ResumableSub_PrepareSurface.resume(main.java:1029)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:738)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:357)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:245)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1736)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

any suggestions.

thanks
 
Status
Not open for further replies.
Top