Java BufferedImage Encode encodeImageToBase64(BufferedImage image)

Here you can find the source of encodeImageToBase64(BufferedImage image)

Description

Encodes target image to a Base64 string

License

Open Source License

Parameter

Parameter Description
image BufferedImage to encode to String

Return

Base64 encoded String of image

Declaration

public static String encodeImageToBase64(BufferedImage image) 

Method Source Code

//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;
    }
}

Related

  1. encode(RenderedImage image, String formatName)
  2. encodeAndWriteJPEGFile(File file, BufferedImage image, float quality)
  3. encodeBufferedImageAsJPEG(BufferedImage bi)
  4. encodeImage(BufferedImage image)
  5. encodeImage(BufferedImage image)
  6. encodeImageToPNGByteArray(BufferedImage image)
  7. encodeJPEG(BufferedImage image)
  8. encodePNG(BufferedImage image, String pathAndFileName)
  9. encodeToImgElement(BufferedImage image, String formatName)