Java Utililty Methods BufferedImage to Byte Array

List of utility methods to do BufferedImage to Byte Array

  1. HOME
  2. Java
  3. B
  4. BufferedImage to Byte Array

Description

The list of methods to do BufferedImage to Byte Array are organized into topic(s).

Method

byte[] getByteArray(File file)
get Byte Array
BufferedImage originalImage = ImageIO.read(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);
baos.flush();
return baos.toByteArray();
byte[] getByteArray(RenderedImage image, String formatName)
get Byte Array
try {
 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 ImageIO.write(image, formatName, byteArrayOutputStream);
 return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
 throw new RuntimeException(e);
ByteBuffer getByteBuffer(BufferedImage bufferedImage)
get Byte Buffer
Raster raster = bufferedImage.getRaster();
DataBufferByte dataBufferByte = (DataBufferByte) raster.getDataBuffer();
byte[] data = dataBufferByte.getData();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length);
byteBuffer.order(ByteOrder.nativeOrder());
byteBuffer.put(data, 0, data.length);
byteBuffer.flip();
return byteBuffer;
...
BufferedImage getByteBufferToImage(ByteBuffer buffer, int width, int height)
Converts a ByteBuffer (Used in OpenGL) to a BufferedImage.
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < width; x++)
 for (int y = 0; y < height; y++) {
 int i = (x + (width * y)) * 4;
 int r = buffer.get(i) & 0xFF;
 int g = buffer.get(i + 1) & 0xFF;
 int b = buffer.get(i + 2) & 0xFF;
 img.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
...
byte[] getByteImage(BufferedImage imagem)
Captura os bytes de um BufferedImage
if (imagem == null)
 return null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(imagem, "jpg", baos);
byte[] bytes = baos.toByteArray();
return bytes;
byte[] getBytes(BufferedImage image)
get Bytes
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
 ImageIO.write(image, "png", outputStream);
} catch (IOException e) {
 throw new RuntimeException(e);
return outputStream.toByteArray();
byte[] getBytes(final InputStream is, final int bufferSize)
get Bytes
final ByteArrayOutputStream out = new ByteArrayOutputStream(bufferSize);
try {
 byte[] buffer = new byte[bufferSize];
 int len;
 while ((len = is.read(buffer)) >= 0) {
 out.write(buffer, 0, len);
} finally {
...
byte[] getBytesFromBufferedImage(BufferedImage bi, String format)
Get bytes from a specified BufferedImage, with the specified format
ByteArrayOutputStream buff = new ByteArrayOutputStream();
try {
 ImageIO.write(bi, format, buff);
 byte[] bytes = buff.toByteArray();
 buff.close();
 return bytes;
} catch (IOException ex) {
 ex.printStackTrace();
...
byte[] getBytesFromImage(String fileIn, String fileOut)
get Bytes From Image
byte[] photo = null;
try {
 File file = new File(fileIn);
 BufferedImage imagem = ImageIO.read(file);
 int new_w = 320, new_h = 240;
 BufferedImage new_img = new BufferedImage(new_w, new_h, BufferedImage.SCALE_DEFAULT);
 Graphics2D g = new_img.createGraphics();
 g.drawImage(imagem, 0, 0, new_w, new_h, null);
...
int getBytesPerPixel(int bufferedImageType)
get Bytes Per Pixel
switch (bufferedImageType) {
case BufferedImage.TYPE_INT_ARGB:
case BufferedImage.TYPE_INT_ARGB_PRE:
case BufferedImage.TYPE_INT_BGR:
case BufferedImage.TYPE_4BYTE_ABGR:
case BufferedImage.TYPE_4BYTE_ABGR_PRE:
 return 4;
case BufferedImage.TYPE_3BYTE_BGR:
...


AltStyle によって変換されたページ (->オリジナル) /