在fxml中使用java,javafx webcapture zxing在fxml中集成

package webcamfx;

public class FXMLDocumentController implements Initializable, Runnable, ThreadFactory {

private Webcam webcam = Webcam.getDefault();

private WebcamPanel webcamPanel = new WebcamPanel(webcam, false);

private Executor executor = Executors.newSingleThreadExecutor(this);

@FXML

private SwingNode swingnode;

private TextField textfield;

//@Override

public void initialize(URL url, ResourceBundle rb) {

//super();

//SwingNode swingnode = new SwingNode();

swingnode.setContent(webcamPanel);

webcamPanel.start();

executor.execute(this);

}

@Override

public void run() {

do {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

Result result = null;

BufferedImage image = null;

if (webcam.isOpen()) {

if ((image = webcam.getImage()) == null) {

continue;

}

LuminanceSource source = new BufferedImageLuminanceSource(image);

BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

try {

result = new MultiFormatReader().decode(bitmap);

} catch (NotFoundException e) {

// fall thru, it means there is no QR code in image

}

}

if (result != null) {

System.out.println(result.getText());

textfield.setText(result.getText());

}

} while (true);

}

@Override

public Thread newThread(Runnable r) {

Thread t = new Thread(r, "example-runner");

t.setDaemon(true);

return t;

}

}

运行和输出:

java.util.concurrent中java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)上的webcamfx.FXMLDocumentController.run(FXMLDocumentController.java:90)中的线程“example-runner”java.lang.NullPointerException中的异常 . ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)at java.lang.Thread.run(Thread.java:745)

this line 90 : textfield.setText(result.getText());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值