【问题标题】:How do I get Base64 string of BMP formatted image如何获取 BMP 格式图像的 Base64 字符串
【发布时间】:2021-04-20 06:23:42
【问题描述】:

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

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

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

【问题讨论】:

  • ImageIO.write(...)的返回值是多少?它返回一个boolean 表示成功。如果是false,则没有写入任何内容,bt 将为空,您的 Base64 字符串也是如此。 PS:你不需要new String(...) 围绕来自encodeToString(byte[]) 的结果,它已经是String
  • 如果问题是您的 Base64 字符串未写入响应,则在您发布的代码中没有尝试这样做。请在问题中添加相关代码(并删除不相关的)。
  • @HaraldK 以上代码仅相关。 ImageIO.write() 会将字节写入输出流。即使我添加了布尔检查,响应仍然是相同的。我正在尝试将返回的编码字符串打印到控制台。
  • 当然,响应是一样的。但是您肯定会知道是否需要一个值......并且您的代码中没有将 Base64 值打印到控制台,所以我看不出上面的代码如何成为唯一相关的代码。 .?无论如何,你的代码对我有用(给定合理的输入)。

标签: java javax.imageio bmp tobase64string bytearrayinputstream


【解决方案1】:

问题已解决!可以将任意图片转成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;

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多