Here you can find the source of encode(RenderedImage image, String formatName)
public static String encode(RenderedImage image, String formatName)
//package com.java2s; //License from project: Open Source License 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 encode(RenderedImage image, String formatName) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(baos);) { ImageIO.write(image, formatName, bos); bos.flush();//w w w . j ava 2 s . c o m return Base64.getEncoder().encodeToString(baos.toByteArray()); } catch (IOException e) { throw new RuntimeException(e); } } }