From 92a2fc830fc801845998d4a230fc138ba82bcf61 Mon Sep 17 00:00:00 2001 From: Bartosz Firyn Date: Wed, 10 Feb 2016 10:55:27 +0100 Subject: [PATCH] Fine tuning saving memory consumption in JFX example, refs #432 --- .../src/main/java/WebCamAppLauncher.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/webcam-capture-examples/webcam-capture-javafx/src/main/java/WebCamAppLauncher.java b/webcam-capture-examples/webcam-capture-javafx/src/main/java/WebCamAppLauncher.java index d491219b..e3f0c9f1 100644 --- a/webcam-capture-examples/webcam-capture-javafx/src/main/java/WebCamAppLauncher.java +++ b/webcam-capture-examples/webcam-capture-javafx/src/main/java/WebCamAppLauncher.java @@ -1,4 +1,9 @@ +import java.awt.Dimension; import java.awt.image.BufferedImage; +import java.util.concurrent.atomic.AtomicReference; + +import com.github.sarxos.webcam.Webcam; +import com.github.sarxos.webcam.WebcamResolution; import javafx.application.Application; import javafx.application.Platform; @@ -20,16 +25,14 @@ import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; +import javafx.scene.image.WritableImage; import javafx.scene.layout.BorderPane; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; -import com.github.sarxos.webcam.Webcam; - /** - * This example demonstrates how to use Webcam Capture API in a JavaFX - * application. + * This example demonstrates how to use Webcam Capture API in a JavaFX application. * * @author Rakesh Bhatt (rakeshbhatt10) */ @@ -177,6 +180,16 @@ protected Void call() throws Exception { } webCam = Webcam.getWebcams().get(webCamIndex); + + Dimension[] nonStandardResolutions = new Dimension[] { + WebcamResolution.PAL.getSize(), + WebcamResolution.HD720.getSize(), + new Dimension(2000, 1000), + new Dimension(1000, 500), }; + + webCam.setCustomViewSizes(nonStandardResolutions); + webCam.setViewSize(WebcamResolution.HD720.getSize()); + webCam.open(); startWebCamStream(); @@ -202,21 +215,23 @@ protected void startWebCamStream() { @Override protected Void call() throws Exception { + final AtomicReference ref = new AtomicReference<>(); + BufferedImage img = null; + while (!stopCamera) { try { - if ((grabbedImage = webCam.getImage()) != null) { + if ((img = webCam.getImage()) != null) { - final Image mainiamge = SwingFXUtils.toFXImage(grabbedImage, null); + ref.set(SwingFXUtils.toFXImage(img, ref.get())); + img.flush(); Platform.runLater(new Runnable() { @Override public void run() { - imageProperty.set(mainiamge); + imageProperty.set(ref.get()); } }); - - grabbedImage.flush(); } } catch (Exception e) { e.printStackTrace();