-2

I am trying to convert any image format to a BMP format and then returning back the base64 String of the converted BMP byte. However when I am returning the BMP image base64 string, it is coming blank. I even tried printing it on console but still the string is not getting printed over the console. In debug mode I can see the image is getting converted to base64 string but its not getting printed or passed to further control. Below is the code snippet:

    InputStream in = new ByteArrayInputStream(content);
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {
        BufferedImage image = ImageIO.read(in);
        ImageIO.write(image, "bmp", out);
        byte[] bt=out.toByteArray();
        in.close();
        out.flush();
        out.close();
        return new String (Base64.getEncoder().encodeToString(bt));
    } catch (IOException | NullPointerException e) {
        System.out.println("error while converting image"+e);
        e.printStackTrace();
    }

but dont know why return string is not getting added in response and tried pritintin it in console as well, still no output If i debug it the base64 string is getting generated but its not gteting pass. can anyone point out my mistake or help me on this.

Ramm
  • 27
  • 1
  • What is the return value of `ImageIO.write(...)`? It returns a `boolean` indicating success. If it's `false`, nothing was written, `bt` will be empty, and so will your Base64 string. PS: You don't need the `new String(...)` around the result from `encodeToString(byte[])`, it's already a `String`. – Harald K Apr 20 '21 at 06:55
  • If the problem is that your Base64 string is not getting written to the response, there's no attempt at that in the code you posted. Please add the relevant code (and remove the irrelevant) to the question. – Harald K Apr 20 '21 at 06:58
  • @HaraldK above code is relevant only. ImageIO.write() will write the byte in out stream. Even if i added the boolean check, still the response is same. I am trying to print the returned encodedstring to console. – Ramm Apr 21 '21 at 05:14
  • Of course, the response would be the same. But you would know for certain if a value was to be expected... And there is no printing of the Base64 value to the console in your code, so I don't see how the above can be the only code that is relevant..? Anyway, your code works for me (given reasonable input). – Harald K Apr 21 '21 at 14:33

1 Answers1

0

Issue got resolved! Able to convert any image to bmp and then to base64string of the converted BMP format image.

private StringBuilder convertToBMP(byte[] content, String id, String filetype) throws IOException {

        String base64image = null;

        InputStream in = new ByteArrayInputStream(content);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BufferedReader br = null;
        FileWriter writer = null;
        StringBuilder sb = null;
        try {

            BufferedImage image = ImageIO.read(in);

            if (filetype.equalsIgnoreCase("image/png")) {
                BufferedImage newBufferedImage = new BufferedImage(image.getWidth(), image.getHeight(),
                        BufferedImage.TYPE_INT_RGB);

                newBufferedImage.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);

                image = newBufferedImage;
            }

            if (!filetype.equalsIgnoreCase("image/bmp")) {
                boolean b = ImageIO.write(image, "bmp", out);
                byte[] bmpbyte = out.toByteArray();
                if (b && bmpbyte.length > 0) {
                    out.flush();
                    base64image = Base64.getEncoder().encodeToString(bmpbyte);
                    sb = new StringBuilder();
                    sb.append(base64image);
                }
            } else if (filetype.equalsIgnoreCase("image/bmp")) {
                base64image = Base64.getEncoder().encodeToString(content);
                sb = new StringBuilder();
                sb.append(base64image);
            }

        } catch (IOException | NullPointerException e) {
            // TODO Auto-generated catch block
            System.out.println("error while converting image" + e);
            e.printStackTrace();
        } finally {
            try {
                if (in != null)
                    in.close();
                if (out != null)
                    out.close();
                if (br != null)
                    br.close();
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return sb;

    }
Ramm
  • 27
  • 1