Here you can find the source of toByteArray(BufferedImage org)
Parameter | Description |
---|---|
org | a java.awt.image.BufferedImage object |
Parameter | Description |
---|---|
IOException | an exception |
public static byte[] toByteArray(BufferedImage org) throws IOException
//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; } }