如何获取BMP格式图像的Base64字符串

问题描述 投票:0回答:1

我正在尝试将任何图像格式转换为 BMP 格式,然后返回转换后的 BMP 字节的 base64 字符串。 但是,当我返回 BMP 图像 base64 字符串时,它变为空白。我什至尝试在控制台上打印它,但字符串仍然没有在控制台上打印。在调试模式下,我可以看到图像正在转换为 Base64 字符串,但它没有被打印或传递给进一步的控制。 下面是代码片段:

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

但不知道为什么返回字符串没有添加到响应中,并尝试在控制台中打印它,仍然没有输出 如果我调试它,则会生成 base64 字符串,但它不会通过。 谁能指出我的错误或帮助我。

java base64 bmp
1个回答
0
投票

问题解决了!能够将任何图像转换为bmp,然后将转换后的BMP格式图像转换为base64string。

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;

    }
© www.soinside.com 2019 - 2024. All rights reserved.