B4J Library WebCam class: how to choose a second camera ?

Hi, All

Thanks to the developer of SarxosWebCam wrapper !
SarxosWebCam lib was updated to "webcam-capture-0.3.12", so i was trying and got success in updating the webcam class - we can choose any camera by the name.

Extra .JARS (sorry, no store for files, they were googled one by one on the net):
#AdditionalJar : webcam-capture-0.3.12
#AdditionalJar : slf4j-api-2.0.0.jar
#AdditionalJar : bridj-0.6.2
#PackagerProperty: AdditionalModuleInfoString = exports org.bridj;
#PackagerProperty: AdditionalModuleInfoString = exports org.bridj.cpp;


B4X:
'v.0.3.12.3 by Peacemaker
'Class module
Private Sub Class_Globals
    Private fx As JFX
    Private WebCam,ImageIO,BufferedImage,FileIO,Dimension As JavaObject
    Private lstWebCams As List
    Type WebCamDeviceDimension (width As Int, height As Int)
    Public CameraIsOpened As Boolean
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    ImageIO.InitializeStatic("javax.imageio.ImageIO")
    WebCam.InitializeStatic("com.github.sarxos.webcam.Webcam")
    BufferedImage.InitializeStatic("java.awt.image.BufferedImage")
End Sub

Public Sub getDefaultCam As JavaObject
    WebCam=WebCam.RunMethod("getDefault",Null)
    Return WebCam
End Sub

Public Sub setDimension(Cam As JavaObject,width As Int,height As Int)
    Cam.RunMethod("setViewSize",Array(Dimension.InitializeNewInstance("java.awt.Dimension",Array(width,height))))
End Sub

Public Sub OpenCam(Cam As JavaObject)
    Cam.RunMethod("open",Null)
    CameraIsOpened = True
End Sub

Public Sub TakePicture(Cam As JavaObject,filename As String)
    FileIO = FileIO.InitializeNewInstance("java.io.File",Array(filename))
    BufferedImage=Cam.RunMethod("getImage",Null)
  
    ImageIO.RunMethod("write",Array(BufferedImage, "PNG", FileIO))
  
    Cam.RunMethod("close",Null)

End Sub

Public Sub TakePicture2(cam As JavaObject) As Image
    Dim fxutils As JavaObject
    fxutils.initializestatic("javafx.embed.swing.SwingFXUtils")
    Dim im As Image = fxutils.runmethod("toFXImage", Array(cam.RunMethod("getImage", Null), Null))
    Return im
End Sub

Public Sub CloseCam(cam As JavaObject)
    cam.RunMethod("close",Null)
    CameraIsOpened = False
End Sub

Sub getWebcams As List
    Return WebCam.RunMethod("getWebcams",Null)
End Sub

Public Sub WebCam_Names() As List
    lstWebCams = getWebcams
    Dim names As List
    names.Initialize
    For i = 0 To lstWebCams.Size - 1
        Dim newcam As JavaObject = lstWebCams.Get(i)
        Dim name As String = getName(newcam)
        names.Add(name)
    Next
    Return names
End Sub
  
Public Sub getWebcamByName(name As String) As JavaObject
    WebCam = WebCam.RunMethodJO("getWebcamByName", Array As String(name))
    Return WebCam
End Sub

Public Sub getName(cam As JavaObject) As String
    Return cam.RunMethod("getName",Null)
End Sub

Private Sub getDevice(cam As JavaObject) As JavaObject
    Return cam.RunMethod("getDevice",Null)
End Sub

Public Sub getViewSize As JavaObject
    Dim device As JavaObject = getDevice(WebCam)
    Return device.RunMethodJO("getResolution",Null)
End Sub

Sub getViewSizes As List
    Dim device As JavaObject = getDevice(WebCam)
    Dim Dimensions() As Object = device.RunMethod("getResolutions",Null)
    Dim L As List
    L.Initialize
    For i = 0 To Dimensions.Length - 1
        Dim d As JavaObject
        d.InitializeNewInstance("java.awt.Dimension", Array(0, 0))
        d = Dimensions(i)
        Dim dd As WebCamDeviceDimension
        dd.Initialize
        dd.width = d.GetField("width")
        dd.height = d.GetField("height")
        L.Add(dd)
    Next
    Return L
End Sub

Public Sub setDimension2(cam As JavaObject, NewDimension As WebCamDeviceDimension)
    setDimension(cam, NewDimension.width, NewDimension.height)
End Sub

'set custom dimension
Public Sub setDimension3(cam As JavaObject, width As Int, height As Int)
    Dim Dims As JavaObject
    Dims.InitializeArray("java.awt.Dimension", Array(Dimension.InitializeNewInstance("java.awt.Dimension",Array(width,height))))
    cam.RunMethod("setCustomViewSizes", Array(Dims))    'add custom resolution into the list
    setDimension(cam, width, height)    'set it
End Sub
 

Attachments

  • Sarxos_v.0.3.12.2.zip
    3.3 KB · Views: 445
  • Sarxos_v.0.3.12.3.zip
    3.6 KB · Views: 140
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
I have added some code to the project in post #1 so that you can scan QR Codes (also some other 1D and 2D codes) with the WebCam.
B4J project is attached
Copy attached javase-2.2.jar to your B4J additional library folder.

Download core-3.2.1.jar from here and copy it to your B4J additional library folder.

Hold the QR Code close enough to the WebCam.

enjoy!

1684651029843.png
 

Attachments

  • Sarxos.zip
    4.3 KB · Views: 310
  • javase-2.2.jar
    38.8 KB · Views: 313
Last edited:

asales

Expert
Licensed User
Longtime User
I have added some code to the project in post #1 so that you can scan QR Codes (also some other 1D and 2D codes) with the WebCam.
B4J project is attached
Copy attached javase-2.2.jar to your B4J additional library folder.

Download core-3.2.1.jar from here and copy it to your B4J additional library folder.

Hold the QR Code close enough to the WebCam.

enjoy!

View attachment 142180
I tried to run this example, but I got this errors:

B4X:
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
main._appstart (java line: 82)
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)

What could be?
 

asales

Expert
Licensed User
Longtime User
I tried to run this example, but I got this errors:

B4X:
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
main._appstart (java line: 82)
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)

What could be?

solved with this line code:
B4X:
#AdditionalJar : slf4j-nop-2.0.3.jar
 

peacemaker

Expert
Licensed User
Longtime User
Class in the 1-st post has been updated, v.0.3.12.3.
Added:

B4X:
'set custom dimension
Public Sub setDimension3(cam As JavaObject, width As Int, height As Int)
    Dim Dims As JavaObject
    Dims.InitializeArray("java.awt.Dimension", Array(Dimension.InitializeNewInstance("java.awt.Dimension",Array(width,height))))
    cam.RunMethod("setCustomViewSizes", Array(Dims))    'add custom resolution into the list
    setDimension(cam, width, height)    'set it
End Sub

Public CameraIsOpened As Boolean

So, trying to set at once the biggest resolution (higher than camera's hardware can), say, 4K (3840, 2160) - will activate the max camera's resolution.
But also it's interesting how to use the more modern camera driver that is able to see all possible camera's resolutions.
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Very nice project.
Have purchased a USB microscope for about EUR 15-00
Amended the code in the main module a bit to fit my initial purpose.
Have added an Event to the Main module:

B4X:
    Dim jo As JavaObject = defCamera
    Log(jo)
    Dim e As Object = jo.CreateEvent("com.github.sarxos.webcam.WebcamListener", "webcam", False)
    jo.RunMethod("addWebcamListener", Array As Object(e))

But this lib unfortunately does not recognize the "snapshot" button that are on the USB camera - no such Event available in the Jar

In the code I take a snapshot via the "Take Snapshot" button of the UI and save it in c:/Images/jhs+timestamp.png - I have added the folder manually and not via code. Change the code if you would like to do it programmatically.

B4X:
"c:/Images/jhs" & DateTime.Now & ".png"

When you change the selection of the Spinner the new camera selected should fire up and the previous one selected will be closed.


microscope.png


1.png


2.png


This is the head of a "Muggie" (very tiny flying insect) under the microscope - see the eyes. The muggie's overall length is only about 1mm. This is a screenshot of it's head. See the EYES....

You need to download the required Jars as too big to attach here:
B4X:
    #AdditionalJar : webcam-capture-0.3.12
    #AdditionalJar : slf4j-api-2.0.3.jar
    #AdditionalJar : bridj-0.7.0

When using Java 14 you can build a standalone package (exe). Make sure your project includes the following to build the standalone package:

B4X:
    #PackagerProperty: AdditionalModuleInfoString = exports org.bridj;
    #PackagerProperty: AdditionalModuleInfoString = exports org.bridj.cpp;
    #PackagerProperty: IncludedModules = javafx.controls
    #PackagerProperty: AdditionalModuleInfoString = uses org.slf4j.spi.SLF4JServiceProvider;

Muggie's Head - see the eyes...
Muggie.png


This is actually what I was after - looking at the cells of an anilox roller:
Anilox.JPG
 

Attachments

  • Sarxos.zip
    7.6 KB · Views: 23
Last edited:
Top