I have to convert an IplImage to a BufferedImage... what should be a simple task is becoming very intricate for me. I'm using JavaCv 1.0 on Mac OSX (which links with OpenCV 3.0).
In the old JavaCV API there was the IplImage method #getBufferedImage, but I can't find it anymore in new 1.0 API. So I tried to convert IplImage to byte array and byte array to BufferedImage. I found this solution to perform such conversion:
IplImage inputImg = cvLoadImage(imgName); //imgName is a JPEG absolute path byte[] imageData = IplImageToByteArray( inputImg); //defined below ByteArrayInputStream bais = new ByteArrayInputStream(imageData); BufferedImage inputImage = ImageIO.read(bais); //Always return null
where IplImageToByteArray can be defined in one of the following ways:
public static byte[] IplImageToByteArray(IplImage src) { ByteBuffer byteBuffer = src.getByteBuffer(); byte[] barray = new byte[byteBuffer.remaining()]; byteBuffer.get(barray); return barray; } public static byte[] IplImageToByteArray2(IplImage src) { byte[] barray = new byte[src.imageSize()]; src.getByteBuffer().get(barray); return barray; }
The returned byte array is the same in both cases, but ImageIO.read returns always null value. I've no idea about what I'm getting wrong.