Java BufferedImage to String encodeToString(BufferedImage image, String type)

Here you can find the source of encodeToString(BufferedImage image, String type)

Description

Encode image to string

License

Open Source License

Declaration

public static String encodeToString(BufferedImage image, String type) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;

public class Main {
    /**//from   w  ww  .j  a  v a2  s  .c o m
     * Encode image to string
     */
    public static String encodeToString(BufferedImage image, String type) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image, type, bos);

        byte[] imageBytes = bos.toByteArray();
        BASE64Encoder encoder = new BASE64Encoder();
        String imageString = encoder.encode(imageBytes);

        bos.close();
        return imageString;
    }
}

Related

  1. encodeToString(BufferedImage image, String type)
  2. encodeToString(BufferedImage image, String type)