Is BufferedImage TYPE_INT_RGB incompatible with Mat? [closed]

asked 2017-01-22 17:17:22 -0600

thatc0der gravatar image

I have a program that I am trying to capture video and I can get it working. I got the program to work by doing this initially.

byte [] dat;
if (dat == null || dat.length != w * h * 3)
        dat = new byte [w * h * 3];
    if (img == null || img.getWidth() != w || img.getHeight() != h || img.getType() != BufferedImage.TYPE_3BYTE_BGR)
        img = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);

The camera loads but I want an RGB filter. This is what I tried to to get my RGB filter so I changed my code to this.

import org.opencv.core.Core;
import org.opencv.core.Mat;

public class Mat2Image {
    Mat mat = new Mat();
    BufferedImage img;
    int[] dat;

    Mat2Image(){
    }

    public Mat2Image(Mat mat) {
        getSpace(mat);
    }

    public void getSpace(Mat mat) {
        this.mat = mat;
        int w = mat.cols(), h = mat.rows();
        if (dat == null || dat.length != w * h)
            dat = new int[w * h];
        if (img == null || img.getWidth() != w || img.getHeight() != h || img.getType() != BufferedImage.TYPE_INT_RGB)
            img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    }

    BufferedImage getImage(Mat mat) {
        getSpace(mat);
        mat.get(0, 0, dat);
        img.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), dat);
        return img;
    }
    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }
}

When I run the program it throws the exception java.lang.UnsupportedOperationException: Mat data type is not compatible: 16 I don't know what this means or what is causing it.

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-04 12:09:46.948869