Java BufferedImage to Base64 String imageToBase64String(BufferedImage image, String type)

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

Description

image To Base String

License

Mozilla Public License

Declaration

public static String imageToBase64String(BufferedImage image, String type) 

Method Source Code


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

Related

  1. imageToBase64(BufferedImage image)
  2. imageToBase64String(RenderedImage image, String type)