Java Code Examples for javax.imageio.metadata.IIOMetadata#isStandardMetadataFormatSupported()

The following examples show how to use javax.imageio.metadata.IIOMetadata#isStandardMetadataFormatSupported() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TIFFImageWriter.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a standard {@code javax_imageio_1.0} tree to a
 * {@code TIFFImageMetadata} object.
 *
 * @param inData The metadata object.
 * @return a {@code TIFFImageMetadata} or {@code null} if
 * the standard tree derived from the input object is {@code null}.
 * @throws IllegalArgumentException if {@code inData} is
 * {@code null}.
 * @throws IllegalArgumentException if {@code inData} does not support
 * the standard metadata format.
 * @throws IIOInvalidTreeException if {@code inData} generates an
 * invalid standard metadata tree.
 */
private TIFFImageMetadata convertStandardImageMetadata(IIOMetadata inData)
    throws IIOInvalidTreeException {

    if(inData == null) {
        throw new NullPointerException("inData == null!");
    } else if(!inData.isStandardMetadataFormatSupported()) {
        throw new IllegalArgumentException
            ("inData does not support standard metadata format!");
    }

    TIFFImageMetadata outData = null;

    String formatName = IIOMetadataFormatImpl.standardMetadataFormatName;
    Node tree = inData.getAsTree(formatName);
    if (tree != null) {
        List<TIFFTagSet> tagSets = new ArrayList<TIFFTagSet>(1);
        tagSets.add(BaselineTIFFTagSet.getInstance());
        outData = new TIFFImageMetadata(tagSets);
        outData.setFromTree(formatName, tree);
    }

    return outData;
}
 
Example 2
Source File: SinglePageDocument.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private void writeDocument(OutputStream outputStream, Format format, int dpi) throws IOException,
		DocumentException, PrintingException {
	if (format == Format.PDF) {
		baos.writeTo(outputStream);
	} else {
		PDDocument pdf = PDDocument.load(new ByteArrayInputStream(baos.toByteArray()), true);
		PDFRenderer renderer = new PDFRenderer(pdf);
		BufferedImage bufferedImage = renderer.renderImageWithDPI(0, dpi);
		pdf.close();
		if (format == Format.PNG) {
			final String formatName = format.getExtension();
			for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {
				ImageWriter writer1 = iw.next();
				ImageWriteParam writeParam = writer1.getDefaultWriteParam();
				ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier
						.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB);
				IIOMetadata metadata = writer1.getDefaultImageMetadata(typeSpecifier, writeParam);
				if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
					continue;
				}

				setDPI(metadata);
				// Write bufferedImage to outputStream
				final ImageOutputStream stream = ImageIO.createImageOutputStream(outputStream);
				try {
					writer1.setOutput(stream);
					writer1.write(metadata, new IIOImage(bufferedImage, null, metadata), writeParam);
				} finally {
					stream.flush();
					stream.close();
				}
				break;
			}
		} else {
			ImageIO.write(bufferedImage, format.getExtension(), outputStream);
		}

	}
}
 
Example 3
Source File: GIFImageWriter.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Merges <code>inData</code> into <code>outData</code>. The supplied
 * metadata format name is attempted first and failing that the standard
 * metadata format name is attempted.
 */
private void convertMetadata(String metadataFormatName,
                             IIOMetadata inData,
                             IIOMetadata outData) {
    String formatName = null;

    String nativeFormatName = inData.getNativeMetadataFormatName();
    if (nativeFormatName != null &&
        nativeFormatName.equals(metadataFormatName)) {
        formatName = metadataFormatName;
    } else {
        String[] extraFormatNames = inData.getExtraMetadataFormatNames();

        if (extraFormatNames != null) {
            for (int i = 0; i < extraFormatNames.length; i++) {
                if (extraFormatNames[i].equals(metadataFormatName)) {
                    formatName = metadataFormatName;
                    break;
                }
            }
        }
    }

    if (formatName == null &&
        inData.isStandardMetadataFormatSupported()) {
        formatName = STANDARD_METADATA_NAME;
    }

    if (formatName != null) {
        try {
            Node root = inData.getAsTree(formatName);
            outData.mergeTree(formatName, root);
        } catch(IIOInvalidTreeException e) {
            // ignore
        }
    }
}
 
Example 4
Source File: GIFImageWriter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Merges <code>inData</code> into <code>outData</code>. The supplied
 * metadata format name is attempted first and failing that the standard
 * metadata format name is attempted.
 */
private void convertMetadata(String metadataFormatName,
                             IIOMetadata inData,
                             IIOMetadata outData) {
    String formatName = null;

    String nativeFormatName = inData.getNativeMetadataFormatName();
    if (nativeFormatName != null &&
        nativeFormatName.equals(metadataFormatName)) {
        formatName = metadataFormatName;
    } else {
        String[] extraFormatNames = inData.getExtraMetadataFormatNames();

        if (extraFormatNames != null) {
            for (int i = 0; i < extraFormatNames.length; i++) {
                if (extraFormatNames[i].equals(metadataFormatName)) {
                    formatName = metadataFormatName;
                    break;
                }
            }
        }
    }

    if (formatName == null &&
        inData.isStandardMetadataFormatSupported()) {
        formatName = STANDARD_METADATA_NAME;
    }

    if (formatName != null) {
        try {
            Node root = inData.getAsTree(formatName);
            outData.mergeTree(formatName, root);
        } catch(IIOInvalidTreeException e) {
            // ignore
        }
    }
}
 
Example 5
Source File: Export.java    From PDV with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Export component to png format
 * @param component Component
 * @param bounds Rectangle
 * @param exportFile Export file
 * @throws IOException
 */
private static void exportPNG(Component component, Rectangle bounds, File exportFile) throws IOException {

    BufferedImage bufferedImage = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_RGB);

    Graphics g = bufferedImage.getGraphics();

    component.paint(g);

    for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName("png"); iw.hasNext();){

        ImageWriter writer = iw.next();
        ImageWriteParam writeParam = writer.getDefaultWriteParam();

        ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
        IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);

        if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()){
            continue;
        }
        ImageOutputStream stream = null;
        try {

            setDPI(metadata);

            stream = ImageIO.createImageOutputStream(exportFile);
            writer.setOutput(stream);
            writer.write(metadata, new IIOImage(bufferedImage, null, metadata), writeParam);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            stream.close();
        }
        break;
    }
}
 
Example 6
Source File: ImageReader.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private IIOMetadata getMetadata(String formatName,
                                Set nodeNames,
                                boolean wantStream,
                                int imageIndex) throws IOException {
    if (formatName == null) {
        throw new IllegalArgumentException("formatName == null!");
    }
    if (nodeNames == null) {
        throw new IllegalArgumentException("nodeNames == null!");
    }
    IIOMetadata metadata =
        wantStream
        ? getStreamMetadata()
        : getImageMetadata(imageIndex);
    if (metadata != null) {
        if (metadata.isStandardMetadataFormatSupported() &&
            formatName.equals
            (IIOMetadataFormatImpl.standardMetadataFormatName)) {
            return metadata;
        }
        String nativeName = metadata.getNativeMetadataFormatName();
        if (nativeName != null && formatName.equals(nativeName)) {
            return metadata;
        }
        String[] extraNames = metadata.getExtraMetadataFormatNames();
        if (extraNames != null) {
            for (int i = 0; i < extraNames.length; i++) {
                if (formatName.equals(extraNames[i])) {
                    return metadata;
                }
            }
        }
    }
    return null;
}
 
Example 7
Source File: LegendManager.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Save image.
 *
 * @param image the grid image
 * @param formatName the format name
 * @param destinationFile the output
 * @param dpi the dpi
 * @return true, if successful
 * @throws IOException Signals that an I/O exception has occurred.
 */
private boolean saveGridImage(
        BufferedImage image, String formatName, File destinationFile, int dpi)
        throws IOException {
    if (!Files.delete(destinationFile)) {
        ConsoleManager.getInstance()
                .information(
                        this,
                        String.format(
                                "Failed to delete '%s'", destinationFile.getAbsolutePath()));
    }

    boolean finish = false;

    for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName);
            iw.hasNext() && !finish; ) {
        ImageWriter writer = iw.next();
        ImageWriteParam writeParam = writer.getDefaultWriteParam();
        ImageTypeSpecifier typeSpecifier =
                ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
        IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
        if (!(metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported())) {

            setDPI(metadata, dpi);

            final ImageOutputStream stream = ImageIO.createImageOutputStream(destinationFile);
            try {
                writer.setOutput(stream);
                writer.write(metadata, new IIOImage(image, null, metadata), writeParam);
            } finally {
                stream.close();
            }
            finish = true;
        }
    }

    return true;
}
 
Example 8
Source File: GIFImageWriter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Merges <code>inData</code> into <code>outData</code>. The supplied
 * metadata format name is attempted first and failing that the standard
 * metadata format name is attempted.
 */
private void convertMetadata(String metadataFormatName,
                             IIOMetadata inData,
                             IIOMetadata outData) {
    String formatName = null;

    String nativeFormatName = inData.getNativeMetadataFormatName();
    if (nativeFormatName != null &&
        nativeFormatName.equals(metadataFormatName)) {
        formatName = metadataFormatName;
    } else {
        String[] extraFormatNames = inData.getExtraMetadataFormatNames();

        if (extraFormatNames != null) {
            for (int i = 0; i < extraFormatNames.length; i++) {
                if (extraFormatNames[i].equals(metadataFormatName)) {
                    formatName = metadataFormatName;
                    break;
                }
            }
        }
    }

    if (formatName == null &&
        inData.isStandardMetadataFormatSupported()) {
        formatName = STANDARD_METADATA_NAME;
    }

    if (formatName != null) {
        try {
            Node root = inData.getAsTree(formatName);
            outData.mergeTree(formatName, root);
        } catch(IIOInvalidTreeException e) {
            // ignore
        }
    }
}
 
Example 9
Source File: GIFImageWriter.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Merges <code>inData</code> into <code>outData</code>. The supplied
 * metadata format name is attempted first and failing that the standard
 * metadata format name is attempted.
 */
private void convertMetadata(String metadataFormatName,
                             IIOMetadata inData,
                             IIOMetadata outData) {
    String formatName = null;

    String nativeFormatName = inData.getNativeMetadataFormatName();
    if (nativeFormatName != null &&
        nativeFormatName.equals(metadataFormatName)) {
        formatName = metadataFormatName;
    } else {
        String[] extraFormatNames = inData.getExtraMetadataFormatNames();

        if (extraFormatNames != null) {
            for (int i = 0; i < extraFormatNames.length; i++) {
                if (extraFormatNames[i].equals(metadataFormatName)) {
                    formatName = metadataFormatName;
                    break;
                }
            }
        }
    }

    if (formatName == null &&
        inData.isStandardMetadataFormatSupported()) {
        formatName = STANDARD_METADATA_NAME;
    }

    if (formatName != null) {
        try {
            Node root = inData.getAsTree(formatName);
            outData.mergeTree(formatName, root);
        } catch(IIOInvalidTreeException e) {
            // ignore
        }
    }
}
 
Example 10
Source File: ImageReader.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private IIOMetadata getMetadata(String formatName,
                                Set nodeNames,
                                boolean wantStream,
                                int imageIndex) throws IOException {
    if (formatName == null) {
        throw new IllegalArgumentException("formatName == null!");
    }
    if (nodeNames == null) {
        throw new IllegalArgumentException("nodeNames == null!");
    }
    IIOMetadata metadata =
        wantStream
        ? getStreamMetadata()
        : getImageMetadata(imageIndex);
    if (metadata != null) {
        if (metadata.isStandardMetadataFormatSupported() &&
            formatName.equals
            (IIOMetadataFormatImpl.standardMetadataFormatName)) {
            return metadata;
        }
        String nativeName = metadata.getNativeMetadataFormatName();
        if (nativeName != null && formatName.equals(nativeName)) {
            return metadata;
        }
        String[] extraNames = metadata.getExtraMetadataFormatNames();
        if (extraNames != null) {
            for (int i = 0; i < extraNames.length; i++) {
                if (formatName.equals(extraNames[i])) {
                    return metadata;
                }
            }
        }
    }
    return null;
}
 
Example 11
Source File: ImageReader.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private IIOMetadata getMetadata(String formatName,
                                Set nodeNames,
                                boolean wantStream,
                                int imageIndex) throws IOException {
    if (formatName == null) {
        throw new IllegalArgumentException("formatName == null!");
    }
    if (nodeNames == null) {
        throw new IllegalArgumentException("nodeNames == null!");
    }
    IIOMetadata metadata =
        wantStream
        ? getStreamMetadata()
        : getImageMetadata(imageIndex);
    if (metadata != null) {
        if (metadata.isStandardMetadataFormatSupported() &&
            formatName.equals
            (IIOMetadataFormatImpl.standardMetadataFormatName)) {
            return metadata;
        }
        String nativeName = metadata.getNativeMetadataFormatName();
        if (nativeName != null && formatName.equals(nativeName)) {
            return metadata;
        }
        String[] extraNames = metadata.getExtraMetadataFormatNames();
        if (extraNames != null) {
            for (int i = 0; i < extraNames.length; i++) {
                if (formatName.equals(extraNames[i])) {
                    return metadata;
                }
            }
        }
    }
    return null;
}
 
Example 12
Source File: ImageReader.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private IIOMetadata getMetadata(String formatName,
                                Set nodeNames,
                                boolean wantStream,
                                int imageIndex) throws IOException {
    if (formatName == null) {
        throw new IllegalArgumentException("formatName == null!");
    }
    if (nodeNames == null) {
        throw new IllegalArgumentException("nodeNames == null!");
    }
    IIOMetadata metadata =
        wantStream
        ? getStreamMetadata()
        : getImageMetadata(imageIndex);
    if (metadata != null) {
        if (metadata.isStandardMetadataFormatSupported() &&
            formatName.equals
            (IIOMetadataFormatImpl.standardMetadataFormatName)) {
            return metadata;
        }
        String nativeName = metadata.getNativeMetadataFormatName();
        if (nativeName != null && formatName.equals(nativeName)) {
            return metadata;
        }
        String[] extraNames = metadata.getExtraMetadataFormatNames();
        if (extraNames != null) {
            for (int i = 0; i < extraNames.length; i++) {
                if (formatName.equals(extraNames[i])) {
                    return metadata;
                }
            }
        }
    }
    return null;
}
 
Example 13
Source File: ImageReader.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private IIOMetadata getMetadata(String formatName,
                                Set nodeNames,
                                boolean wantStream,
                                int imageIndex) throws IOException {
    if (formatName == null) {
        throw new IllegalArgumentException("formatName == null!");
    }
    if (nodeNames == null) {
        throw new IllegalArgumentException("nodeNames == null!");
    }
    IIOMetadata metadata =
        wantStream
        ? getStreamMetadata()
        : getImageMetadata(imageIndex);
    if (metadata != null) {
        if (metadata.isStandardMetadataFormatSupported() &&
            formatName.equals
            (IIOMetadataFormatImpl.standardMetadataFormatName)) {
            return metadata;
        }
        String nativeName = metadata.getNativeMetadataFormatName();
        if (nativeName != null && formatName.equals(nativeName)) {
            return metadata;
        }
        String[] extraNames = metadata.getExtraMetadataFormatNames();
        if (extraNames != null) {
            for (int i = 0; i < extraNames.length; i++) {
                if (formatName.equals(extraNames[i])) {
                    return metadata;
                }
            }
        }
    }
    return null;
}
 
Example 14
Source File: ImageReader.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
private IIOMetadata getMetadata(String formatName,
                                Set nodeNames,
                                boolean wantStream,
                                int imageIndex) throws IOException {
    if (formatName == null) {
        throw new IllegalArgumentException("formatName == null!");
    }
    if (nodeNames == null) {
        throw new IllegalArgumentException("nodeNames == null!");
    }
    IIOMetadata metadata =
        wantStream
        ? getStreamMetadata()
        : getImageMetadata(imageIndex);
    if (metadata != null) {
        if (metadata.isStandardMetadataFormatSupported() &&
            formatName.equals
            (IIOMetadataFormatImpl.standardMetadataFormatName)) {
            return metadata;
        }
        String nativeName = metadata.getNativeMetadataFormatName();
        if (nativeName != null && formatName.equals(nativeName)) {
            return metadata;
        }
        String[] extraNames = metadata.getExtraMetadataFormatNames();
        if (extraNames != null) {
            for (int i = 0; i < extraNames.length; i++) {
                if (formatName.equals(extraNames[i])) {
                    return metadata;
                }
            }
        }
    }
    return null;
}
 
Example 15
Source File: ImageReader.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private IIOMetadata getMetadata(String formatName,
                                Set nodeNames,
                                boolean wantStream,
                                int imageIndex) throws IOException {
    if (formatName == null) {
        throw new IllegalArgumentException("formatName == null!");
    }
    if (nodeNames == null) {
        throw new IllegalArgumentException("nodeNames == null!");
    }
    IIOMetadata metadata =
        wantStream
        ? getStreamMetadata()
        : getImageMetadata(imageIndex);
    if (metadata != null) {
        if (metadata.isStandardMetadataFormatSupported() &&
            formatName.equals
            (IIOMetadataFormatImpl.standardMetadataFormatName)) {
            return metadata;
        }
        String nativeName = metadata.getNativeMetadataFormatName();
        if (nativeName != null && formatName.equals(nativeName)) {
            return metadata;
        }
        String[] extraNames = metadata.getExtraMetadataFormatNames();
        if (extraNames != null) {
            for (int i = 0; i < extraNames.length; i++) {
                if (formatName.equals(extraNames[i])) {
                    return metadata;
                }
            }
        }
    }
    return null;
}
 
Example 16
Source File: ImageReader.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private IIOMetadata getMetadata(String formatName,
                                Set<String> nodeNames,
                                boolean wantStream,
                                int imageIndex) throws IOException {
    if (formatName == null) {
        throw new IllegalArgumentException("formatName == null!");
    }
    if (nodeNames == null) {
        throw new IllegalArgumentException("nodeNames == null!");
    }
    IIOMetadata metadata =
        wantStream
        ? getStreamMetadata()
        : getImageMetadata(imageIndex);
    if (metadata != null) {
        if (metadata.isStandardMetadataFormatSupported() &&
            formatName.equals
            (IIOMetadataFormatImpl.standardMetadataFormatName)) {
            return metadata;
        }
        String nativeName = metadata.getNativeMetadataFormatName();
        if (nativeName != null && formatName.equals(nativeName)) {
            return metadata;
        }
        String[] extraNames = metadata.getExtraMetadataFormatNames();
        if (extraNames != null) {
            for (int i = 0; i < extraNames.length; i++) {
                if (formatName.equals(extraNames[i])) {
                    return metadata;
                }
            }
        }
    }
    return null;
}
 
Example 17
Source File: GLChartPanel.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Save image file
 * @param fn File path
 * @param dpi Image dpi
 * @param width Image width
 * @param height Image height
 * @param sleep Sleep seconds
 * @throws InterruptedException
 */
public void saveImage(String fn, int dpi, int width, int height, Integer sleep) throws InterruptedException, IOException {
    BufferedImage image = this.paintViewImage(width, height, dpi);

    if (image != null) {
        File output = new File(fn);
        output.delete();
        String formatName = fn.substring(fn.lastIndexOf('.') + 1);
        for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {
            ImageWriter writer = iw.next();
            ImageWriteParam writeParam = writer.getDefaultWriteParam();
            ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB);
            IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
            if (metadata == null) {
                metadata = writer.getDefaultImageMetadata(typeSpecifier, null);
            }
            if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
                continue;
            }

            ImageUtil.setDPI(metadata, dpi);

            if (sleep != null) {
                Thread.sleep(sleep * 1000);
            }
            final ImageOutputStream stream = ImageIO.createImageOutputStream(output);
            try {
                writer.setOutput(stream);
                writer.write(metadata, new IIOImage(image, null, metadata), writeParam);
            } finally {
                stream.close();
            }
            break;
        }
    }

    this.plot3DGL.reshape(this, 0, 0, this.getWidth(), this.getHeight());
}
 
Example 18
Source File: ImageReader.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
private IIOMetadata getMetadata(String formatName,
                                Set nodeNames,
                                boolean wantStream,
                                int imageIndex) throws IOException {
    if (formatName == null) {
        throw new IllegalArgumentException("formatName == null!");
    }
    if (nodeNames == null) {
        throw new IllegalArgumentException("nodeNames == null!");
    }
    IIOMetadata metadata =
        wantStream
        ? getStreamMetadata()
        : getImageMetadata(imageIndex);
    if (metadata != null) {
        if (metadata.isStandardMetadataFormatSupported() &&
            formatName.equals
            (IIOMetadataFormatImpl.standardMetadataFormatName)) {
            return metadata;
        }
        String nativeName = metadata.getNativeMetadataFormatName();
        if (nativeName != null && formatName.equals(nativeName)) {
            return metadata;
        }
        String[] extraNames = metadata.getExtraMetadataFormatNames();
        if (extraNames != null) {
            for (int i = 0; i < extraNames.length; i++) {
                if (formatName.equals(extraNames[i])) {
                    return metadata;
                }
            }
        }
    }
    return null;
}
 
Example 19
Source File: ChartPanel.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Save image
 *
 * @param fileName File name
 * @param dpi DPI
 * @param width Width
 * @param height Height
 * @param sleep Sleep seconds for web map layer
 * @throws IOException
 * @throws java.lang.InterruptedException
 */
public void saveImage(String fileName, int dpi, int width, int height, Integer sleep) throws IOException, InterruptedException {
    File output = new File(fileName);
    output.delete();

    String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
    if (formatName.equals("jpg")) {
        formatName = "jpeg";
        saveImage_Jpeg(fileName, width, height, dpi);
        return;
    }

    double scaleFactor = dpi / 72.0;
    BufferedImage image = new BufferedImage((int)(width * scaleFactor), (int)(height * scaleFactor), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = image.createGraphics();
    AffineTransform at = g.getTransform();
    at.scale(scaleFactor, scaleFactor);
    g.setTransform(at);
    paintGraphics(g, width, height);
    for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {
        ImageWriter writer = iw.next();
        ImageWriteParam writeParam = writer.getDefaultWriteParam();
        ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB);
        IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
        if (metadata == null) {
            metadata = writer.getDefaultImageMetadata(typeSpecifier, null);
        }
        if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
            continue;
        }

        ImageUtil.setDPI(metadata, dpi);

        if (sleep != null) {
            Thread.sleep(sleep * 1000);
        }
        final ImageOutputStream stream = ImageIO.createImageOutputStream(output);
        try {
            writer.setOutput(stream);
            writer.write(metadata, new IIOImage(image, null, metadata), writeParam);
        } finally {
            stream.close();
        }
        break;
    }
    g.dispose();
}
 
Example 20
Source File: TIFFDirectory.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a {@code TIFFDirectory} instance from the contents of
 * an image metadata object. The supplied object must support an image
 * metadata format supported by the TIFF {@link javax.imageio.ImageWriter}
 * plug-in. This will usually be either the TIFF native image metadata
 * format {@code javax_imageio_tiff_image_1.0} or the Java
 * Image I/O standard metadata format {@code javax_imageio_1.0}.
 *
 * @param tiffImageMetadata A metadata object which supports a compatible
 * image metadata format.
 *
 * @return A {@code TIFFDirectory} populated from the contents of
 * the supplied metadata object.
 *
 * @throws NullPointerException if {@code tiffImageMetadata}
 * is {@code null}.
 * @throws IllegalArgumentException if {@code tiffImageMetadata}
 * does not support a compatible image metadata format.
 * @throws IIOInvalidTreeException if the supplied metadata object
 * cannot be parsed.
 */
public static TIFFDirectory
    createFromMetadata(IIOMetadata tiffImageMetadata)
    throws IIOInvalidTreeException {

    if(tiffImageMetadata == null) {
        throw new NullPointerException("tiffImageMetadata == null");
    }

    TIFFImageMetadata tim;
    if(tiffImageMetadata instanceof TIFFImageMetadata) {
        tim = (TIFFImageMetadata)tiffImageMetadata;
    } else {
        // Create a native metadata object.
        ArrayList<TIFFTagSet> l = new ArrayList<TIFFTagSet>(1);
        l.add(BaselineTIFFTagSet.getInstance());
        tim = new TIFFImageMetadata(l);

        // Determine the format name to use.
        String formatName = null;
        if(TIFFImageMetadata.NATIVE_METADATA_FORMAT_NAME.equals
           (tiffImageMetadata.getNativeMetadataFormatName())) {
            formatName = TIFFImageMetadata.NATIVE_METADATA_FORMAT_NAME;
        } else {
            String[] extraNames =
                tiffImageMetadata.getExtraMetadataFormatNames();
            if(extraNames != null) {
                for(int i = 0; i < extraNames.length; i++) {
                    if(TIFFImageMetadata.NATIVE_METADATA_FORMAT_NAME.equals
                       (extraNames[i])) {
                        formatName = extraNames[i];
                        break;
                    }
                }
            }

            if(formatName == null) {
                if(tiffImageMetadata.isStandardMetadataFormatSupported()) {
                    formatName =
                        IIOMetadataFormatImpl.standardMetadataFormatName;
                } else {
                    throw new IllegalArgumentException
                        ("Parameter does not support required metadata format!");
                }
            }
        }

        // Set the native metadata object from the tree.
        tim.setFromTree(formatName,
                        tiffImageMetadata.getAsTree(formatName));
    }

    return tim.getRootIFD();
}