Example usage for javax.imageio.metadata IIOMetadataNode getUserObject

List of usage examples for javax.imageio.metadata IIOMetadataNode getUserObject

Introduction

In this page you can find the example usage for javax.imageio.metadata IIOMetadataNode getUserObject.

Prototype

public Object getUserObject() 

Source Link

Document

Returns the Object value associated with this node.

Usage

From source file:net.algart.simagis.imageio.IIOMetadataToJsonConverter.java

private JSONObject iioNodeUserObjectToJson(IIOMetadataNode node) throws JSONException {
    final Object userObject = node.getUserObject();
    if (userObject == null) {
        return null;
    }/*from   w w  w . ja  v a  2s  .  c o m*/
    JSONObject result = new JSONObject();
    result.put("class", userObject.getClass().getCanonicalName());
    final String stringRepresentation = userObject.toString();
    final String javaDefaultStringRepresentation = userObject.getClass().getName() + "@"
            + Integer.toHexString(System.identityHashCode(userObject)); // see JavaDoc to Object.toString
    if (!stringRepresentation.equals(javaDefaultStringRepresentation)) {
        // so, we can hope for some non-trivial information in toString()
        result.put("toString", stringRepresentation);
    }
    if (userObject instanceof byte[]) {
        byte[] bytes = (byte[]) userObject;
        result.put("valueLength", bytes.length);
        if (bytes.length <= 16384) {
            StringBuilder sb = new StringBuilder();
            for (int k = 0; k < bytes.length; k++) {
                // formatting bytes like it is done in TIFFUndefined value in TIFF metadata
                if (k > 0) {
                    sb.append(",");
                }
                sb.append(bytes[k] & 0xFF);
            }
            result.put("valueBytes", sb.toString());
        }
    }
    JSONObject extendedInfo;
    try {
        extendedInfo = extendedUserObjectToJson(userObject);
    } catch (Exception e) {
        extendedInfo = exceptionToJson(e);
    }
    result.put("extendedInfo", extendedInfo);
    return result;
}

From source file:org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderImageIO.java

private ICC_Profile tryToExctractICCProfileFromJPEGMetadataNode(Element jpgNode) {
    ICC_Profile iccProf = null;/*from w  w w.j  a  v  a 2  s .  c om*/
    Element jfifNode = ImageIOUtil.getChild(jpgNode, "app0JFIF");
    if (jfifNode != null) {
        Element app2iccNode = ImageIOUtil.getChild(jfifNode, "app2ICC");
        if (app2iccNode instanceof IIOMetadataNode) {
            IIOMetadataNode imn = (IIOMetadataNode) app2iccNode;
            iccProf = (ICC_Profile) imn.getUserObject();
        }
    }
    return iccProf;
}

From source file:org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderImageIO.java

private ICC_Profile tryToExctractICCProfileFromPNGMetadataNode(Element pngNode) {
    ICC_Profile iccProf = null;/*ww  w .ja  v  a2s  .co m*/
    Element iccpNode = ImageIOUtil.getChild(pngNode, "iCCP");
    if (iccpNode instanceof IIOMetadataNode) {
        IIOMetadataNode imn = (IIOMetadataNode) iccpNode;
        byte[] prof = (byte[]) imn.getUserObject();
        String comp = imn.getAttribute("compressionMethod");
        if ("deflate".equalsIgnoreCase(comp)) {
            Inflater decompresser = new Inflater();
            decompresser.setInput(prof);
            byte[] result = new byte[100];
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            boolean failed = false;
            while (!decompresser.finished() && !failed) {
                try {
                    int resultLength = decompresser.inflate(result);
                    bos.write(result, 0, resultLength);
                    if (resultLength == 0) {
                        // this means more data or an external dictionary is
                        // needed. Both of which are not available, so we
                        // fail.
                        log.debug("Failed to deflate ICC Profile");
                        failed = true;
                    }
                } catch (DataFormatException e) {
                    log.debug("Failed to deflate ICC Profile", e);
                    failed = true;
                }
            }
            decompresser.end();
            try {
                iccProf = ICC_Profile.getInstance(bos.toByteArray());
            } catch (IllegalArgumentException e) {
                log.debug("Failed to interpret embedded ICC Profile", e);
                iccProf = null;
            }
        }
    }
    return iccProf;
}

From source file:org.photovault.image.ImageIOImage.java

/**
 * Parse JPEG metadata structure and store the data in metadata and exifData fields
 * @param top The metadata object tree in format "javax_imageio_jpeg_image_1.0"
 */// www  .ja  v  a  2s . c  om
private void parseJPEGMetadata(IIOMetadataNode top) {
    NodeList candidates = top.getElementsByTagName("unknown");
    for (int n = 0; n < candidates.getLength(); n++) {
        Node node = candidates.item(n);
        if (node instanceof IIOMetadataNode) {
            IIOMetadataNode m = (IIOMetadataNode) node;
            Object obj = m.getUserObject();
            if (obj instanceof byte[]) {
                byte[] data = (byte[]) obj;
                if (data[0] == 'E' && data[1] == 'x' && data[2] == 'i' && data[3] == 'f') {
                    log.debug("exif data found");
                    InputStream is = new ByteArrayInputStream(data, 6, data.length - 6);
                    try {
                        ImageInputStream metadataStream = ImageIO.createImageInputStream(is);
                        Iterator readers = ImageIO.getImageReadersByFormatName("TIFF");
                        if (readers.hasNext()) {
                            ImageReader reader = (ImageReader) readers.next();
                            reader.setInput(metadataStream);
                            IIOMetadata iioMetadata = reader.getImageMetadata(0);
                            this.metadata = TIFFDirectory.createFromMetadata(iioMetadata);
                            TIFFField exifField = this.metadata
                                    .getTIFFField(EXIFParentTIFFTagSet.TAG_EXIF_IFD_POINTER);
                        }
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                } else if (data.length > 28 && data[28] == 0) {
                    String id = null;
                    try {
                        id = new String(data, 0, 28, "utf-8");
                    } catch (UnsupportedEncodingException e) {
                        log.error(e);
                    }
                    if ("http://ns.adobe.com/xap/1.0/".equals(id)) {

                        // XMP metadata
                        try {
                            String xmpPacket = new String(data, 29, data.length - 29, "utf-8");
                            XMPMeta xmp = XMPMetaFactory.parseFromString(xmpPacket);
                            log.debug("Found XMP metadata");
                        } catch (XMPException e) {
                            log.warn("caught XMP exception while parsing metadata", e);
                        } catch (UnsupportedEncodingException e) {
                            log.error(e);
                        }
                    }
                }
            }
        }
    }
}