Example usage for javax.imageio ImageWriter getClass

List of usage examples for javax.imageio ImageWriter getClass

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:mobac.program.tiledatawriter.TileImageJpegDataWriter.java

public void initialize() {
    if (log.isTraceEnabled()) {
        String s = "Available JPEG image writers:";
        Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg");
        while (writers.hasNext()) {
            ImageWriter w = writers.next();
            s += "\n\t" + w.getClass().getName();
        }/*from w w w . ja v  a  2  s  .c o  m*/
        log.trace(s);
    }
    jpegImageWriter = ImageIO.getImageWritersByFormatName("jpeg").next();
    if (jpegImageWriter == null)
        throw new NullPointerException("Unable to create a JPEG image writer");
    jpegImageWriter.addIIOWriteWarningListener(ImageWriterWarningListener.INSTANCE);
    log.debug("Used JPEG image writer: " + jpegImageWriter.getClass().getName());
    iwp = jpegImageWriter.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionQuality(jpegCompressionLevel);
}

From source file:nl.b3p.imagetool.ImageTool.java

/**
 * Private method which seeks through the supported image writers to check
 * if a there is a writers which handles the specified MIME. This method
 * checks spe- cifically for JPG or PNG images because Sun's Java supports
 * two kind of writers for these particular formats. And because one of
 * these writers doesn't function well, we need to be sure we have the right
 * writers.// ww  w. j  a va 2  s  . c o m
 *
 * @param mime String with the MIME to find.
 *
 * @return ImageWriter which can handle the specified MIME or null if no
 * writer was found.
 */
// <editor-fold defaultstate="" desc="getJPGOrPNGWriter(String mime) method.">
private ImageWriter getJPGOrPNGWriter(String mime) {
    Iterator it = ImageIO.getImageReadersByMIMEType(mime);
    ImageWriter imTest = null;
    while (it.hasNext()) {
        imTest = (ImageWriter) it.next();
        String name = imTest.getClass().getPackage().getName();
        String generalPackage = name.substring(0, name.lastIndexOf("."));
        if (generalPackage.equalsIgnoreCase("com.sun.media.imageioimpl.plugins")) {
            continue;
        }
    }
    return imTest;
}

From source file:org.dcm4che.tool.dcm2jpg.Dcm2Jpg.java

public static void listSupportedImageWriters(String format) {
    System.out.println(MessageFormat.format(rb.getString("writers"), format));
    Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(format);
    while (it.hasNext()) {
        ImageWriter writer = it.next();
        ImageWriteParam param = writer.getDefaultWriteParam();
        System.out.println(MessageFormat.format(rb.getString("writer"), writer.getClass().getName(),
                param.canWriteCompressed(), param.canWriteProgressive(), param.canWriteTiles(),
                param.canOffsetTiles(),/*  w ww  . j  a  va2  s. c  om*/
                param.canWriteCompressed() ? Arrays.toString(param.getCompressionTypes()) : null));
    }
}

From source file:org.dcm4che2.tool.dcm2jpg.Dcm2Jpg.java

private ImageWriter getImageWriter(String imageWriterClass) throws IIOException {
    ImageWriter writer;
    for (Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(formatName); it.hasNext();) {
        writer = it.next();/*  w w  w. j  av a 2s  . c  om*/
        if ("*".equals(imageWriterClass) || writer.getClass().getName().equals(imageWriterClass)) {
            return writer;
        }
    }
    throw new IIOException("No such ImageWriter - " + imageWriterClass);
}

From source file:org.dcm4che2.tool.dcm2jpg.Dcm2Jpg.java

private void showImageWriters() {
    ImageWriter writer;
    System.out.println("ImageWriters for format name:" + formatName);
    int i = 0;/*from ww w . j  ava 2 s  .  co m*/
    for (Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(formatName); it.hasNext();) {
        writer = it.next();
        System.out.println("Writer[" + (i++) + "]: " + writer.getClass().getName() + ":");
        System.out.println("   Write Param:");
        ImageWriteParam param = writer.getDefaultWriteParam();
        System.out.println("       canWriteCompressed:" + param.canWriteCompressed());
        System.out.println("      canWriteProgressive:" + param.canWriteProgressive());
        System.out.println("            canWriteTiles:" + param.canWriteTiles());
        System.out.println("           canOffsetTiles:" + param.canOffsetTiles());
        if (param.canWriteCompressed()) {
            String[] types = param.getCompressionTypes();
            System.out.println("   Compression Types:");
            if (types != null && types.length > 0) {
                for (int j = 0; j < types.length; j++) {
                    System.out.println("           Type[" + j + "]:" + types[j]);
                }
            }
        }
        System.out.println("-----------------------------");
    }
}

From source file:org.photovault.imginfo.CreateCopyImageCommand.java

/**
 Helper function to save a rendered image to file
 @param instanceFile The file into which the image will be saved
 @param img Image that willb e saved// w ww  . ja  v  a 2 s . c om
 @param xmpData XPM metadata packet that should be saved with the image
 @throws PhotovaultException if saving does not succeed
 */
protected void saveImage(File instanceFile, RenderedImage img, byte[] xmpData) throws PhotovaultException {
    ImageOutputStream out = null;
    log.debug("Entry: saveImage, file = " + instanceFile.getAbsolutePath());
    try {
        out = new FileImageOutputStream(instanceFile);
    } catch (IOException e) {
        log.error("Error writing image: " + e.getMessage());
        throw new PhotovaultException(e.getMessage());
    }
    if (img.getSampleModel().getSampleSize(0) == 16) {
        log.debug("16 bit image, converting to 8 bits");
        double[] subtract = new double[1];
        subtract[0] = 0;
        double[] divide = new double[1];
        divide[0] = 1. / 256.;
        // Now we can rescale the pixels gray levels:
        ParameterBlock pbRescale = new ParameterBlock();
        pbRescale.add(divide);
        pbRescale.add(subtract);
        pbRescale.addSource(img);
        PlanarImage outputImage = (PlanarImage) JAI.create("rescale", pbRescale, null);
        // Make sure it is a byte image - force conversion.
        ParameterBlock pbConvert = new ParameterBlock();
        pbConvert.addSource(outputImage);
        pbConvert.add(DataBuffer.TYPE_BYTE);
        img = JAI.create("format", pbConvert);
    }

    IIOImage iioimg = new IIOImage(img, null, null);

    /*
     Not all encoders support metadata handling
     */
    Iterator writers = ImageIO.getImageWritersByFormatName("jpeg");
    ImageWriter imgwriter = null;
    while (writers.hasNext()) {
        imgwriter = (ImageWriter) writers.next();
        if (imgwriter.getClass().getName().endsWith("JPEGImageEncoder")) {
            // Break on finding the core provider.
            break;
        }
    }
    if (imgwriter == null) {
        System.err.println("Cannot find core JPEG writer!");
    }
    imgwriter.addIIOWriteWarningListener(new IIOWriteWarningListener() {

        public void warningOccurred(ImageWriter arg0, int arg1, String arg2) {
            log.warn("Warning from ImageWriter: " + arg2);
        }
    });
    ImageWriteParam params = imgwriter.getDefaultWriteParam();
    ImageTypeSpecifier its = ImageTypeSpecifier.createFromRenderedImage(img);
    IIOMetadata metadata = imgwriter.getDefaultImageMetadata(its, null);

    IIOMetadataNode metatop = (IIOMetadataNode) metadata.getAsTree("javax_imageio_jpeg_image_1.0");
    NodeList markerSeqNodes = metatop.getElementsByTagName("markerSequence");
    if (markerSeqNodes.getLength() > 0) {
        IIOMetadataNode xmpNode = new IIOMetadataNode("unknown");
        xmpNode.setAttribute("MarkerTag", "225");
        xmpNode.setUserObject(xmpData);
        markerSeqNodes.item(0).appendChild(xmpNode);
    }

    try {
        metadata.setFromTree("javax_imageio_jpeg_image_1.0", metatop);
    } catch (Exception e) {
        log.warn("error editing metadata: " + e.getMessage());
        e.printStackTrace();
        throw new PhotovaultException("error setting image metadata: \n" + e.getMessage());
    }

    iioimg.setMetadata(metadata);

    try {
        imgwriter.setOutput(out);
        imgwriter.write(iioimg);
    } catch (IOException e) {
        log.warn("Exception while encoding" + e.getMessage());
        throw new PhotovaultException(
                "Error writing instance " + instanceFile.getAbsolutePath() + ": " + e.getMessage());
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            log.warn("Exception while closing file: " + e.getMessage());
            imgwriter.dispose();
            throw new PhotovaultException(
                    "Error writing instance " + instanceFile.getAbsolutePath() + ": " + e.getMessage());

        }
        imgwriter.dispose();
    }
    log.debug("Exit: saveImage");
}

From source file:osmb.program.tiledatawriter.TileImageJpegDataWriter.java

@Override
public void initialize() {
    if (log.isTraceEnabled()) {
        String s = "Available JPEG image writers:";
        Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg");
        while (writers.hasNext()) {
            ImageWriter w = writers.next();
            s += "\n\t" + w.getClass().getName();
        }/*from  ww  w. j  a  v a2 s .  com*/
        log.trace(s);
    }
    jpegImageWriter = ImageIO.getImageWritersByFormatName("jpeg").next();
    if (jpegImageWriter == null)
        throw new NullPointerException("Unable to create a JPEG image writer");
    jpegImageWriter.addIIOWriteWarningListener(ImageWriterWarningListener.INSTANCE);
    log.debug("Used JPEG image writer: " + jpegImageWriter.getClass().getName());
    iwp = jpegImageWriter.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionQuality(jpegCompressionLevel);
}