1
votes

In my work I have to get image matrix from the encoded string of an image. I am using OpenCV and JAVA.

Can anyone tell me how to do this?

Code:

BufferedImage originalImage;
                try {
                    originalImage =    ImageIO.read(new File("D:\\testimg.jpg"));
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    ImageIO.write(originalImage, "jpeg", baos);
                    baos.flush();
                    imageInByte = baos.toByteArray();

                    baos.close();

                    String imageDataString = encodeImage(imageInByte);
} catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception ex){
                    ex.printStackTrace();
                }

Now I have the string of image. The imread() of opencv takes filepath as input param and create Mat, but I have to create the Mat from this imageDataString .

Thanks, Surodip

3
show some of your code - mnagel
Why would you do that !? you mean " but I have to create the Mat from this imageDataString " that you have raw images a "imageDataString" ? - lastjeef

3 Answers

1
votes

The opencv function used to decode images from its encoded forms is imdecode. See http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html

1
votes

try this code to convert base64 string to mat object in opencv java

byte[] imgbytes = DatatypeConverter.parseBase64Binary(fileBase64);

Mat image = Imgcodecs.imdecode(new MatOfByte(imgbytes), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);

-1
votes

you can try this

    File image = new File("assets/imageparser/imageA.jpg");
    BufferedImage buffImage = ImageIO.read(image);

    byte[] data = ((DataBufferByte) buffImage.getRaster().getDataBuffer()).getData();
    Mat mat = new Mat(buffImage.getHeight(), buffImage.getWidth(), CvType.CV_8UC3);
    mat.put(0, 0, data);