将Mat转换为PNG图片

问题描述

我正在开发用于从纸上识别数独并将其显示在屏幕上的应用程序。由于我使用的是OpenCV,因此我尝试将Mat对象转换为PNG图像。我尝试使用此代码

private static void Mat2BufferedImage(Mat m,String s){
    int type = BufferedImage.TYPE_BYTE_GRAY;
    if ( m.channels() > 1 ) {
        type = BufferedImage.TYPE_3BYTE_BGR;
    }
    int bufferSize = m.channels()*m.cols()*m.rows();
    byte [] b = new byte[bufferSize];
    m.get(0,b);
    BufferedImage image = new BufferedImage(m.cols(),m.rows(),type);
    final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    System.arraycopy(b,targetPixels,b.length);
    File f = new File("resources/img-" + s + ".png");
    try {
        ImageIO.write(image,"PNG",f);
    } catch (IOException e) {
        e.printstacktrace();
    }
}

,但由于不再支持BufferedImage而无法执行。还有其他方法吗?我对OpenCV不太满意。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)