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

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

Description

encode To String

License

Open Source License

Declaration

public static String encodeToString(BufferedImage image, String type) 

Method Source Code

//package com.java2s;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import sun.misc.BASE64Encoder;

public class Main {
    public static String encodeToString(File image, String type) {

        BufferedImage originalImage = null;
        try {//www .  j  a  v a 2  s.c o  m
            originalImage = ImageIO.read(image);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return encodeToString(originalImage, type);
    }

    public static String encodeToString(BufferedImage image, String type) {
        String imageString = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        try {
            ImageIO.write(image, type, bos);
            byte[] imageBytes = bos.toByteArray();

            BASE64Encoder encoder = new BASE64Encoder();
            imageString = encoder.encode(imageBytes);

            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imageString;
    }
}

Related

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