List of usage examples for javax.imageio.metadata IIOMetadataNode getAttribute
public String getAttribute(String name)
From source file:com.sketchy.utils.image.SketchyImage.java
public static SketchyImage load(File file) throws Exception { SketchyImage sketchyImage = null;//from w w w .j a v a 2s . c o m if (!file.exists() || !file.canRead()) { throw new Exception("Can not find or read File: " + file.getPath() + "!"); } if (!StringUtils.endsWithIgnoreCase(file.getName(), ".png")) { throw new Exception("Can not load SketchyImage! Must be a .png file!"); } Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByFormatName("png"); ImageReader imageReader = null; if (imageReaders.hasNext()) { // Just get first one imageReader = imageReaders.next(); } if (imageReader == null) { // this should never happen!! if so.. we got problems throw new Exception("Can not find ImageReader for .png Files!"); } ImageInputStream is = null; try { is = ImageIO.createImageInputStream(file); imageReader.setInput(is, true); IIOMetadata metaData = imageReader.getImageMetadata(0); // always get first image IIOMetadataNode metaDataNode = (IIOMetadataNode) metaData .getAsTree(metaData.getNativeMetadataFormatName()); if (metaDataNode == null) { throw new Exception("Error retreiving MetaData properties from .png File!"); } NodeList childNodes = metaDataNode.getElementsByTagName("pHYs"); // only look in the first node if (childNodes.getLength() == 0) { throw new Exception("Invalid SketchyImage file. It must contain 'pixelsPerUnit' MetaData!"); } IIOMetadataNode physNode = (IIOMetadataNode) childNodes.item(0); String pixelsPerUnitXAxisAttribute = physNode.getAttribute("pixelsPerUnitXAxis"); String pixelsPerUnitYAxisAttribute = physNode.getAttribute("pixelsPerUnitYAxis"); // String unitSpecifierAttribute = physNode.getAttribute("unitSpecifier"); Just assuming meter if (StringUtils.isBlank(pixelsPerUnitXAxisAttribute)) { throw new Exception("Invalid SketchyImage file. It must contain 'pixelsPerUnitXAxis' MetaData!"); } if (StringUtils.isBlank(pixelsPerUnitYAxisAttribute)) { throw new Exception("Invalid SketchyImage file. It must contain 'pixelsPerUnitYAxis' MetaData!"); } int pixelsPerUnitXAxis; try { pixelsPerUnitXAxis = Integer.parseInt(pixelsPerUnitXAxisAttribute); if (pixelsPerUnitXAxis <= 0) throw new Exception("Value must be > 0"); } catch (Exception e) { throw new Exception("Invalid 'pixelsPerUnitXAxis' MetaData Attribute! " + e.getMessage()); } int pixelsPerUnitYAxis; try { pixelsPerUnitYAxis = Integer.parseInt(pixelsPerUnitYAxisAttribute); if (pixelsPerUnitYAxis <= 0) throw new Exception("Value must be > 0"); } catch (Exception e) { throw new Exception("Invalid 'pixelsPerUnitYAxis' MetaData Attribute! " + e.getMessage()); } // We successfully processed the MetaData.. now read/set the image BufferedImage bufferedImage = imageReader.read(0); // always get first image double xPixelsPerMM = pixelsPerUnitXAxis / 1000.0; double yPixelsPerMM = pixelsPerUnitYAxis / 1000.0; sketchyImage = new SketchyImage(bufferedImage, xPixelsPerMM, yPixelsPerMM); } catch (Exception e) { throw new Exception("Error Loading SketchyImage File: " + file.getPath() + "! " + e.getMessage()); } finally { IOUtils.closeQuietly(is); } return sketchyImage; }
From source file:net.rptools.tokentool.util.ImageUtil.java
private static ImageView getImage(ImageView thumbView, final Path filePath, final boolean overlayWanted, final int THUMB_SIZE) throws IOException { Image thumb = null;//from w ww . jav a2 s . co m String fileURL = filePath.toUri().toURL().toString(); if (ImageUtil.SUPPORTED_IMAGE_FILE_FILTER.accept(null, fileURL)) { if (THUMB_SIZE <= 0) thumb = processMagenta(new Image(fileURL), COLOR_THRESHOLD, overlayWanted); else thumb = processMagenta(new Image(fileURL, THUMB_SIZE, THUMB_SIZE, true, true), COLOR_THRESHOLD, overlayWanted); } else if (ImageUtil.PSD_FILE_FILTER.accept(null, fileURL)) { ImageInputStream is = null; PSDImageReader reader = null; int imageIndex = 1; // Mask layer should always be layer 1 and overlay image on layer 2. Note, layer 0 will be a combined layer composite if (overlayWanted) imageIndex = 2; File file = filePath.toFile(); try { is = ImageIO.createImageInputStream(file); if (is == null || is.length() == 0) { log.info("Image from file " + file.getAbsolutePath() + " is null"); } Iterator<ImageReader> iterator = ImageIO.getImageReaders(is); if (iterator == null || !iterator.hasNext()) { throw new IOException("Image file format not supported by ImageIO: " + filePath); } reader = (PSDImageReader) iterator.next(); reader.setInput(is); BufferedImage thumbBI; thumbBI = reader.read(imageIndex); if (thumbBI != null) { int layerIndex = 0; if (overlayWanted) layerIndex = 1; IIOMetadata metadata = reader.getImageMetadata(0); IIOMetadataNode root = (IIOMetadataNode) metadata .getAsTree(PSDMetadata.NATIVE_METADATA_FORMAT_NAME); NodeList layerInfos = root.getElementsByTagName("LayerInfo"); // Layer index corresponds to imageIndex - 1 in the reader IIOMetadataNode layerInfo = (IIOMetadataNode) layerInfos.item(layerIndex); // Get the width & height of the Mask layer so we can create the overlay the same size int width = reader.getWidth(0); int height = reader.getHeight(0); // Get layer offsets, PhotoShop PSD layers can have different widths/heights and all images start at 0,0 with a layer offset applied int x = Math.max(Integer.parseInt(layerInfo.getAttribute("left")), 0); int y = Math.max(Integer.parseInt(layerInfo.getAttribute("top")), 0); // Lets pad the overlay with transparency to make it the same size as the PSD canvas size thumb = resizeCanvas(SwingFXUtils.toFXImage(thumbBI, null), width, height, x, y); // Finally set ImageView to thumbnail size if (THUMB_SIZE > 0) { thumbView.setFitWidth(THUMB_SIZE); thumbView.setPreserveRatio(true); } } } catch (Exception e) { log.error("Processing: " + file.getAbsolutePath(), e); } finally { // Dispose reader in finally block to avoid memory leaks reader.dispose(); is.close(); } } thumbView.setImage(thumb); return thumbView; }
From source file:org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderImageIO.java
private ICC_Profile tryToExctractICCProfileFromPNGMetadataNode(Element pngNode) { ICC_Profile iccProf = null;//from w ww .j ava 2s .c o 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; }