Example usage for javax.imageio ImageTypeSpecifier ImageTypeSpecifier

List of usage examples for javax.imageio ImageTypeSpecifier ImageTypeSpecifier

Introduction

In this page you can find the example usage for javax.imageio ImageTypeSpecifier ImageTypeSpecifier.

Prototype

public ImageTypeSpecifier(ColorModel colorModel, SampleModel sampleModel) 

Source Link

Document

Constructs an ImageTypeSpecifier directly from a ColorModel and a SampleModel .

Usage

From source file:ImageUtil.java

/**
 * store BufferedImage to file/*from w ww . j  a v a 2  s. co  m*/
 * @param image BufferedImage
 * @param outputFile output image file
 * @param quality quality of output image
 * @return true success, else fail
 */
public static boolean storeImage(BufferedImage image, File outputFile, float quality) {
    try {
        //reconstruct folder structure for image file output
        if (outputFile.getParentFile() != null && !outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }
        if (outputFile.exists()) {
            outputFile.delete();
        }
        //get image file suffix
        String extName = "png";
        //get registry ImageWriter for specified image suffix
        Iterator writers = ImageIO.getImageWritersByFormatName(extName);
        ImageWriter imageWriter = (ImageWriter) writers.next();
        //set image output params
        ImageWriteParam params = new JPEGImageWriteParam(null);
        params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        params.setCompressionQuality(quality);
        params.setProgressiveMode(javax.imageio.ImageWriteParam.MODE_DISABLED);
        params.setDestinationType(new ImageTypeSpecifier(IndexColorModel.getRGBdefault(),
                IndexColorModel.getRGBdefault().createCompatibleSampleModel(16, 16)));
        //writer image to file
        ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputFile);
        imageWriter.setOutput(imageOutputStream);
        imageWriter.write(null, new IIOImage(image, null, null), params);
        imageOutputStream.close();
        imageWriter.dispose();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:org.apache.flex.compiler.internal.embedding.transcoders.JPEGTranscoder.java

private byte[] bufferedImageToJPEG(ImageInfo imageInfo, int[] pixels) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(imageInfo.width, imageInfo.height,
            BufferedImage.TYPE_INT_ARGB);
    bufferedImage.setRGB(0, 0, imageInfo.width, imageInfo.height, pixels, 0, imageInfo.width);

    ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
    ImageWriteParam writeParam = writer.getDefaultWriteParam();
    ColorModel colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
    ImageTypeSpecifier imageTypeSpecifier = new ImageTypeSpecifier(colorModel,
            colorModel.createCompatibleSampleModel(1, 1));
    writeParam.setDestinationType(imageTypeSpecifier);
    writeParam.setSourceBands(new int[] { 0, 1, 2 });
    writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

    float q = 1.0f;
    if (quality != null)
        q = quality.floatValue();/*from  w  w  w .  j  av  a  2 s  . com*/
    writeParam.setCompressionQuality(q);

    DAByteArrayOutputStream buffer = new DAByteArrayOutputStream();
    writer.setOutput(new MemoryCacheImageOutputStream(buffer));

    IIOImage ioImage = new IIOImage(bufferedImage, null, null);

    writer.write(null, ioImage, writeParam);
    writer.dispose();

    return buffer.getDirectByteArray();
}

From source file:org.geotools.imageio.netcdf.NetCDFImageReader.java

@Override
public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex) throws IOException {
    final List<ImageTypeSpecifier> l = new java.util.ArrayList<ImageTypeSpecifier>();
    final VariableAdapter wrapper = getCoverageDescriptor(imageIndex);
    if (wrapper != null) {
        final SampleModel sampleModel = wrapper.getSampleModel();
        final ImageTypeSpecifier imageType = new ImageTypeSpecifier(
                ImageIOUtilities.createColorModel(sampleModel), sampleModel);
        l.add(imageType);/*from ww  w  .ja  v  a  2s  . c  o  m*/
    }
    return l.iterator();
}

From source file:org.geotools.imageio.unidata.UnidataImageReader.java

@Override
public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex) throws IOException {
    final List<ImageTypeSpecifier> l = new java.util.ArrayList<ImageTypeSpecifier>();
    final UnidataVariableAdapter wrapper = getCoverageDescriptor(imageIndex);
    if (wrapper != null) {
        final SampleModel sampleModel = wrapper.getSampleModel();
        final ImageTypeSpecifier imageType = new ImageTypeSpecifier(
                ImageIOUtilities.createColorModel(sampleModel), sampleModel);
        l.add(imageType);// w w w. ja  va2s.  c  om
    }
    return l.iterator();
}

From source file:org.polymap.core.data.image.ImageEncodeProcessor.java

private void imageioEncodeJPEG(Image image, ChunkedResponseOutputStream out) throws IOException {
    // this code is from http://forums.sun.com/thread.jspa?threadID=5197061
    ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
    ImageWriteParam param = writer.getDefaultWriteParam();
    param.setSourceBands(new int[] { 0, 1, 2 });
    ColorModel cm = new DirectColorModel(24, 0x00ff0000, // Red
            0x0000ff00, // Green
            0x000000ff, // Blue
            0x0); // Alpha
    param.setDestinationType(new ImageTypeSpecifier(cm, cm.createCompatibleSampleModel(1, 1)));

    ImageOutputStream imageOut = ImageIO.createImageOutputStream(out);
    writer.setOutput(imageOut);//from w w  w  .j  ava 2  s  .  c om
    writer.write(null, new IIOImage((RenderedImage) image, null, null), param);
    writer.dispose();
    imageOut.close();
}