Here you can find the source of imageToBytes(BufferedImage img, String formatName)
Parameter | Description |
---|---|
img | - a RenderedImage to be written. |
formatName | - a String containing the informal name of the format. |
public static byte[] imageToBytes(BufferedImage img, String formatName)
//package com.java2s; //License from project: Open Source License import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; public class Main { /**//www.jav a 2 s .c om * Returns the image into an array of bytes. * * @param img - a RenderedImage to be written. * @param formatName - a String containing the informal name of the format. * @return */ public static byte[] imageToBytes(BufferedImage img, String formatName) { byte[] imgBytes = null; ByteArrayOutputStream stream = null; try { stream = new ByteArrayOutputStream(); ImageIO.write(img, formatName, stream); stream.flush(); imgBytes = stream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } return imgBytes; } }