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.jar
#AdditionalJar : slf4j-api-2.0.0.jar
#AdditionalJar : bridj-0.6.2.jar
#AdditionalJar : slf4j-nop-2.0.3.jar


B4X:
'v.0.3.12.2
'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)
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)
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)
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

Class was updated.
 

Attachments

  • Sarxos_v.0.3.12.2.zip
    3.3 KB · Views: 185
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Please, try on your PCs and report if the sample is working. On my one old laptop (2GB RAM) - it's error in "getWebcams"
 

peacemaker

Expert
Licensed User
Longtime User
And if to use "Build Standalone Package" - also crash at camera start. No idea why...

Oct 07, 2022 5:56:35 PM com.github.sarxos.webcam.Webcam getDriver
INFO: WebcamDefaultDriver capture driver will be used
sarxoswebcam._getwebcams (java line: -1)
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at b4j/anywheresoftware.b4j.object.JavaObject.RunMethod(Unknown Source)
at b4j/peacemaker.orionlab.sarxoswebcam._getwebcams(Unknown Source)
at b4j/peacemaker.orionlab.sarxoswebcam._webcam_names(Unknown Source)
at b4j/peacemaker.orionlab.b4xmainpage._lblcamera_mouseclicked(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at b4j/anywheresoftware.b4a.BA.raiseEvent2(Unknown Source)
at b4j/anywheresoftware.b4a.BA.raiseEvent(Unknown Source)
at b4j/anywheresoftware.b4j.objects.NodeWrapper$1.handle(Unknown Source)
at b4j/anywheresoftware.b4j.objects.NodeWrapper$1.handle(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Unknown Source)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8200(Unknown Source)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Unknown Source)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Unknown Source)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: com.github.sarxos.webcam.WebcamException: java.util.concurrent.ExecutionException: com.github.sarxos.webcam.WebcamException: Cannot execute task
at b4j/com.github.sarxos.webcam.WebcamDiscoveryService.getWebcams(Unknown Source)
at b4j/com.github.sarxos.webcam.Webcam.getWebcams(Unknown Source)
at b4j/com.github.sarxos.webcam.Webcam.getWebcams(Unknown Source)
at b4j/com.github.sarxos.webcam.Webcam.getWebcams(Unknown Source)
... 46 more
Caused by: java.util.concurrent.ExecutionException: com.github.sarxos.webcam.WebcamException: Cannot execute task
at java.base/java.util.concurrent.FutureTask.report(Unknown Source)
at java.base/java.util.concurrent.FutureTask.get(Unknown Source)
... 50 more
Caused by: com.github.sarxos.webcam.WebcamException: Cannot execute task
at b4j/com.github.sarxos.webcam.WebcamProcessor$AtomicProcessor.process(Unknown Source)
at b4j/com.github.sarxos.webcam.WebcamProcessor.process(Unknown Source)
at b4j/com.github.sarxos.webcam.WebcamTask.process(Unknown Source)
at b4j/com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver$WebcamNewGrabberTask.newGrabber(Unknown Source)
at b4j/com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver.getDevices(Unknown Source)
at b4j/com.github.sarxos.webcam.WebcamDiscoveryService$WebcamsDiscovery.call(Unknown Source)
at b4j/com.github.sarxos.webcam.WebcamDiscoveryService$WebcamsDiscovery.call(Unknown Source)
at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
... 1 more
Caused by: java.lang.IllegalAccessError: superclass access check failed: class org.bridj.cpp.CPPRuntime_CPPDestructor_NativeImpl (in unnamed module @0x10f98ce2) cannot access class org.bridj.cpp.CPPRuntime$CPPDestructor (in module b4j) because module b4j does not export org.bridj.cpp to unnamed module @0x10f98ce2
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
at b4j/org.bridj.CallbackNativeImplementer.defineClass(Unknown Source)
at b4j/org.bridj.CallbackNativeImplementer.getCallbackImplType(Unknown Source)
at b4j/org.bridj.CRuntime.register(Unknown Source)
at b4j/org.bridj.CRuntime.register(Unknown Source)
at b4j/org.bridj.CRuntime$CTypeInfo.<init>(Unknown Source)
at b4j/org.bridj.CRuntime.getTypeInfo(Unknown Source)
at b4j/org.bridj.BridJ.getTypeInfo(Unknown Source)
at b4j/org.bridj.BridJ.createNativeObjectFromPointer(Unknown Source)
at b4j/org.bridj.BridJ.createNativeObjectFromPointer(Unknown Source)
at b4j/org.bridj.Pointer.getNativeObjectAtOffset(Unknown Source)
at b4j/org.bridj.CommonPointerIOs$CallbackPointerIO.get(Unknown Source)
at b4j/org.bridj.CommonPointerIOs$CallbackPointerIO.get(Unknown Source)
at b4j/org.bridj.Pointer.get(Unknown Source)
at b4j/org.bridj.Pointer.get(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime.getDestructor(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime.newCPPReleaser(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime.newCPPInstance(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime$CPPTypeInfo.initialize(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime$CPPTypeInfo.initialize(Unknown Source)
at b4j/org.bridj.CRuntime$CTypeInfo.initialize(Unknown Source)
at b4j/org.bridj.BridJ.initialize(Unknown Source)
at b4j/org.bridj.NativeObject.<init>(Unknown Source)
at b4j/org.bridj.StructObject.<init>(Unknown Source)
at b4j/org.bridj.cpp.CPPObject.<init>(Unknown Source)
at b4j/com.github.sarxos.webcam.ds.buildin.natives.OpenIMAJGrabber.<init>(Unknown Source)
at b4j/com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver$WebcamNewGrabberTask.handle(Unknown Source)
at b4j/com.github.sarxos.webcam.WebcamProcessor$AtomicProcessor.run(Unknown Source)
... 3 more

BlueCove stack shutdown completed
 

peacemaker

Expert
Licensed User
Longtime User
#PackagerProperty: AdditionalModuleInfoString = exports org.bridj.cpp;

Added, error is so:
Oct 09, 2022 9:47:11 AM com.github.sarxos.webcam.Webcam getDriver
INFO: WebcamDefaultDriver capture driver will be used
java.lang.IllegalAccessError: superclass access check failed: class org.bridj.dyncallbacks.PointerToVoid_0 (in unnamed module @0x1d712d3a) cannot access class org.bridj.DynamicFunction (in module b4j) because module b4j does not export org.bridj to unnamed module @0x1d712d3a
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
at b4j/org.bridj.CallbackNativeImplementer.defineClass(Unknown Source)
at b4j/org.bridj.CallbackNativeImplementer.getDynamicCallback(Unknown Source)
at b4j/org.bridj.CRuntime.getDynamicFunctionFactory(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime.getConstructor(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime.newCPPInstance(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime$CPPTypeInfo.initialize(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime$CPPTypeInfo.initialize(Unknown Source)
at b4j/org.bridj.CRuntime$CTypeInfo.initialize(Unknown Source)
at b4j/org.bridj.BridJ.initialize(Unknown Source)
at b4j/org.bridj.NativeObject.<init>(Unknown Source)
at b4j/org.bridj.StructObject.<init>(Unknown Source)
at b4j/org.bridj.cpp.CPPObject.<init>(Unknown Source)
at b4j/com.github.sarxos.webcam.ds.buildin.natives.OpenIMAJGrabber.<init>(Unknown Source)
at b4j/com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver$WebcamNewGrabberTask.handle(Unknown Source)
at b4j/com.github.sarxos.webcam.WebcamProcessor$AtomicProcessor.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
java.lang.RuntimeException: Failed to create callback for [void, class org.bridj.Pointer] : java.lang.IllegalAccessError: superclass access check failed: class org.bridj.dyncallbacks.PointerToVoid_0 (in unnamed module @0x1d712d3a) cannot access class org.bridj.DynamicFunction (in module b4j) because module b4j does not export org.bridj to unnamed module @0x1d712d3a
at b4j/org.bridj.CallbackNativeImplementer.getDynamicCallback(Unknown Source)
at b4j/org.bridj.CRuntime.getDynamicFunctionFactory(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime.getConstructor(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime.newCPPInstance(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime$CPPTypeInfo.initialize(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime$CPPTypeInfo.initialize(Unknown Source)
at b4j/org.bridj.CRuntime$CTypeInfo.initialize(Unknown Source)
at b4j/org.bridj.BridJ.initialize(Unknown Source)
at b4j/org.bridj.NativeObject.<init>(Unknown Source)
at b4j/org.bridj.StructObject.<init>(Unknown Source)
at b4j/org.bridj.cpp.CPPObject.<init>(Unknown Source)
at b4j/com.github.sarxos.webcam.ds.buildin.natives.OpenIMAJGrabber.<init>(Unknown Source)
at b4j/com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver$WebcamNewGrabberTask.handle(Unknown Source)
at b4j/com.github.sarxos.webcam.WebcamProcessor$AtomicProcessor.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalAccessError: superclass access check failed: class org.bridj.dyncallbacks.PointerToVoid_0 (in unnamed module @0x1d712d3a) cannot access class org.bridj.DynamicFunction (in module b4j) because module b4j does not export org.bridj to unnamed module @0x1d712d3a
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
at b4j/org.bridj.CallbackNativeImplementer.defineClass(Unknown Source)
... 17 more
java.lang.RuntimeException: Unable to create constructor -1 for class com.github.sarxos.webcam.ds.buildin.natives.OpenIMAJGrabber : java.lang.RuntimeException: Failed to create callback for [void, class org.bridj.Pointer] : java.lang.IllegalAccessError: superclass access check failed: class org.bridj.dyncallbacks.PointerToVoid_0 (in unnamed module @0x1d712d3a) cannot access class org.bridj.DynamicFunction (in module b4j) because module b4j does not export org.bridj to unnamed module @0x1d712d3a
at b4j/org.bridj.cpp.CPPRuntime.getConstructor(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime.newCPPInstance(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime$CPPTypeInfo.initialize(Unknown Source)
at b4j/org.bridj.cpp.CPPRuntime$CPPTypeInfo.initialize(Unknown Source)
at b4j/org.bridj.CRuntime$CTypeInfo.initialize(Unknown Source)
at b4j/org.bridj.BridJ.initialize(Unknown Source)
at b4j/org.bridj.NativeObject.<init>(Unknown Source)
at b4j/org.bridj.StructObject.<init>(Unknown Source)
at b4j/org.bridj.cpp.CPPObject.<init>(Unknown Source)
at b4j/com.github.sarxos.webcam.ds.buildin.natives.OpenIMAJGrabber.<init>(Unknown Source)
at b4j/com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver$WebcamNewGrabberTask.handle(Unknown Source)
at b4j/com.github.sarxos.webcam.WebcamProcessor$AtomicProcessor.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.RuntimeException: Failed to create callback for [void, class org.bridj.Pointer] : java.lang.IllegalAccessError: superclass access check failed: class org.bridj.dyncallbacks.PointerToVoid_0 (in unnamed module @0x1d712d3a) cannot access class org.bridj.DynamicFunction (in module b4j) because module b4j does not export org.bridj to unnamed module @0x1d712d3a
at b4j/org.bridj.CallbackNativeImplementer.getDynamicCallback(Unknown Source)
at b4j/org.bridj.CRuntime.getDynamicFunctionFactory(Unknown Source)
... 15 more
Caused by: java.lang.IllegalAccessError: superclass access check failed: class org.bridj.dyncallbacks.PointerToVoid_0 (in unnamed module @0x1d712d3a) cannot access class org.bridj.DynamicFunction (in module b4j) because module b4j does not export org.bridj to unnamed module @0x1d712d3a
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
at b4j/org.bridj.CallbackNativeImplementer.defineClass(Unknown Source)
... 17 more
 

peacemaker

Expert
Licensed User
Longtime User
But helped so:

#PackagerProperty: AdditionalModuleInfoString = exports org.bridj;
#PackagerProperty: AdditionalModuleInfoString = exports org.bridj.cpp;

Does such syntax not work ?

#PackagerProperty: AdditionalModuleInfoString = exports org.bridj.*;
 

peacemaker

Expert
Licensed User
Longtime User
Why slf4j-nop-2.0.3.jar ?
 

mcqueccu

Well-Known Member
Licensed User
Longtime User

TILogistic

Expert
Licensed User
Longtime User
What are the supported dimensions? Anytime I change the dimension it crashes. When I tried my webcams max resolution (1280x720) too it did not work.
But I need a Square (1:1 ratio) dimension like 640x640
?
 

TILogistic

Expert
Licensed User
Longtime User
?
search:
 

Magma

Expert
Licensed User
Longtime User
What are the supported dimensions? Anytime I change the dimension it crashes. When I tried my webcams max resolution (1280x720) too it did not work.
But I need a Square (1:1 ratio) dimension like 640x640
B4X:
Sub GetCameraAndResolutions As Boolean
    Dim success As Boolean=False
    Dim haveacam As Int=0
    Dim haveares As Int=0
    Log ("Trying to find webcam and set resolution")
    Try
        Log ("WebCamera Initialization")
        cam.Initialize
    Catch
        haveacam=0
        Log("No WebCam!!!")
        success=False
        Return success
    End Try
    Try
        defaultCam=cam.defaultCam
        haveacam=1
    Catch
        haveacam=0
        Log("I am sure, No WebCam!!!")
        success=False
        Return success
    End Try

    If haveacam>0 Then
' For example if you don't want the lowest... and you want something like or more than 320x240 rem... the resolutions
'        Try
'            cam.setDimension(defaultCam,176,144)
'            haveares=1
'        Catch
'            Log(LastException)
'        End Try
'        If haveares=0 Then
'            Try
'                cam.setDimension(defaultCam,240,160)
'                haveares=1
'            Catch
'                Log(LastException)
'            End Try
'        End If
        If haveares=0 Then
            Try
                cam.setDimension(defaultCam,320,240)
                haveares=1
            Catch
                Log("Go check next resolution...")
            End Try
        End If
        If haveares=0 Then
            Try
                cam.setDimension(defaultCam,400,240)
                haveares=1
            Catch
                Log("Go check next resolution...")
            End Try
        End If
        If haveares=0 Then
            Try
                cam.setDimension(defaultCam,480,320)
                haveares=1
            Catch
                Log("Go check next resolution...")
            End Try
        End If
        If haveares=0 Then
            Try
                cam.setDimension(defaultCam,640,480)
                haveares=1
            Catch
                Log("Go check next resolution...")
            End Try
        End If
        If haveares=0 Then
            Try
                cam.setDimension(defaultCam,768,576)
                haveares=1
            Catch
                Log("Go check next resolution...")
            End Try
        End If
        If haveares=0 Then
            Try
                cam.setDimension(defaultCam,1280,720)
                haveares=1
            Catch
                Log("Go check next resolution...")
            End Try
        End If
        If haveares=0 Then
            Try
                cam.setDimension(defaultCam,1920,1080)
                haveares=1
            Catch
                Log("Go check next resolution, is there other ?... need a special setup")
            End Try
        End If

        If haveares=0 Then
            Log("Not selectable resolution...")
            success=False
            Return success
        End If
    End If
  
    If haveacam=1 And haveares=1 Then
            Log("Yes we have WEBCam and a Right Resolution!")
            success=True
        webcamok=success
        Return True
    Else
        webcamok=success
        success=False
    End If
    Return success
End Sub

Ofcourse you can reverse and start from highest to lowest... if you wanna have fullhd or at least hd...
Is the only way found and worked for me...

the code can be optimized, setting resolutions into arrays... using loop
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
If so - i have added some subs for getting resolutions of WebCam. The project with updated class is uploaded to the first post.
Added::
Private Sub Class_Globals
    .....
    Type WebCamDeviceDimension (width As Int, height As Int)
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
 

Magma

Expert
Licensed User
Longtime User
For those who want to download the .jar:


1666082955375.png


Here is the site, where you can informed about latest news (for the API):
 

Magma

Expert
Licensed User
Longtime User
If so - i have added some subs for getting resolutions of WebCam. The project with updated class is uploaded to the first post.
Added::
Private Sub Class_Globals
    .....
    Type WebCamDeviceDimension (width As Int, height As Int)
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
Hi there...

I was happy for that sub giving us the possible resolutions of webcam... but it not giving a full list with resolutions... but only 2,3 max 4 res... For example my webcam has about 10-12 different resolutions... and gave me 3... but may be it is only for me...

So I ve change "for the moment" to my trick-sub that try/catch ... not the best but at least i can have more options :) ... but blind... :cool:
 
Top