camera.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
import java.awt.image.BufferedImage; import java.util.concurrent.atomic.AtomicReference; import com.github.sarxos.webcam.Webcam; 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.image.WritableImage; import javafx.scene.layout.BorderPane; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; /** * This example demonstrates how to use Webcam Capture API in a JavaFX application. * * @author Rakesh Bhatt (rakeshbhatt10) */ public class WebCamAppLauncher extends Application { private class WebCamInfo { private String 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; } @Override public String toString() { return webCamName; } } private FlowPane bottomCameraControlPane; private FlowPane topPane; private BorderPane root; private String cameraListPromptText = "Choose Camera"; private ImageView imgWebCamCapturedImage; private Webcam webCam = null; private boolean stopCamera = false; private BufferedImage grabbedImage; private ObjectProperty<Image> imageProperty = new SimpleObjectProperty<Image>(); private BorderPane webCamPane; private Button btnCamreaStop; private Button btnCamreaStart; private Button btnCameraDispose; @Override public void start(Stage primaryStage) { primaryStage.setTitle("Connecting Camera Device Using Webcam Capture API"); root = new BorderPane(); topPane = new FlowPane(); topPane.setAlignment(Pos.CENTER); topPane.setHgap(20); topPane.setOrientation(Orientation.HORIZONTAL); topPane.setPrefHeight(40); 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(10); bottomCameraControlPane.setPrefHeight(40); bottomCameraControlPane.setDisable(true); createCameraControls(); root.setBottom(bottomCameraControlPane); primaryStage.setScene(new Scene(root)); primaryStage.setHeight(700); primaryStage.setWidth(600); primaryStage.centerOnScreen(); primaryStage.show(); Platform.runLater(new Runnable() { @Override public void run() { setImageViewSize(); } }); } protected void setImageViewSize() { double height = webCamPane.getHeight(); double 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>() { @Override public void changed(ObservableValue<? 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>() { @Override protected 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>() { @Override protected Void call() throws Exception { final AtomicReference<WritableImage> ref = new AtomicReference<>(); BufferedImage img = null; while (!stopCamera) { try { if ((img = webCam.getImage()) != null) { ref.set(SwingFXUtils.toFXImage(img, ref.get())); img.flush(); Platform.runLater(new Runnable() { @Override public void run() { imageProperty.set(ref.get()); } }); } } 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>() { @Override public void handle(ActionEvent arg0) { stopWebCamCamera(); } }); btnCamreaStop.setText("Stop Camera"); btnCamreaStart = new Button(); btnCamreaStart.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent arg0) { startWebCamCamera(); } }); btnCamreaStart.setText("Start Camera"); btnCameraDispose = new Button(); btnCameraDispose.setText("Dispose Camera"); btnCameraDispose.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent arg0) { disposeWebCamCamera(); } }); bottomCameraControlPane.getChildren().add(btnCamreaStart); bottomCameraControlPane.getChildren().add(btnCamreaStop); bottomCameraControlPane.getChildren().add(btnCameraDispose); } protected void disposeWebCamCamera() { stopCamera = true; webCam.close(); 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); } } |