Image doesn't appear in ImageView after it is taken by the camera

Refresh

November 2018

Views

29 time

2

Hello I've start using OpenCv and I wanted to take a photo from the camera and show it in an ImageView that was already created in SceneBuilder but it doesn't appear. There is no apparent error.

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 answers