Here you can find the source of encodeImageToBase64(BufferedImage image)
Parameter | Description |
---|---|
image | BufferedImage to encode to String |
public static String encodeImageToBase64(BufferedImage image)
//package com.java2s; //License from project: Open Source License import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import org.apache.commons.codec.binary.Base64; public class Main { /**/*from w w w. j av a 2 s .co m*/ * Encodes target image to a Base64 string * * @param image * BufferedImage to encode to String * @return Base64 encoded String of image */ public static String encodeImageToBase64(BufferedImage image) { String encodedImage = ""; Base64 encoder = new Base64(); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "png", baos); baos.flush(); byte[] encodedBytes = encoder.encode(baos.toByteArray()); encodedImage = new String(encodedBytes); baos.close(); } catch (IOException e) { e.printStackTrace(); } return encodedImage; } }