B4J Library [Class] Webcam Capture

CanguroCode

Active Member
Licensed 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:




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




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



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:
http://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

Last edited:

Phayao

Active Member
Licensed 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
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
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:




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




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



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:
http://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
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:




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




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



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:
http://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
 

Enrique Gonzalez R

Well-Known Member
Licensed 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.
 

GanjaKyp

Active Member
Licensed 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
 

GanjaKyp

Active Member
Licensed 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
 
Top