Here you can find the source of toByteBuffer(BufferedImage img)
Parameter | Description |
---|---|
IOException | an exception |
public static ByteBuffer[] toByteBuffer(BufferedImage img) throws IOException
//package com.java2s; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.ByteBuffer; public class Main { /**//from w ww .j ava 2 s . c om * Converts a BufferedImage to a ByteBuffer. * * @throws IOException */ public static ByteBuffer[] toByteBuffer(BufferedImage img) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(img, "pnm", baos); ByteBuffer bb = ByteBuffer.allocateDirect(1); bb.put(baos.toByteArray()); bb.rewind(); return (new ByteBuffer[] { bb }); } /** * Converts a BufferedImage to a ByteBuffer. * * @throws IOException */ public static byte[] toByteArray(BufferedImage img) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(img, "pnm", baos); return (baos.toByteArray()); } }