JavaFX使用摄像头API的示例

在GITHUB上面有这样的示例:

它的网址是:https://github.com/sarxos/webcam-capture/tree/master/webcam-capture-examples/webcam-capture-javafx

我不知道大家是否可以访问的上这个链接,不知道有没有被墙了(因为我不在国内)。

如果有被墙的话,我上传了摄像头包,其中包含很多示例。可在这个链接上下载  http://download.csdn.net/detail/yizdream/8196815


当你附加你的LIB后,也就是摄像头包中的JAR,别忘了摄像头包里的LIB里的JAR一样要引用的。


看看示例吧,怎样在javaFX上使用这个包。

不过这里要感谢Rakesh Bhatt (rakeshbhatt10)分享了他的代码......


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 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 {


				while (!stopCamera) {
					try {
						if ((grabbedImage = webCam.getImage()) != null) {


							Platform.runLater(new Runnable() {


								@Override
								public void run() {
									Image mainiamge = SwingFXUtils.toFXImage(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>() {


			@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();
		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);
	}
}




  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的JavaFX二级菜单示例: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class TwoLevelMenuExample extends Application { @Override public void start(Stage primaryStage) throws Exception { BorderPane root = new BorderPane(); MenuBar menuBar = new MenuBar(); Menu fileMenu = new Menu("File"); MenuItem openFile = new MenuItem("Open File"); MenuItem saveFile = new MenuItem("Save File"); fileMenu.getItems().addAll(openFile, saveFile); Menu editMenu = new Menu("Edit"); MenuItem cut = new MenuItem("Cut"); MenuItem copy = new MenuItem("Copy"); MenuItem paste = new MenuItem("Paste"); editMenu.getItems().addAll(cut, copy, paste); MenuBar subMenu = new MenuBar(); Menu optionsMenu = new Menu("Options"); MenuItem settings = new MenuItem("Settings"); optionsMenu.getItems().add(settings); subMenu.getMenus().add(optionsMenu); menuBar.getMenus().addAll(fileMenu, editMenu, subMenu); root.setTop(menuBar); Scene scene = new Scene(root, 400, 300); primaryStage.setTitle("Two Level Menu Example"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 该示例创建了一个包含文件和编辑两个主菜单以及一个选项子菜单的菜单栏。选项子菜单包含一个设置菜单项。当用户单击菜单项时,可以显示相应的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值