直接将处理好的 BufferedImage 对象传入即可
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流 ImageIO.write(bufferedImage, "png", baos);//写入流中 byte[] bytes = baos.toByteArray();//转换成字节 BASE64Encoder encoder = new BASE64Encoder(); String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串 png_base64 = png_base64.replaceAll(" ", "").replaceAll(" ", "");//删除 // ImageIO.write(bufferedImage, "png", new File("D:/qrcode1.png")); System.out.println("值为:"+"data:image/jpg;base64,"+png_base64);
注意:png_base64 字段,最终要拼接上
data:image/jpg;base64,
才能正常给前端转成图片!