Java BufferedImage Encode encodeJPEG(BufferedImage image)

Here you can find the source of encodeJPEG(BufferedImage image)

Description

Performs in-memory encoding of a BufferedImage to a byte array.

License

Open Source License

Parameter

Parameter Description
image the image to encode

Return

a newly allocated byte array that contains the encoded image

Declaration

public static byte[] encodeJPEG(BufferedImage image) 

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;

public class Main {
    /**/* ww w .  j av a  2s .  com*/
     * Performs in-memory encoding of a BufferedImage to a byte array.
     *
     * @param image the image to encode
     * @return a newly allocated byte array that contains the encoded image
     */
    public static byte[] encodeJPEG(BufferedImage image) {
        ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "jpeg", resultStream);
        } catch (IOException e) {
            throw new Error(e);
        }
        return resultStream.toByteArray();
    }
}

Related

  1. encodeBufferedImageAsJPEG(BufferedImage bi)
  2. encodeImage(BufferedImage image)
  3. encodeImage(BufferedImage image)
  4. encodeImageToBase64(BufferedImage image)
  5. encodeImageToPNGByteArray(BufferedImage image)
  6. encodePNG(BufferedImage image, String pathAndFileName)
  7. encodeToImgElement(BufferedImage image, String formatName)