Examples of JavaFX using the webcam API

Source: Internet
Author: User

Here's an example of this on GitHub:

Its URL is: Https://github.com/sarxos/webcam-capture/tree/master/webcam-capture-examples/webcam-capture-javafx

I do not know if you can access this link on this, do not know if there is no wall (because I am not in the country).

If there was a wall, I uploaded the camera bag, which contains many examples. You can download http://download.csdn.net/detail/yizdream/8196815 on this link.


When you attach your lib, which is the jar in the camera bag, do not forget to refer to the jar in the Lib in the camera bag.


Take a look at the example, how to use this package on JavaFX.

But here's to thank Rakesh Bhatt (rakeshbhatt10) for sharing his code ...


Import Java.awt.image.bufferedimage;import Javafx.application.application;import javafx.application.Platform; Import Javafx.beans.property.objectproperty;import Javafx.beans.property.simpleobjectproperty;import Javafx.beans.value.changelistener;import Javafx.beans.value.observablevalue;import Javafx.collections.fxcollections;import Javafx.collections.observablelist;import Javafx.concurrent.Task;import Javafx.embed.swing.swingfxutils;import Javafx.event.actionevent;import Javafx.event.eventhandler;import Javafx.geometry.orientation;import Javafx.geometry.pos;import Javafx.scene.scene;import Javafx.scene.control.button;import Javafx.scene.control.combobox;import Javafx.scene.control.label;import Javafx.scene.image.image;import Javafx.scene.image.imageview;import Javafx.scene.layout.borderpane;import Javafx.scene.layout.flowpane;import Javafx.stage.stage;import com.github.sarxos.webcam.webcam;/** * This example Demonstrates. Webcam Capture API in a JavaFX * application. * * @autHor Rakesh Bhatt (rakeshbhatt10) */public class Webcamapplauncher extends application {Private class Webcaminfo {private S Tring webcamname;private int webcamindex;public String getwebcamname () {return webcamname;} public void Setwebcamname (String webcamname) {this.webcamname = Webcamname;} public int Getwebcamindex () {return webcamindex;} public void Setwebcamindex (int webcamindex) {this.webcamindex = Webcamindex;} @Overridepublic String toString () {return webcamname;}} Private Flowpane bottomcameracontrolpane;private flowpane toppane;private borderpane root;private String Cameralistprompttext = "Choose Camera";p rivate ImageView imgwebcamcapturedimage;private Webcam Webcam = null;private Boolean Stopcamera = false;private bufferedimage grabbedimage;private objectproperty<image> imageProperty = new Simpleobjectproperty<image> ();p rivate borderpane webcampane;private button btncamreastop;private button Btncamreastart;private Button btncameradispose; @Overridepublic void Start (StagePrimarystage) {primarystage.settitle ("Connecting Camera Device Using Webcam Capture API"); root = new Borderpane (); TopPane = new Flowpane (); toppane.setalignment (Pos.center); Toppane.sethgap () Toppane.setorientation ( Orientation.horizontal); Toppane.setprefheight (+); Root.settop (toppane); webcampane = new Borderpane (); Webcampane.setstyle ("-fx-background-color: #ccc;"); Imgwebcamcapturedimage = new ImageView (); Webcampane.setcenter (imgwebcamcapturedimage); Root.setcenter (WebCamPane); Createtoppanel (); bottomcameracontrolpane = new Flowpane (); Bottomcameracontrolpane.setorientation ( Orientation.horizontal); bottomcameracontrolpane.setalignment (Pos.center); Bottomcameracontrolpane.sethgap (20); Bottomcameracontrolpane.setvgap (+); Bottomcameracontrolpane.setprefheight (40); Bottomcameracontrolpane.setdisable (True); Createcameracontrols (); Root.setbottom (Bottomcameracontrolpane); Primarystage.setscene (Root);p rimarystage.setheight;p rimarystage.setwidth (600); Primarystage.centeronscreen ();p RimarysTage.show (); Platform.runlater (New Runnable () {@Overridepublic void run () {setimageviewsize ();}}); protected void Setimageviewsize () {Double height = webcampane.getheight ();d ouble width = webcampane.getwidth (); Imgwebcamcapturedimage.setfitheight (height); imgwebcamcapturedimage.setfitwidth (width); Imgwebcamcapturedimage.prefheight (height); imgwebcamcapturedimage.prefwidth (width); Imgwebcamcapturedimage.setpreserveratio (TRUE);} private void Createtoppanel () {int webcamcounter = 0; Label Lbinfolabel = new Label ("Select Your WebCam Camera");observablelist<webcaminfo> options = Fxcollections.observablearraylist (); Toppane.getchildren (). Add (Lbinfolabel); for (Webcam Webcam:Webcam.getWebcams ( ) {Webcaminfo webcaminfo = new Webcaminfo (); Webcaminfo.setwebcamindex (Webcamcounter); Webcaminfo.setwebcamname ( Webcam.getname ()); Options.add (webcaminfo); webcamcounter++;} combobox<webcaminfo> cameraoptions = new combobox<webcaminfo> (); Cameraoptions.setitems (options); Cameraoptions.setprompttext (cAmeralistprompttext); Cameraoptions.getselectionmodel (). Selecteditemproperty (). AddListener (New ChangeListener <WebCamInfo> () {@Overridepublic void changed (OBSERVABLEVALUE&LT;? extends Webcaminfo> arg0, Webcaminfo arg1, Webcaminfo arg2) {if (arg2! = null) {System.out.println ("WebCam Index:" + arg2.getwebcamindex () + ": WebCam Name:" + arg2 . Getwebcamname ()); Initializewebcam (Arg2.getwebcamindex ());}}); Toppane.getchildren (). Add (cameraoptions);} protected void Initializewebcam (final int webcamindex) {task<void> webcamtask = new task<void> () {@ overrideprotected Void Call () throws Exception {if (webCam! = null) {Disposewebcamcamera ();} WebCam = Webcam.getwebcams (). get (Webcamindex); Webcam.open (); Startwebcamstream (); return null;}}; Thread webcamthread = new Thread (webcamtask); Webcamthread.setdaemon (true); Webcamthread.start (); Bottomcameracontrolpane.setdisable (false); btncamreastart.setdisable (true);} protected void Startwebcamstream () {Stopcamera = false; task<void> task =New Task<void> () {@Overrideprotected Void call () throws Exception {while (!stopcamera) {try {if (grabbedimage = Web Cam.getimage ()) = null) {Platform.runlater (new Runnable () {@Overridepublic void run () {Image mainiamge = swingfxutils.to Fximage (grabbedimage, null); Imageproperty.set (MAINIAMGE);}}); Grabbedimage.flush ();}} catch (Exception e) {e.printstacktrace ();}} return null;}}; Thread th = new thread (Task); Th.setdaemon (true); Th.start (); Imgwebcamcapturedimage.imageproperty (). Bind ( Imageproperty);} private void Createcameracontrols () {btncamreastop = new Button (); Btncamreastop.setonaction (New eventhandler< Actionevent> () {@Overridepublic void handle (ActionEvent arg0) {Stopwebcamcamera ();}}); Btncamreastop.settext ("Stop Camera"); Btncamreastart = new Button (); Btncamreastart.setonaction (New eventhandler< Actionevent> () {@Overridepublic void handle (ActionEvent arg0) {Startwebcamcamera ();}}); Btncamreastart.settext ("Start Camera"); btncameradispose = new Button (); btncameradispose.seTtext ("Dispose Camera"); Btncameradispose.setonaction (new eventhandler<actionevent> () {@Overridepublic void Handle (ActionEvent arg0) {Disposewebcamcamera ();}}); Bottomcameracontrolpane.getchildren (). Add (Btncamreastart); Bottomcameracontrolpane.getchildren (). Add ( Btncamreastop); Bottomcameracontrolpane.getchildren (). Add (btncameradispose);} protected void Disposewebcamcamera () {Stopcamera = True;webcam.close (); Webcam.shutdown (); btncamreastart.setdisable (true); Btncamreastop.setdisable (true);} protected void Startwebcamcamera () {Stopcamera = False;startwebcamstream (); btncamreastop.setdisable (false); Btncamreastart.setdisable (TRUE);} protected void Stopwebcamcamera () {Stopcamera = True;btncamreastart.setdisable (false); Btncamreastop.setdisable (True );} public static void Main (string[] args) {launch (args);}}




Examples of JavaFX using the webcam API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.