Here you can find the source of imageToBase64String(BufferedImage image, String type)
public static String imageToBase64String(BufferedImage image, String type)
//package com.java2s; //License from project: Mozilla Public License import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.util.Base64; public class Main { public static String imageToBase64String(BufferedImage image, String type) { String imageString = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {//from ww w .java 2 s.c om ImageIO.write(image, type, bos); byte[] imageBytes = bos.toByteArray(); imageString = Base64.getEncoder().encodeToString(imageBytes); bos.close(); } catch (IOException e) { e.printStackTrace(); } return imageString; } }