B4J Library [Class] Webcam Capture

The goal of this class is to control a webcam. There is a free software project called Webcam Capture (Generic Webcam Java API). The data of the author of this API are:

Bartosz Firyn.
Web
GitHub

This class directly accesses to some functions of the Webcam Capture Api.

Instructions:

1. Copy this files (Link) to the folder defalut libs of the B4J:

2015-04-25_2044.png



2. In the project add the references to the libs like the image below:

2015-04-25_2046.png



3.- Check the JavaObject library like the image below:

2015-04-25_2050.png


4.- Use the class


Class: Webcam
Author of the class:
@somed3v3loper
Version: 1.0

The available methods are:

  • getDefaultCam
  • SetDimension (cam As JavaObject, width As Int, height As Int)
  • OpenCam (cam As JavaObject)
  • TakePicture(cam As JavaObject, filename As String)
Example:

B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
    #AdditionalJar : webcam-capture-0.3.10
    #AdditionalJar : slf4j-api-1.7.2
    #AdditionalJar : bridj-0.6.2
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim cam As Webcam
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    cam.Initialize
    Dim defaultCam As JavaObject=cam.defaultCam
    cam.setDimension(defaultCam,640,480)
    cam.OpenCam(defaultCam)
    cam.TakePicture(defaultCam,"test1.png")
End Sub


This class comes from the information provided in the discussion of the following thread:
https://www.b4x.com/android/forum/threads/web-camera.36098/

Thanks to: @somed3v3loper (he is the initial autor of the class), I'm just gathering information and putting it pretty :)

I hope that the class is going to expand soon with the other functions. Any comments or contribution is welcome
 

Attachments

  • WebcamCaptureExample.zip
    1.7 KB · Views: 1,323
Last edited:

Phayao

Active Member
Licensed User
Longtime User
Thanks for the very useful code - but for me the Webcam class is not working:
The line:
ImageIO.RunMethod("write",Array(BufferedImage, "PNG", FileIO))
causes the error:
java.lang.RuntimeException: Object should first be initialized (JavaObject).

I played around but could not figure out why this error appeared since all the objects should be initialized in the sub initialize code.
Somebody has an idea what I'm doing wrong here ?
Thanks in advance,
Chris
 

Chris Tyrie

Member
Licensed User
Longtime User
Is there any chance someone could pls expose custom view sizes in this library so I can use HD cameras.

If you do I'll be your best friend forever :)
 

BarryW

Active Member
Licensed User
Longtime User
The goal of this class is to control a webcam. There is a free software project called Webcam Capture (Generic Webcam Java API). The data of the author of this API are:

Bartosz Firyn.
Web
GitHub

This class directly accesses to some functions of the Webcam Capture Api.

Instructions:

1. Copy this files (Link) to the folder defalut libs of the B4J:

2015-04-25_2044.png



2. In the project add the references to the libs like the image below:

2015-04-25_2046.png



3.- Check the JavaObject library like the image below:

2015-04-25_2050.png


4.- Use the class


Class: Webcam
Author of the class:
@somed3v3loper
Version: 1.0

The available methods are:

  • getDefaultCam
  • SetDimension (cam As JavaObject, width As Int, height As Int)
  • OpenCam (cam As JavaObject)
  • TakePicture(cam As JavaObject, filename As String)
Example:

B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
    #AdditionalJar : webcam-capture-0.3.10
    #AdditionalJar : slf4j-api-1.7.2
    #AdditionalJar : bridj-0.6.2
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim cam As Webcam
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    cam.Initialize
    Dim defaultCam As JavaObject=cam.defaultCam
    cam.setDimension(defaultCam,640,480)
    cam.OpenCam(defaultCam)
    cam.TakePicture(defaultCam,"test1.png")
End Sub


This class comes from the information provided in the discussion of the following thread:
https://www.b4x.com/android/forum/threads/web-camera.36098/

Thanks to: @somed3v3loper (he is the initial autor of the class), I'm just gathering information and putting it pretty :)

I hope that the class is going to expand soon with the other functions. Any comments or contribution is welcome

can we use this on non ui project?
 

German Buchmuller

Member
Licensed User
Longtime User
The goal of this class is to control a webcam. There is a free software project called Webcam Capture (Generic Webcam Java API). The data of the author of this API are:

Bartosz Firyn.
Web
GitHub

This class directly accesses to some functions of the Webcam Capture Api.

Instructions:

1. Copy this files (Link) to the folder defalut libs of the B4J:

2015-04-25_2044.png



2. In the project add the references to the libs like the image below:

2015-04-25_2046.png



3.- Check the JavaObject library like the image below:

2015-04-25_2050.png


4.- Use the class


Class: Webcam
Author of the class:
@somed3v3loper
Version: 1.0

The available methods are:

  • getDefaultCam
  • SetDimension (cam As JavaObject, width As Int, height As Int)
  • OpenCam (cam As JavaObject)
  • TakePicture(cam As JavaObject, filename As String)
Example:

B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
    #AdditionalJar : webcam-capture-0.3.10
    #AdditionalJar : slf4j-api-1.7.2
    #AdditionalJar : bridj-0.6.2
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim cam As Webcam
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    cam.Initialize
    Dim defaultCam As JavaObject=cam.defaultCam
    cam.setDimension(defaultCam,640,480)
    cam.OpenCam(defaultCam)
    cam.TakePicture(defaultCam,"test1.png")
End Sub


This class comes from the information provided in the discussion of the following thread:
https://www.b4x.com/android/forum/threads/web-camera.36098/

Thanks to: @somed3v3loper (he is the initial autor of the class), I'm just gathering information and putting it pretty :)

I hope that the class is going to expand soon with the other functions. Any comments or contribution is welcome
Hi, the Libs Link is broken. Can you Re-Upload it please? thanks
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
if i remember correctly, the photo actually was showing on a pane, you could take a snapshot of with the pane.snaphost function. that was many many years ago. i dont know as of today.
 

Ilya G.

Active Member
Licensed User
Longtime User
How to remake this function so that it returns type Image?

B4X:
Public Sub TakePicture2(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))
    Log("Close: "& cam.RunMethod("close", Null))
End Sub
 

Ilya G.

Active Member
Licensed User
Longtime User
It works fine when called with a 200ms timer

B4X:
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
 

ElliotHC

Active Member
Licensed User
Does anyone know how I can open the camera leaving it open to take multiple images fairly rapidly?
It takes over 3 seconds to open it each time which is too slow for what I'm trying to use it for.
Thanks

Added:
Set a timer with a flag for opening the camera. Then used an if statement to take the images once the camera is open.
 
Last edited:

Magma

Expert
Licensed User
Longtime User
Top