注意:代码仅供参考,需要根据具体的设备做调整
同系列文章:
演示程序源代码下载:
FaceIdDemo-Java-20161118.zip
注意:需要引入FaceId.jar
演示程序界面
源代码
TcpClientDemo.java
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Splash; import java.util.Locale; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.stage.Stage; /** * * @author Splash */ public class TcpClientDemo extends Application { @Override public void start(Stage stage) throws Exception { Locale.setDefault(Locale.CHINESE);// 让Dialogs按钮显示中文 Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); Scene scene = new Scene(root); stage.setScene(scene); // 设置窗体标题 stage.setTitle("设备命令控制台"); // 设置窗体图标 stage.getIcons().add(new Image(getClass().getResourceAsStream("FireEyes.png"))); // 设置到屏幕中心 stage.centerOnScreen(); // 显示窗体 stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }
FXMLDocumentController.java
/* ---------------------------------------------------------- * 文件名称:FXMLDocumentController.java * * 作者:秦建辉 * * QQ:36748897 * * 博客:http://www.firstsolver.com/wordpress/ * * 开发环境: * NetBeans 8.0 * JDK 8u20 * ControlsFX-8.0.6_20 * * 版本历史: * V1.0 2014年09月09日 * 执行设备命令 ------------------------------------------------------------ */ package Splash; import com.hanvon.faceid.sdk.*; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.stage.Stage; import org.controlsfx.dialog.DialogStyle; import org.controlsfx.dialog.Dialogs; /** * * @author Splash */ public class FXMLDocumentController implements Initializable { private final String DeviceCharset = "GBK"; @FXML private TextField textFieldDeviceIP; @FXML private TextField textFieldDevicePort; @FXML private TextField textFieldDeviceCommand; @FXML private TextArea textAreaRecords; @FXML private void handleButtonExecuteDeviceCommand(ActionEvent event) { // 获取当前应用窗体 Stage stage = (Stage)((Button)event.getSource()).getScene().getWindow(); // 清空命令结果文本显示框 textAreaRecords.clear(); try(FaceId tcpClient = new FaceId(textFieldDeviceIP.getText(), Integer.parseInt(textFieldDevicePort.getText()))) { // 设置服务器地址 FaceIdAnswer output = new FaceIdAnswer(); FaceId_ErrorCode ErrorCode = tcpClient.Execute(textFieldDeviceCommand.getText(), output, DeviceCharset); if (ErrorCode.equals(FaceId_ErrorCode.Success)) { textAreaRecords.appendText(output.answer); } else { Dialogs.create().style(DialogStyle.NATIVE).owner(stage).title("错误").message("执行设备命令失败!").showError(); } } catch (RuntimeException | IOException e) { Dialogs.create().style(DialogStyle.NATIVE).owner(stage).title("错误").message("连接设备失败!").showError(); } } @FXML private void handleButtonClearAction(ActionEvent event) { textAreaRecords.clear(); } @Override public void initialize(URL url, ResourceBundle rb) { } }
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.text.*?> <?import javafx.scene.effect.*?> <?import javafx.geometry.*?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <AnchorPane id="AnchorPane" minWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Splash.FXMLDocumentController"> <children> <GridPane minWidth="600.0"> <columnConstraints> <ColumnConstraints /> </columnConstraints> <rowConstraints> <RowConstraints /> <RowConstraints /> <RowConstraints /> <RowConstraints /> </rowConstraints> <children> <GridPane GridPane.hgrow="ALWAYS" GridPane.rowIndex="0"> <columnConstraints> <ColumnConstraints /> <ColumnConstraints hgrow="ALWAYS" /> </columnConstraints> <rowConstraints> <RowConstraints /> <RowConstraints /> </rowConstraints> <children> <Label text="设备地址" GridPane.columnIndex="0" GridPane.halignment="RIGHT" GridPane.rowIndex="0"> <GridPane.margin> <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" /> </GridPane.margin> </Label> <Label text="设备端口" GridPane.columnIndex="0" GridPane.halignment="RIGHT" GridPane.rowIndex="1"> <GridPane.margin> <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" /> </GridPane.margin> </Label> <TextField fx:id="textFieldDeviceIP" text="192.168.1.18" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="0" GridPane.vgrow="ALWAYS"> <GridPane.margin> <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" /> </GridPane.margin> </TextField> <TextField fx:id="textFieldDevicePort" text="9922" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1"> <GridPane.margin> <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" /> </GridPane.margin> </TextField> </children> <GridPane.margin> <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" /> </GridPane.margin> </GridPane> <GridPane GridPane.hgrow="ALWAYS" GridPane.rowIndex="1"> <columnConstraints> <ColumnConstraints hgrow="ALWAYS" /> </columnConstraints> <rowConstraints> <RowConstraints /> <RowConstraints /> </rowConstraints> <children> <Label text="设备命令:" GridPane.rowIndex="0"> <GridPane.margin> <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" /> </GridPane.margin> </Label> <TextField fx:id="textFieldDeviceCommand" text="GetDeviceInfo()" GridPane.rowIndex="1"> <GridPane.margin> <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" /> </GridPane.margin> </TextField> </children> <GridPane.margin> <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" /> </GridPane.margin> </GridPane> <GridPane GridPane.rowIndex="2"> <columnConstraints> <ColumnConstraints /> <ColumnConstraints /> </columnConstraints> <rowConstraints> <RowConstraints /> </rowConstraints> <children> <Button fx:id="buttonClear" minHeight="40.0" minWidth="80.0" onAction="#handleButtonClearAction" text="清空" GridPane.columnIndex="0" GridPane.hgrow="ALWAYS"> <GridPane.margin> <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" /> </GridPane.margin> </Button> <Button fx:id="buttonExecuteDeviceCommand" minHeight="40.0" minWidth="80.0" onAction="#handleButtonExecuteDeviceCommand" text="执行命令" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.hgrow="ALWAYS"> <GridPane.margin> <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" /> </GridPane.margin> </Button> </children> </GridPane> <TextArea fx:id="textAreaRecords" editable="false" minHeight="400.0" minWidth="600.0" wrapText="true" GridPane.hgrow="ALWAYS" GridPane.rowIndex="3" GridPane.vgrow="ALWAYS"> <GridPane.margin> <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" /> </GridPane.margin> </TextArea> </children> </GridPane> </children> </AnchorPane>