Java BufferedImage to Byte Array imageToByteArray(BufferedImage o)

Here you can find the source of imageToByteArray(BufferedImage o)

Description

Ein byte-Array (BLOB) aus einem Image erzeugen

License

Open Source License

Parameter

Parameter Description
o BufferedImage

Return

byte[]

Declaration

public static byte[] imageToByteArray(BufferedImage o) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * HELIUM V, Open Source ERP software for sustained success
 * at small and medium-sized enterprises.
 * Copyright (C) 2004 - 2014 HELIUM V IT-Solutions GmbH
 * /*from   ww w .j  a  va 2 s .  c o m*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published 
 * by the Free Software Foundation, either version 3 of theLicense, or 
 * (at your option) any later version.
 * 
 * According to sec. 7 of the GNU Affero General Public License, version 3, 
 * the terms of the AGPL are supplemented with the following terms:
 * 
 * "HELIUM V" and "HELIUM 5" are registered trademarks of 
 * HELIUM V IT-Solutions GmbH. The licensing of the program under the 
 * AGPL does not imply a trademark license. Therefore any rights, title and
 * interest in our trademarks remain entirely with us. If you want to propagate
 * modified versions of the Program under the name "HELIUM V" or "HELIUM 5",
 * you may only do so if you have a written permission by HELIUM V IT-Solutions 
 * GmbH (to acquire a permission please contact HELIUM V IT-Solutions
 * at trademark@heliumv.com).
 * 
 * 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contact: developers@heliumv.com
 ******************************************************************************/

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.imageio.ImageIO;

public class Main {
    /**
     * Ein byte-Array (BLOB) aus einem Image erzeugen
     * 
     * @param o
     *            BufferedImage
     * @return byte[]
     */
    public static byte[] imageToByteArray(BufferedImage o) {
        if (o != null) {
            BufferedImage image = (BufferedImage) o;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                boolean converted = ImageIO.write(image, "jpeg", baos);
                if (!converted) {

                }
                byte[] b = baos.toByteArray();
                return b;
            } catch (IOException e) {
                throw new IllegalStateException(e.toString());
            } finally {
                closeIgnoreIOEx(baos);
            }
        }
        return new byte[0];
    }

    public static void closeIgnoreIOEx(ObjectOutputStream oos) {
        if (null == oos)
            return;

        try {
            oos.close();
        } catch (IOException ex) {
        }
    }

    public static void closeIgnoreIOEx(ByteArrayOutputStream baos) {
        if (null == baos)
            return;

        try {
            baos.close();
        } catch (IOException ex) {
        }
    }
}

Related

  1. image2Bytes(BufferedImage image)
  2. image2Bytes(File f)
  3. imageToByte(BufferedImage bi, String format)
  4. imageToByte(BufferedImage image)
  5. imageToByte(BufferedImage img)
  6. imageToBytes(BufferedImage image, String encoding)
  7. imageToBytes(BufferedImage image, String imageFormat)
  8. imageToBytes(BufferedImage img, String formatName)
  9. toArrayByte(BufferedImage image)