Java Code Examples for javax.imageio.metadata.IIOMetadata#getAsTree()
The following examples show how to use
javax.imageio.metadata.IIOMetadata#getAsTree() .
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: ITXtTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 2
Source File: ITXtTest.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 3
Source File: TIFFImageWriter.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * 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 4
Source File: TIFFImageWriter.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Converts a native * {@code javax_imageio_tiff_image_1.0} tree to a * {@code TIFFImageMetadata} object. * * @param inData The metadata object. * @return a {@code TIFFImageMetadata} or {@code null} if * the native tree derived from the input object is {@code null}. * @throws IllegalArgumentException if {@code inData} is * {@code null} or does not support the native metadata format. * @throws IIOInvalidTreeException if {@code inData} generates an * invalid native metadata tree. */ private TIFFImageMetadata convertNativeImageMetadata(IIOMetadata inData) throws IIOInvalidTreeException { if(inData == null) { throw new NullPointerException("inData == null!"); } else if(!Arrays.asList(inData.getMetadataFormatNames()).contains( TIFFImageMetadata.NATIVE_METADATA_FORMAT_NAME)) { throw new IllegalArgumentException ("inData does not support native metadata format!"); } TIFFImageMetadata outData = null; String formatName = TIFFImageMetadata.NATIVE_METADATA_FORMAT_NAME; 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 5
Source File: ShortHistogramTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private IIOMetadata upgradeMetadata(IIOMetadata src, BufferedImage bi) { String format = src.getNativeMetadataFormatName(); System.out.println("Native format: " + format); Node root = src.getAsTree(format); // add hIST node Node n = lookupChildNode(root, "hIST"); if (n == null) { System.out.println("Appending new hIST node..."); Node hIST = gethISTNode(bi); root.appendChild(hIST); } System.out.println("Upgraded metadata tree:"); dump(root, ""); System.out.println("Merging metadata..."); try { src.mergeTree(format, root); } catch (IIOInvalidTreeException e) { throw new RuntimeException("Test FAILED!", e); } return src; }
Example 6
Source File: GIFImageWriter.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
private void writeHeader(IIOMetadata streamMetadata, int bitsPerPixel) throws IOException { GIFWritableStreamMetadata sm; if (streamMetadata instanceof GIFWritableStreamMetadata) { sm = (GIFWritableStreamMetadata)streamMetadata; } else { sm = new GIFWritableStreamMetadata(); Node root = streamMetadata.getAsTree(STREAM_METADATA_NAME); sm.setFromTree(STREAM_METADATA_NAME, root); } writeHeader(sm.version, sm.logicalScreenWidth, sm.logicalScreenHeight, sm.colorResolution, sm.pixelAspectRatio, sm.backgroundColorIndex, sm.sortFlag, bitsPerPixel, sm.globalColorTable); }
Example 7
Source File: ShortHistogramTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
private IIOMetadata upgradeMetadata(IIOMetadata src, BufferedImage bi) { String format = src.getNativeMetadataFormatName(); System.out.println("Native format: " + format); Node root = src.getAsTree(format); // add hIST node Node n = lookupChildNode(root, "hIST"); if (n == null) { System.out.println("Appending new hIST node..."); Node hIST = gethISTNode(bi); root.appendChild(hIST); } System.out.println("Upgraded metadata tree:"); dump(root, ""); System.out.println("Merging metadata..."); try { src.mergeTree(format, root); } catch (IIOInvalidTreeException e) { throw new RuntimeException("Test FAILED!", e); } return src; }
Example 8
Source File: GIFImageWriter.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private void writeHeader(IIOMetadata streamMetadata, int bitsPerPixel) throws IOException { GIFWritableStreamMetadata sm; if (streamMetadata instanceof GIFWritableStreamMetadata) { sm = (GIFWritableStreamMetadata)streamMetadata; } else { sm = new GIFWritableStreamMetadata(); Node root = streamMetadata.getAsTree(STREAM_METADATA_NAME); sm.setFromTree(STREAM_METADATA_NAME, root); } writeHeader(sm.version, sm.logicalScreenWidth, sm.logicalScreenHeight, sm.colorResolution, sm.pixelAspectRatio, sm.backgroundColorIndex, sm.sortFlag, bitsPerPixel, sm.globalColorTable); }
Example 9
Source File: PngDitDepthTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws IIOInvalidTreeException { // getting the writer for the png format Iterator iter = ImageIO.getImageWritersByFormatName("png"); ImageWriter writer = (ImageWriter) iter.next(); // creating a color model ColorModel colorModel = ColorModel.getRGBdefault(); // creating a sample model SampleModel sampleModel = colorModel.createCompatibleSampleModel(640, 480); // creating a default metadata object IIOMetadata metaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(colorModel, sampleModel), null); String formatName = metaData.getNativeMetadataFormatName(); // first call Node metaDataNode = metaData.getAsTree(formatName); try { metaData.setFromTree(formatName, metaDataNode); } catch (Exception ex) { ex.printStackTrace(); } // second call (bitdepht is already set to an invalid value) metaDataNode = metaData.getAsTree(formatName); metaData.setFromTree(formatName, metaDataNode); }
Example 10
Source File: AnimationImpl.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
static boolean isRestoreToBackgroundColor(ImageReader reader, int num) throws IOException { IIOMetadata meta = reader.getImageMetadata(num); Node parent = meta.getAsTree("javax_imageio_gif_image_1.0"); //printNodeTree(parent); NodeList root = parent.getChildNodes(); for(int iter = 0 ; iter < root.getLength() ; iter++) { Node n = root.item(iter); if(n.getNodeName().equals("GraphicControlExtension")) { return n.getAttributes().getNamedItem("disposalMethod").getNodeValue().equalsIgnoreCase("restoreToBackgroundColor"); } } return false; }
Example 11
Source File: PngDitDepthTest.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws IIOInvalidTreeException { // getting the writer for the png format Iterator iter = ImageIO.getImageWritersByFormatName("png"); ImageWriter writer = (ImageWriter) iter.next(); // creating a color model ColorModel colorModel = ColorModel.getRGBdefault(); // creating a sample model SampleModel sampleModel = colorModel.createCompatibleSampleModel(640, 480); // creating a default metadata object IIOMetadata metaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(colorModel, sampleModel), null); String formatName = metaData.getNativeMetadataFormatName(); // first call Node metaDataNode = metaData.getAsTree(formatName); try { metaData.setFromTree(formatName, metaDataNode); } catch (Exception ex) { ex.printStackTrace(); } // second call (bitdepht is already set to an invalid value) metaDataNode = metaData.getAsTree(formatName); metaData.setFromTree(formatName, metaDataNode); }
Example 12
Source File: TimelineEditor.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
static String getDisposalMethod(ImageReader reader, int num) throws IOException { IIOMetadata meta = reader.getImageMetadata(num); Node parent = meta.getAsTree("javax_imageio_gif_image_1.0"); //printNodeTree(parent); NodeList root = parent.getChildNodes(); for(int iter = 0 ; iter < root.getLength() ; iter++) { Node n = root.item(iter); if(n.getNodeName().equals("GraphicControlExtension")) { return n.getAttributes().getNamedItem("disposalMethod").getNodeValue(); } } return null; }
Example 13
Source File: GIFImageWriter.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * 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 14
Source File: ExifUtils.java From youkefu with Apache License 2.0 | 5 votes |
/** * Returns the orientation obtained from the Exif metadata. * * @param reader An {@link ImageReader} which is reading the * target image. * @param imageIndex The index of the image from which the Exif * metadata should be read from. * @return The orientation information obtained from the * Exif metadata, as a {@link Orientation} enum. * @throws IOException When an error occurs during reading. * @throws IllegalArgumentException If the {@link ImageReader} does not * have the target image set, or if the * reader does not have a JPEG open. */ public static Orientation getExifOrientation(ImageReader reader, int imageIndex) throws IOException { IIOMetadata metadata = reader.getImageMetadata(imageIndex); Node rootNode = metadata.getAsTree("javax_imageio_jpeg_image_1.0"); NodeList childNodes = rootNode.getChildNodes(); // Look for the APP1 containing Exif data, and retrieve it. for (int i = 0; i < childNodes.getLength(); i++) { if ("markerSequence".equals(childNodes.item(i).getNodeName())) { NodeList markerSequenceChildren = childNodes.item(i).getChildNodes(); for (int j = 0; j < markerSequenceChildren.getLength(); j++) { IIOMetadataNode metadataNode = (IIOMetadataNode)(markerSequenceChildren.item(j)); byte[] bytes = (byte[])metadataNode.getUserObject(); if (bytes == null) { continue; } byte[] magicNumber = new byte[4]; ByteBuffer.wrap(bytes).get(magicNumber); if (EXIF_MAGIC_STRING.equals(new String(magicNumber))) { return getOrientationFromExif(bytes); } } } } return null; }
Example 15
Source File: MergeTreeTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws IOException { ImageWriter iw = (ImageWriter)ImageIO.getImageWritersByFormatName("jpeg").next(); ImageTypeSpecifier type = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB); ImageOutputStream ios = ImageIO.createImageOutputStream(new File("MergeTreeTest.jpeg")); iw.setOutput(ios); IIOMetadata meta = iw.getDefaultImageMetadata(type, null); boolean isFailed = false; String[] fmts = meta.getMetadataFormatNames(); for (int i=0; i<fmts.length; i++) { System.out.print("Format: " + fmts[i] + " ... "); Node root = meta.getAsTree(fmts[i]); try { meta.mergeTree(fmts[i], root); } catch (NullPointerException e) { throw new RuntimeException("Test failed for format " + fmts[i], e); } System.out.println("PASSED"); } }
Example 16
Source File: ImageUtils.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
private static ImageAndResolution readAndDisplayMetadataPNG(DSSDocument image) throws IOException { try (InputStream is = image.openStream(); ImageInputStream iis = ImageIO.createImageInputStream(is)) { ImageReader reader = getImageReader("png"); // attach source to the reader reader.setInput(iis, true); int hdpi = DEFAULT_DPI; int vdpi = DEFAULT_DPI; if (isSupportedColorSpace(reader)) { // read metadata of first image IIOMetadata metadata = reader.getImageMetadata(0); double mm2inch = 25.4; Element node = (Element) metadata.getAsTree("javax_imageio_1.0"); NodeList lst = node.getElementsByTagName("HorizontalPixelSize"); if (lst != null && lst.getLength() == 1) { hdpi = (int) (mm2inch / Float.parseFloat(((Element) lst.item(0)).getAttribute("value"))); } else { LOG.debug("Cannot get HorizontalPixelSize value. Using the default dpi [{}]", DEFAULT_DPI); } lst = node.getElementsByTagName("VerticalPixelSize"); if (lst != null && lst.getLength() == 1) { vdpi = (int) (mm2inch / Float.parseFloat(((Element) lst.item(0)).getAttribute("value"))); } else { LOG.debug("Cannot get HorizontalPixelSize value. Using the default dpi [{}]", DEFAULT_DPI); } } else { LOG.warn("Cannot read metadata of the image with name [{}]. The color space is not supported. " + "Using the default dpi with value [{}]", image.getName(), DEFAULT_DPI); } return new ImageAndResolution(image, hdpi, vdpi); } }
Example 17
Source File: NoExtraBytesTest.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
private void checkColorDepth() { IIOMetadata dst = iio_dst.getMetadata(); Node data = dst.getAsTree("javax_imageio_bmp_1.0"); Node n = data.getFirstChild(); while (n != null && !("BitsPerPixel".equals(n.getNodeName()))) { System.out.println("Node " + n.getNodeName()); n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No BitsPerSample node!"); } int bpp = 0; String value = n.getNodeValue(); System.out.println("value = " + value); try { bpp = Integer.parseInt(value); } catch (NumberFormatException e) { throw new RuntimeException("Wrong bpp value: " + value, e); } if (bpp != this.expectedColorDepth) { throw new RuntimeException("Wrong color depth: " + bpp + " (should be " + this.expectedColorDepth + ")"); } }
Example 18
Source File: Utils.java From lizzie with GNU General Public License v3.0 | 4 votes |
public static void toGif( String path, List<BufferedImage> frames, int delayTime, boolean override) { if (Utils.isBlank(path) || frames == null || frames.size() == 0) return; Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("gif"); try { File file = new File(path); if (override) { file.deleteOnExit(); } ImageWriter writer = it.hasNext() ? it.next() : null; ImageOutputStream stream = ImageIO.createImageOutputStream(file); if (Objects.isNull(writer)) { throw new IOException(); } writer.setOutput(stream); writer.prepareWriteSequence(null); IIOMetadataNode gce = new IIOMetadataNode("GraphicControlExtension"); gce.setAttribute("disposalMethod", "none"); gce.setAttribute("userInputFlag", "FALSE"); gce.setAttribute("transparentColorFlag", "FALSE"); gce.setAttribute("transparentColorIndex", "0"); gce.setAttribute("delayTime", Objects.toString(delayTime)); IIOMetadataNode ae = new IIOMetadataNode("ApplicationExtension"); ae.setAttribute("applicationID", "NETSCAPE"); ae.setAttribute("authenticationCode", "2.0"); ae.setUserObject(new byte[] {0x1, 0x0, 0x0}); IIOMetadataNode aes = new IIOMetadataNode("ApplicationExtensions"); aes.appendChild(ae); for (BufferedImage image : frames) { ImageWriteParam iwp = writer.getDefaultWriteParam(); IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(image), iwp); String metaFormat = metadata.getNativeMetadataFormatName(); Node root = metadata.getAsTree(metaFormat); root.appendChild(gce); root.appendChild(aes); metadata.setFromTree(metaFormat, root); writer.writeToSequence(new IIOImage(image, null, metadata), null); metadata = null; } writer.endWriteSequence(); stream.close(); } catch (IOException ex) { ex.printStackTrace(); } }
Example 19
Source File: JPEGFactory.java From gcs with Mozilla Public License 2.0 | 4 votes |
private static void encodeImageToJPEGStream(BufferedImage image, float quality, int dpi, OutputStream out) throws IOException { // encode to JPEG ImageOutputStream ios = null; ImageWriter imageWriter = null; try { // find JAI writer imageWriter = getJPEGImageWriter(); ios = ImageIO.createImageOutputStream(out); imageWriter.setOutput(ios); // add compression JPEGImageWriteParam jpegParam = (JPEGImageWriteParam)imageWriter.getDefaultWriteParam(); jpegParam.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT); jpegParam.setCompressionQuality(quality); // add metadata ImageTypeSpecifier imageTypeSpecifier = new ImageTypeSpecifier(image); IIOMetadata data = imageWriter.getDefaultImageMetadata(imageTypeSpecifier, jpegParam); Element tree = (Element)data.getAsTree("javax_imageio_jpeg_image_1.0"); Element jfif = (Element)tree.getElementsByTagName("app0JFIF").item(0); jfif.setAttribute("Xdensity", Integer.toString(dpi)); jfif.setAttribute("Ydensity", Integer.toString(dpi)); jfif.setAttribute("resUnits", "1"); // 1 = dots/inch // write imageWriter.write(data, new IIOImage(image, null, null), jpegParam); } finally { // clean up IOUtils.closeQuietly(out); if (ios != null) { ios.close(); } if (imageWriter != null) { imageWriter.dispose(); } } }
Example 20
Source File: ImageFileReaders.java From MyBox with Apache License 2.0 | 4 votes |
public static void readImageMetaData(String format, ImageInformation imageInfo, IIOMetadata iioMetaData) { try { if (imageInfo == null || iioMetaData == null) { return; } StringBuilder metaDataXml = new StringBuilder(); String[] formatNames = iioMetaData.getMetadataFormatNames(); Map<String, Map<String, List<Map<String, Object>>>> metaData = new HashMap<>(); for (String formatName : formatNames) { Map<String, List<Map<String, Object>>> formatMetaData = new HashMap<>(); IIOMetadataNode tree = (IIOMetadataNode) iioMetaData.getAsTree(formatName); readImageMetaData(formatMetaData, metaDataXml, tree, 2); metaData.put(formatName, formatMetaData); } imageInfo.setMetaData(metaData); imageInfo.setMetaDataXml(metaDataXml.toString()); explainCommonMetaData(metaData, imageInfo); switch (format.toLowerCase()) { case "png": ImagePngFile.explainPngMetaData(metaData, imageInfo); break; case "jpg": case "jpeg": ImageJpgFile.explainJpegMetaData(metaData, imageInfo); break; case "gif": ImageGifFile.explainGifMetaData(metaData, imageInfo); break; case "bmp": ImageBmpFile.explainBmpMetaData(metaData, imageInfo); break; case "tif": case "tiff": ImageTiffFile.explainTiffMetaData(iioMetaData, imageInfo); break; default: } // logger.debug(metaData); } catch (Exception e) { logger.error(e.toString()); } }