4

我正在与允许我使用指纹扫描仪的 JNI 交互。我编写的代码将扫描的 ByteBuffer 由 JNI 解析回它,并将其转换为 BufferedImage 进行保存。

我想不通的是如何在我的 GUI 上的 jlabel 图标尝试更新之前等待扫描线程完成。最简单的方法是什么?

我还需要添加什么?

编辑:

//Scanner class
Thread thread = new Thread() {
        public void run() {
            // [...] get ByteBuffer and Create Image code
            try {
                File out = new File("C:\\Users\\Desktop\\print.png");
                ImageIO.write(padded, "png", out);
                // [???] set flag here
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
thread.start();
return true;

//Gui class
private void btnScanPrintActionPerformed(java.awt.event.ActionEvent evt) {
    Scanner scanPrint = new Scanner();
    boolean x = scanPrint.initDevice();
    //Wait for the scanning thread to finish the Update the jLabel here to show
    //the fingerprint
} 
4

2 回答 2

3

不确定您使用的是 Swing 还是 Android 的 UI,但您想通知主事件调度线程(在 swing 中它被称为)。您将运行扫描线程,然后在完成时向 EDT 发送一条“消息”,其中包含您希望对按钮执行的操作。

Thread thread = new Thread(new Runnable(){
     public void run(){
         //scan
         SwingUtiltilies.invokeLater(new Runnable(){
              //here you can update the the jlabel icon
              public void run(){
                  jlabel.setText("Completed");
              }
         });
     } 
});

在 UI 开发中,无需等待操作完成,因为您总是希望 EDT 能够响应。

于 2013-07-09T12:37:04.750 回答
0

在扫描线程结束时,使用扫描SwingUtilities.invokeLater()结果更新 gui。

于 2013-07-09T12:35:24.347 回答