Loading

BufferedImage转Base64编码方法封装(JAVA版)

 该方法可以将BufferedImage格式转Base64编码。


    /**
     * BufferedImage转base64
     * @param bufferedImage
     * @return
     */
    public String bufferedImageToBase64(BufferedImage bufferedImage) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            // 设置图片格式
            ImageIO.write(bufferedImage, "jpg", stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes = Base64.encodeBase64(stream.toByteArray());
        String base64 = new String(bytes);
        return "data:image/jpeg;base64," + base64;
    }

posted @ 2023-02-20 04:14  Roc-xb  阅读(313)  评论(0编辑  收藏  举报

易微帮源码


易微帮官网