-2

我正在尝试将任何图像格式转换为 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();
    }

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

4

1 回答 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;

    }
于 2021-05-09T10:57:30.747 回答