0

I am developing application for recognising Sudoku from paper and showing it on the screen. Since I am using OpenCV, I've tried to convert Mat object to PNG image. I tried to use this code:

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();
    }
}

, but I am unable since BufferedImage is not supported anymore. Is there any other way to do this? I am not so good with OpenCV.

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • 1
    What makes you think `BufferedImage` is not supported anymore? – jmizv Oct 15 '20 at 16:23
  • I've tried to find some solution, so this is what made me think that: https://stackoverflow.com/questions/33210065/how-can-i-import-java-awt-image-bufferedimage-in-android-studio/33210293 Again, I am not sure if that's right. – Andrija Randjelovic Oct 15 '20 at 16:35
  • Are you developing for Android? `BufferedImage` has *never* been supported (or available) on Android. It's a Java 2D class, only available (and supported) on Java platforms. – Harald K Oct 19 '20 at 07:01
  • @haraldK I am working in Android Studio, trying to develop Android application. It seems like I completely missed the point here... – Andrija Randjelovic Oct 22 '20 at 21:54
  • You could probably use the Android `Bitmap` class, which is kind of like `BufferedImage`, using `Bitmap.create(...)`, `copyPixelsFromBuffer(...)` and then `compress(...)` to write the contents to disk. – Harald K Oct 23 '20 at 08:12

0 Answers0