Java BufferedImage Encode encodeBufferedImageAsJPEG(BufferedImage bi)

Here you can find the source of encodeBufferedImageAsJPEG(BufferedImage bi)

Description

Stores BufferedImage as JPEG encoded bytestream

License

Open Source License

Parameter

Parameter Description
bi a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static byte[] encodeBufferedImageAsJPEG(BufferedImage bi) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   w ww  .j a v  a  2  s  .  com*/
 *     Orbit, a versatile image analysis software for biological image-based quantification.
 *     Copyright (C) 2009 - 2016 Actelion Pharmaceuticals Ltd., Gewerbestrasse 16, CH-4123 Allschwil, Switzerland.
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

public class Main {
    /**
     * Stores BufferedImage as JPEG encoded bytestream
     *
     * @param bi
     * @return
     * @throws IOException
     */
    public static byte[] encodeBufferedImageAsJPEG(BufferedImage bi) throws IOException {

        byte[] byteStream = null;

        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
        ImageIO.write(bi, "jpeg", baos);
        baos.flush();
        byteStream = baos.toByteArray();
        baos.close();

        return byteStream;
    }
}

Related

  1. encode(RenderedImage image, String formatName)
  2. encodeAndWriteJPEGFile(File file, BufferedImage image, float quality)
  3. encodeImage(BufferedImage image)
  4. encodeImage(BufferedImage image)
  5. encodeImageToBase64(BufferedImage image)
  6. encodeImageToPNGByteArray(BufferedImage image)