Java Byte Array to BufferedImage bytesToBufferedImage(final byte[] imageData)

Here you can find the source of bytesToBufferedImage(final byte[] imageData)

Description

Converts an image as an array of bytes to a BufferedImage

License

Open Source License

Parameter

Parameter Description
imageData The image as an array of bytes

Exception

Parameter Description
IOException If an error occurs in reading the image

Return

A

Declaration

public static BufferedImage bytesToBufferedImage(final byte[] imageData) throws IOException 

Method Source Code


//package com.java2s;
/*  Copyright (C) 2014 Reinventing Geospatial, Inc
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.//from   w  ww.jav  a2  s  . c om
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>,
 *  or write to the Free Software Foundation, Inc., 59 Temple Place -
 *  Suite 330, Boston, MA 02111-1307, USA.
 */

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
    /**
     * Converts an image as an array of bytes to a {@link BufferedImage}
     *
     * @param imageData
     *             The image as an array of bytes
     * @return A {@link BufferedImage}
     * @throws IOException If an error occurs in reading the image
     */
    public static BufferedImage bytesToBufferedImage(final byte[] imageData) throws IOException {
        if (imageData == null) {
            throw new IllegalArgumentException("Image data may not be null");
        }

        try (ByteArrayInputStream imageInputStream = new ByteArrayInputStream(imageData)) {
            final BufferedImage bufferedImage = ImageIO.read(imageInputStream);

            if (bufferedImage == null) {
                throw new IOException("Image data is corrupt or in an unknown format");
            }

            return bufferedImage;
        }
    }
}

Related

  1. byteArrayToBufferedImage(byte[] array)
  2. byteArrayToBufferedImage(byte[] image)
  3. byteArrayToImage(byte[] imagebytes)
  4. byteArrayToImage(byte[] imageData)
  5. bytesToImage(byte[] buf)
  6. bytesToImage(byte[] imageData)
  7. bytesToImage(byte[] imageData)
  8. byteToBufferedImage(byte[] buff)