Java BufferedImage to Byte Array toByteArray(BufferedImage org)

Here you can find the source of toByteArray(BufferedImage org)

Description

Converts a BufferedImage to a byte array.

License

Open Source License

Parameter

Parameter Description
org a java.awt.image.BufferedImage object

Exception

Parameter Description
IOException an exception

Return

a byte array with the contents of the image

Declaration

public static byte[] toByteArray(BufferedImage org) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w w w .j  a v  a 2s  .c o m*/
 * Copyright 2007-2012 The Europeana Foundation
 *
 *  Licenced under the EUPL, Version 1.1 (the "Licence") and subsequent versions as approved 
 *  by the European Commission;
 *  You may not use this work except in compliance with the Licence.
 *  
 *  You may obtain a copy of the Licence at:
 *  http://joinup.ec.europa.eu/software/page/eupl
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under 
 *  the Licence is distributed on an "AS IS" basis, without warranties or conditions of 
 *  any kind, either express or implied.
 *  See the Licence for the specific language governing permissions and limitations under 
 *  the Licence.
 */

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
    /**
     * Converts a BufferedImage to a byte array.
     * 
     * @param org a java.awt.image.BufferedImage object
     * @return a byte array with the contents of the image
     * @throws IOException
     */
    public static byte[] toByteArray(BufferedImage org) throws IOException {
        if (org != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(org, "jpg", baos);
            baos.flush();
            byte[] imageInByte = baos.toByteArray();
            baos.close();
            return imageInByte;
        }
        return null;
    }
}

Related

  1. toByteArray(BufferedImage image)
  2. toByteArray(BufferedImage image, float quality)
  3. toByteArray(BufferedImage image, String extension)
  4. toByteArray(BufferedImage image, String formatName)
  5. toByteArray(BufferedImage img, String imageFileType)
  6. toByteArray(final BufferedImage image, final String format)
  7. toByteArray(RenderedImage image)
  8. toByteBuffer(BufferedImage img)
  9. toBytes(final BufferedImage image)