Java Utililty Methods BufferedImage Encode

List of utility methods to do BufferedImage Encode

Description

The list of methods to do BufferedImage Encode are organized into topic(s).

Method

Stringencode(RenderedImage image, String formatName)
encode
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(baos);) {
    ImageIO.write(image, formatName, bos);
    bos.flush();
    return Base64.getEncoder().encodeToString(baos.toByteArray());
} catch (IOException e) {
    throw new RuntimeException(e);
voidencodeAndWriteJPEGFile(File file, BufferedImage image, float quality)
encode And Write JPEG File
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage iioi = new IIOImage(image, null, null);
...
byte[]encodeBufferedImageAsJPEG(BufferedImage bi)
Stores BufferedImage as JPEG encoded bytestream
byte[] byteStream = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
ImageIO.write(bi, "jpeg", baos);
baos.flush();
byteStream = baos.toByteArray();
baos.close();
return byteStream;
StringencodeImage(BufferedImage image)
encode Image
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
    ImageIO.write(image, "png", bos);
    byte[] imageBytes = bos.toByteArray();
    imageString = DatatypeConverter.printBase64Binary(imageBytes);
    bos.close();
} catch (IOException e) {
...
StringencodeImage(BufferedImage image)
create an ascii-encoded representation of the given image
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", bos);
byte[] bytes = bos.toByteArray();
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(bytes);
StringencodeImageToBase64(BufferedImage image)
Encodes target image to a Base64 string
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);
...
byte[]encodeImageToPNGByteArray(BufferedImage image)
encode Image To PNG Byte Array
try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", bos);
    return bos.toByteArray();
} catch (Exception e) {
    e.printStackTrace(System.err);
return null;
...
byte[]encodeJPEG(BufferedImage image)
Performs in-memory encoding of a BufferedImage to a byte array.
ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
try {
    ImageIO.write(image, "jpeg", resultStream);
} catch (IOException e) {
    throw new Error(e);
return resultStream.toByteArray();
voidencodePNG(BufferedImage image, String pathAndFileName)
encode PNG
try {
    ImageIO.write(image, "png", new File(pathAndFileName));
} catch (Exception e) {
    e.printStackTrace(System.err);
StringencodeToImgElement(BufferedImage image, String formatName)
encode To Img Element
return encodedImageToImgElement(encode(image, formatName), formatName);