Java BufferedImage to Byte Array toByteArray(BufferedImage image, float quality)

Here you can find the source of toByteArray(BufferedImage image, float quality)

Description

to Byte Array

License

Open Source License

Declaration

public static byte[] toByteArray(BufferedImage image, float quality) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *              Crimson Extended Administration Tool (CrimsonXAT)              *
 *                   Copyright (C) 2015 Subterranean Security                  *
 *                                                                             *
 *     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.                    *
 *                                                                             *
 *       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      *
 *******************************************************************************/

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

public class Main {
    private static ImageWriter writer = null;
    private static ImageWriteParam param = null;

    public static byte[] toByteArray(BufferedImage image, float quality) {
        try {/*from ww w  .j  av a 2  s  . c  om*/
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            if (quality == -1)
                ImageIO.write(image, "jpeg", out); // write without compression
            else
                write(image, quality, out); // write with compression
            return out.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return new byte[] {};
        }
    }

    public static void write(BufferedImage image, float quality, OutputStream out) throws IOException {
        ImageOutputStream ios = ImageIO.createImageOutputStream(out);
        writer.setOutput(ios);
        param.setCompressionQuality(quality);
        writer.write(null, new IIOImage(image, null, null), param);
        ios.close();
        // writer.dispose();
    }
}

Related

  1. imageToBytes(BufferedImage image, String encoding)
  2. imageToBytes(BufferedImage image, String imageFormat)
  3. imageToBytes(BufferedImage img, String formatName)
  4. toArrayByte(BufferedImage image)
  5. toByteArray(BufferedImage image)
  6. toByteArray(BufferedImage image, String extension)
  7. toByteArray(BufferedImage image, String formatName)
  8. toByteArray(BufferedImage img, String imageFileType)
  9. toByteArray(BufferedImage org)