摄像机拍摄后,图像不会出现在ImageView中

时间:2018-03-01 16:53:43

标签: java opencv javafx

您好我已经开始使用OpenCv了,我想从相机拍摄照片并将其显示在已经在SceneBuilder中创建的ImageView中,但它不会出现。没有明显的错误。

private class Photo {
    private Mat matrix = null;
    private VideoCapture c = null;

    public void takePhoto() {
        Photo p = new Photo();
        WritableImage wii = p.capturePhoto();

        p.saveImage();

        image = new ImageView(wii);
    }

    private WritableImage capturePhoto() {
        WritableImage wiiU = null;
        System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
        c = new VideoCapture(0);
        Mat matrix = new Mat();
        c.read(matrix);

        if(c.isOpened()) {
            if(c.read(matrix)) {
                BufferedImage img = new BufferedImage(matrix.width(), matrix.height(), BufferedImage.TYPE_3BYTE_BGR);
                WritableRaster r = img.getRaster();
                DataBufferByte db = (DataBufferByte) r.getDataBuffer();
                byte[] bd = db.getData();
                matrix.get(0, 0, bd);
                this.matrix = matrix;

                wiiU = SwingFXUtils.toFXImage(img, null);
            }
        }
        return wiiU;
    }

    private void saveImage() {
        String file = ".//images//photo.png";
        Imgcodecs imageCodecs = new Imgcodecs();
        imageCodecs.imwrite(file, matrix);
    }
}

0 个答案:

没有答案