Java:汉王人脸通SDK示例代码(五)获取考勤记录

注意:代码仅供参考,需要根据具体的设备做调整
同系列文章:

演示程序源代码下载:

FaceIdDemo-Java-20161118.zip
注意:需要引入FaceId.jar

演示程序界面
GetRecord

源代码
GetRecord.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 GetRecord 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.1    2014年11月04日
 *              完善考勤记录提取正则表达式,使之能完整获取带照片的考勤记录
 *
 *      V1.0    2014年09月13日
 *              获取考勤记录
------------------------------------------------------------ */

package Splash;

import com.hanvon.faceid.sdk.FaceId;
import com.hanvon.faceid.sdk.FaceIdAnswer;
import com.hanvon.faceid.sdk.FaceId_ErrorCode;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
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 textBoxDeviceIP;
    
    @FXML
    private TextField textBoxDevicePort;
     
    @FXML
    private ListView listViewRecord;    
    
    @FXML
    private void handleButtonExport(ActionEvent event) throws IOException{
        // 获取当前应用窗体
        Stage stage = (Stage)((Button)event.getSource()).getScene().getWindow(); 
        
        try(FaceId tcpClient = new FaceId(textBoxDeviceIP.getText(), Integer.parseInt(textBoxDevicePort.getText()))) {
            // 获取考勤记录
            String Command = "GetRecord(end_time=\"" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\")";
            FaceIdAnswer output = new FaceIdAnswer();
            FaceId_ErrorCode ErrorCode = tcpClient.Execute(Command, output, DeviceCharset);
            if (ErrorCode.equals(FaceId_ErrorCode.Success)) 
            {   
                // 提取单条考勤记录
                List<String> RecordList = new LinkedList<>();
                Pattern p = Pattern.compile("\\b(time=.+\\R(?:photo=\"[^\"]+\")*)");                
                Matcher m = p.matcher(output.answer);
                while(m.find())
                {                    
                    RecordList.add(m.group(1));
                }  
                listViewRecord.setItems(FXCollections.observableList(RecordList));
             }
        }
        catch (RuntimeException | IOException e)
        {                 
            Dialogs.create().style(DialogStyle.NATIVE).owner(stage).title("错误").message("连接设备失败!").showError();            
        }        
    }
    
    @FXML
    private void handleButtonClear(ActionEvent event){
        listViewRecord.setItems(null);
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO        
    }    
    
}

FXMLDocument.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" minWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Splash.FXMLDocumentController">
    <children>
        <VBox minWidth="800.0">
            <children> 
                <GridPane>
                    <rowConstraints>
                        <RowConstraints />
                        <RowConstraints />                      
                        <RowConstraints />
                    </rowConstraints>
                    
                    <columnConstraints>
                        <ColumnConstraints />
                        <ColumnConstraints halignment="LEFT" hgrow="ALWAYS" />
                    </columnConstraints>
                    
                    <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>
                        
                        <TextField fx:id="textBoxDeviceIP" text="192.168.1.18" GridPane.columnIndex="1" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="0">
                            <GridPane.margin>
                                <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" />
                            </GridPane.margin>
                        </TextField>

                        <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="textBoxDevicePort" text="9922" GridPane.columnIndex="1" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="1">
                            <GridPane.margin>
                                <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" />
                            </GridPane.margin>
                        </TextField>
                        
                        <GridPane GridPane.columnIndex="0" GridPane.columnSpan="2147483647" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2">
                            <columnConstraints>
                                <ColumnConstraints hgrow="ALWAYS" />
                                <ColumnConstraints hgrow="ALWAYS" />
                                <ColumnConstraints hgrow="ALWAYS" />
                            </columnConstraints>
                            
                            <rowConstraints>
                               <RowConstraints />
                           </rowConstraints>
                           
                           <children>                               
                               <Label text="考勤记录列表" GridPane.columnIndex="0" GridPane.halignment="LEFT" GridPane.hgrow="ALWAYS" GridPane.valignment="BOTTOM">
                                   <GridPane.margin>
                                       <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" />
                                   </GridPane.margin>
                               </Label>
                               
                               <Button fx:id="buttonExport" minHeight="48.0" onAction="#handleButtonExport" text="导出记录" GridPane.columnIndex="1" GridPane.halignment="CENTER">
                                   <GridPane.margin>
                                       <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" />
                                   </GridPane.margin>
                               </Button>
                               
                               <Button fx:id="buttonClear" minHeight="40.0" onAction="#handleButtonClear" text="清空记录" GridPane.columnIndex="2" GridPane.halignment="RIGHT">
                                   <GridPane.margin>
                                       <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" />
                                   </GridPane.margin>
                               </Button>
                           </children>
                           
                           <GridPane.margin>
                               <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" />
                           </GridPane.margin>
                        </GridPane>
                    </children>
                </GridPane>
                                 
                <ListView fx:id="listViewRecord" VBox.vgrow="ALWAYS">
                   <VBox.margin>
                       <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" />
                   </VBox.margin>
                </ListView>
                
            </children>
            <padding>
                <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" />
            </padding> 
        </VBox>      
    </children>
</AnchorPane>

Comments are closed.