将Mat转换为PNG图片

时间:2020-10-15 16:18:35

标签: java android opencv png bufferedimage

我正在开发用于从纸上识别数独并将其显示在屏幕上的应用程序。由于我使用的是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,0,b);
    BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
    final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    System.arraycopy(b, 0, targetPixels, 0, b.length);
    File f = new File("resources/img-" + s + ".png");
    try {
        ImageIO.write(image, "PNG", f);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

0 个答案:

没有答案
相关问题