Here you can find the source of encodeToImgElement(BufferedImage image, String formatName)
public static String encodeToImgElement(BufferedImage image, String formatName)
//package com.java2s; //License from project: Open Source License import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Base64; import javax.imageio.ImageIO; public class Main { public static String encodeToImgElement(BufferedImage image, String formatName) { return encodedImageToImgElement(encode(image, formatName), formatName); }//from w w w. j a v a 2 s . com public static String encodedImageToImgElement(String encodedImage, String formatName) { return String.format("<img src='%s'/>", encodedImage); } public static String encode(RenderedImage image, String formatName) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(baos);) { ImageIO.write(image, formatName, bos); bos.flush(); return Base64.getEncoder().encodeToString(baos.toByteArray()); } catch (IOException e) { throw new RuntimeException(e); } } }