B4A Class Camera2Torch - Torch using B4A's Camera2 library

Attached is the class Camera2Torch. It uses B4A's Camera2 library to use Android's CameraManager's setTorchMode method. Please note that this method is only available since API23, whereas CameraManager is available as of API21.

This has been successfully tested (via sideloading, targetSdkVersion="30") on the following:
  • Pixel 7 running Android 13
  • Chinese phone running Android 11
  • Chinese phone running Android 6
I'm also attaching a bare-bones test application (a B4XPages skeleton app with minimum add-on code to allow turning the rear camera torch on and off).

Note:

With sideloading the app, no additional Manifest entries seem to be required, nor do I need to ask the user for permission to use the camera. I do not know if this changes for apps on Google's Play Store. Various internet posts on the subject of the usage of setTorchMode have the additional Manifest entries
  • AddPermission("android.permission.FLASHLIGHT")
  • AddManifestText(<uses-feature android:name="android.hardware.camera.flash" />)
but my testing seems to indicate that they are not necessary (and my test app does not use them).

Android Docs link for setTorchMode:
 

Attachments

  • ProjectTorch.2023.01.11.zip
    15 KB · Views: 458
  • Camera2Torch.bas
    2.3 KB · Views: 474

Magma

Expert
Licensed User
Longtime User
Attached is the class Camera2Torch. It uses B4A's Camera2 library to use Android's CameraManager's setTorchMode method. Please note that this method is only available since API23, whereas CameraManager is available as of API21.

This has been successfully tested (via sideloading, targetSdkVersion="30") on the following:
  • Pixel 7 running Android 13
  • Chinese phone running Android 11
  • Chinese phone running Android 6
I'm also attaching a bare-bones test application (a B4XPages skeleton app with minimum add-on code to allow turning the rear camera torch on and off).

Note:

With sideloading the app, no additional Manifest entries seem to be required, nor do I need to ask the user for permission to use the camera. I do not know if this changes for apps on Google's Play Store. Various internet posts on the subject of the usage of setTorchMode have the additional Manifest entries
  • AddPermission("android.permission.FLASHLIGHT")
  • AddManifestText(<uses-feature android:name="android.hardware.camera.flash" />)
but my testing seems to indicate that they are not necessary (and my test app does not use them).

Android Docs link for setTorchMode:
Hi there !

I ve tried to combine it with my project... B4XOCRPage (crop camera preview and specific ocr) + Torch
but somewhere i am loosing it....

I am not sure if my phone not supporting the way of torch or can't combine in this way...

can you check?
attaching project
 

Attachments

  • myscanner2.zip
    33.7 KB · Views: 18

Johan Schoeman

Expert
Licensed User
Longtime User
Hi there !

I ve tried to combine it with my project... B4XOCRPage (crop camera preview and specific ocr) + Torch
but somewhere i am loosing it....

I am not sure if my phone not supporting the way of torch or can't combine in this way...

can you check?
attaching project
I think once the camera is occupied by OCR no other app/library can access the camera/torch until it has been released by the first app/library. They need to reside in the same library when using more than one library to access the torch
 

Magma

Expert
Licensed User
Longtime User
I think once the camera is occupied by OCR no other app/library can access the camera/torch until it has been released by the first app/library. They need to reside in the same library when using more than one library to access the torch
Hi Johan... I am not using OCR with camera... but ocr with images... so i am using camera2 (camex) and combining with torch using the same lib...
 
Top