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

Parameter

Parameter Description
image the image
type the type

Return

the string

Declaration

private static String encodeToString(BufferedImage image, String type) 

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 javax.xml.bind.DatatypeConverter;

public class Main {
    /**/*from  ww w.  jav a  2  s .c om*/
     * Encode to string.
     *
     * @param image the image
     * @param type the type
     * @return the string
     */
    private static String encodeToString(BufferedImage image, String type) {
        String imageString = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

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

            imageString = DatatypeConverter.printBase64Binary(imageBytes);

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

Related

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