Here you can find the source of toByteArray(BufferedImage img, String imageFileType)
private static byte[] toByteArray(BufferedImage img, String imageFileType)
//package com.java2s; /******************************************************************************* * Copyright (c) 2014 Open Door Logistics (www.opendoorlogistics.com) * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v3 * which accompanies this distribution, and is available at http://www.gnu.org/licenses/lgpl.txt ******************************************************************************/ import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import javax.imageio.ImageIO; public class Main { private static byte[] toByteArray(BufferedImage img, String imageFileType) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {/* w w w .ja va 2 s. c om*/ ImageIO.write(img, imageFileType, baos); return baos.toByteArray(); } catch (Throwable e) { throw new RuntimeException(); } } }