Java BufferedImage to Byte Array toByteArray(final BufferedImage image, final String format)

Here you can find the source of toByteArray(final BufferedImage image, final String format)

Description

Converts an image to a byte array.

License

Apache License

Parameter

Parameter Description
image The image to convert
format The image format

Return

The image data

Declaration

public static byte[] toByteArray(final BufferedImage image, final String format) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2016 Johannes Boczek//from  w ww.  ja v a 2  s. c o  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

public class Main {
    /**
     * Converts an image to a byte array.
     * @param image The image to convert
     * @param format The image format
     * @return The image data
     */
    public static byte[] toByteArray(final BufferedImage image, final String format) {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            ImageIO.write(image, "PNG", out);
        } catch (final IOException ex) {
            ex.printStackTrace();
        }

        final byte[] data = out.toByteArray();

        return data;
    }

    /**
     * Converts an image to a byte array.
     * Uses the PNG image format.
     * @param image The image to convert
     * @return The image data
     */
    public static byte[] toByteArray(final BufferedImage image) {
        return toByteArray(image, "PNG");
    }

    /**
     * Converts an image to a byte array.
     * Uses the JPEG image format.
     * The quality can be between 1 (best) and 0 (highest compression)
     * @param image The image that should be converted
     * @param quality The quality of the image
     * @return The image as a byte array
     */
    public static byte[] toByteArray(final BufferedImage image, final float quality) {
        final Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("JPG");
        final ImageWriter writer = writers.next();
        final ImageWriteParam param = writer.getDefaultWriteParam();
        final ByteArrayOutputStream out = new ByteArrayOutputStream();

        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality);

        try {
            final ImageOutputStream imageOut = ImageIO.createImageOutputStream(out);

            writer.setOutput(imageOut);
            writer.write(image);

            return out.toByteArray();
        } catch (final IOException ex) {
            ex.printStackTrace();
        }

        return null;
    }
}

Related

  1. toByteArray(BufferedImage image, float quality)
  2. toByteArray(BufferedImage image, String extension)
  3. toByteArray(BufferedImage image, String formatName)
  4. toByteArray(BufferedImage img, String imageFileType)
  5. toByteArray(BufferedImage org)
  6. toByteArray(RenderedImage image)
  7. toByteBuffer(BufferedImage img)
  8. toBytes(final BufferedImage image)