Skip to content

Commit

Permalink
Fine tuning saving memory consumption in JFX example, refs #432
Browse files Browse the repository at this point in the history
sarxos committed Feb 10, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 158b65a commit 92a2fc8
Showing 1 changed file with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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<WritableImage> 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();

0 comments on commit 92a2fc8

Please sign in to comment.