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)