Java BufferedImage to JPEG bufferedImage2Jpeg(final BufferedImage img)

Here you can find the source of bufferedImage2Jpeg(final BufferedImage img)

Description

Convierte una BufferedImage de AWT en una imagen JPEG.

License

Apache License

Parameter

Parameter Description
img <code>BufferedImage</code> de origen

Exception

Parameter Description
IOException Si hay problemas en el tratamiento de la imagen.

Return

Imagen JPEG

Declaration

public static byte[] bufferedImage2Jpeg(final BufferedImage img)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;

import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.spi.ServiceRegistry.Filter;
import javax.imageio.stream.MemoryCacheImageOutputStream;

public class Main {
    /** Convierte una <code>BufferedImage</code> de AWT en una imagen JPEG.
     * @param img <code>BufferedImage</code> de origen
     * @return Imagen JPEG/*from w w w .ja  va  2 s. c o m*/
     * @throws IOException Si hay problemas en el tratamiento de la imagen. */
    public static byte[] bufferedImage2Jpeg(final BufferedImage img)
            throws IOException {
        final IIORegistry registry = IIORegistry.getDefaultInstance();
        final Iterator<ImageWriterSpi> services = registry
                .getServiceProviders(ImageWriterSpi.class, new Filter() {

                    @Override
                    public boolean filter(final Object provider) {
                        if (!(provider instanceof ImageWriterSpi)) {
                            return false;
                        }
                        final ImageWriterSpi writerSPI = (ImageWriterSpi) provider;
                        final String[] formatNames = writerSPI
                                .getFormatNames();
                        for (final String formatName : formatNames) {
                            if (formatName.equalsIgnoreCase("JPEG")) { //$NON-NLS-1$
                                return true;
                            }
                        }
                        return false;
                    }
                }, true);
        if (!services.hasNext()) {
            throw new IOException(
                    "No hay un proveedor para el manejo de JPEG"); //$NON-NLS-1$
        }
        final ImageWriterSpi writerSpi = services.next();
        final ImageWriter writer = writerSpi.createWriterInstance();

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(
                baos);
        writer.setOutput(os);

        final JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
        jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        jpegParams.setCompressionQuality(1f);

        writer.write(null, new IIOImage(img, null, null), jpegParams);

        final byte[] ret = baos.toByteArray();
        os.close();

        return ret;
    }
}

Related

  1. imageToJpeg(BufferedImage image)