在JavaCV中将字节数组转换为Mat

时间:2016-11-11 18:34:42

标签: scala opencv javacv

像这样:

import org.bytedeco.javacpp.opencv_core.Mat
import org.bytedeco.javacpp.{opencv_core, opencv_imgcodecs}

val jpegRawBytes: Array[Byte] = ???  // this is given, not a problem

val matRaw = new Mat(1, jpegRawBytes.length, opencv_core.CV_8UC1)
??? // mat.put(jpegRawBytes)
val matImg = opencv_imgcodecs.imdecode(matRaw, opencv_imgcodecs.IMREAD_COLOR)

如何将???放入Array[Byte]

这是一个,但它看起来非常低效,不得不为每个字节调用Mat

put

1 个答案:

答案 0 :(得分:0)

 public static Mat toMat(BufferedImage bi) {
    OpenCVFrameConverter.ToIplImage cv = new OpenCVFrameConverter.ToIplImage();
    Java2DFrameConverter jcv = new Java2DFrameConverter();
    return cv.convertToMat(jcv.convert(bi));
}

public static Mat toMat(byte[] bs) {
    return toMat(toBufferedImage(bs));
}

public static BufferedImage toBufferedImage(byte[] bs) {
    try {
        return ImageIO.read(new ByteArrayInputStream(bs));
    } catch (IOException ex) {
        Logger.getLogger(CEIUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}