Java Code Examples for javax.imageio.metadata.IIOMetadata#isReadOnly()
The following examples show how to use
javax.imageio.metadata.IIOMetadata#isReadOnly() .
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: GLChartPanel.java From MeteoInfo with GNU Lesser General Public License v3.0 | 5 votes |
/** * 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 2
Source File: Export.java From PDV with GNU General Public License v3.0 | 5 votes |
/** * 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 3
Source File: PNGCanvas.java From SwissQRBill with MIT License | 5 votes |
/** * Saves image as PDF and stores meta data to indicate the resolution. */ private static void createPNG(BufferedImage image, OutputStream os, int resolution) throws IOException { ImageWriter writer = null; ImageWriteParam writeParam = null; IIOMetadata metadata = null; for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName("png"); iw.hasNext(); ) { writer = iw.next(); writeParam = writer.getDefaultWriteParam(); ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(image.getType()); metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam); if (!metadata.isReadOnly() && metadata.isStandardMetadataFormatSupported()) break; } if (writer == null || writeParam == null) throw new QRBillGenerationException("No valid PNG writer found"); addDpiMetadata(metadata, resolution); addTextMetadata(metadata); try (ImageOutputStream stream = ImageIO.createImageOutputStream(os)) { writer.setOutput(stream); writer.write(metadata, new IIOImage(image, null, metadata), writeParam); } }
Example 4
Source File: LegendManager.java From sldeditor with GNU General Public License v3.0 | 5 votes |
/** * 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 5
Source File: SinglePageDocument.java From geomajas-project-server with GNU Affero General Public License v3.0 | 5 votes |
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 6
Source File: ChartPanel.java From MeteoInfo with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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 7
Source File: MapLayout.java From MeteoInfo with GNU Lesser General Public License v3.0 | 4 votes |
/** * Export to a picture file * * @param fileName File path * @param dpi DPI * @throws java.io.FileNotFoundException * @throws javax.print.PrintException */ public void exportToPicture(String fileName, Integer dpi) throws FileNotFoundException, PrintException, IOException { if (dpi == null) { exportToPicture(fileName); } else { File output = new File(fileName); output.delete(); int width = _pageBounds.width; int height = _pageBounds.height; 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); 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.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) { continue; } ImageUtil.setDPI(metadata, dpi); 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 8
Source File: ImageJpgFile.java From MyBox with Apache License 2.0 | 4 votes |
public static IIOMetadata getWriterMeta2(ImageAttributes attributes, BufferedImage image, ImageWriter writer, ImageWriteParam param) { try { IIOMetadata metaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(image), param); if (metaData == null || metaData.isReadOnly() || attributes == null) { return metaData; } String nativeFormat = metaData.getNativeMetadataFormatName();// "javax_imageio_jpeg_image_1.0" IIOMetadataNode root = new IIOMetadataNode(nativeFormat); IIOMetadataNode jpegVariety = new IIOMetadataNode("JPEGvariety"); IIOMetadataNode markerSequence = new IIOMetadataNode("markerSequence"); IIOMetadataNode app0JFIF = new IIOMetadataNode("app0JFIF"); root.appendChild(jpegVariety); root.appendChild(markerSequence); jpegVariety.appendChild(app0JFIF); app0JFIF.setAttribute("majorVersion", "1"); app0JFIF.setAttribute("minorVersion", "2"); app0JFIF.setAttribute("thumbWidth", "0"); app0JFIF.setAttribute("thumbHeight", "0"); if (attributes.getDensity() > 0) { app0JFIF.setAttribute("Xdensity", attributes.getDensity() + ""); app0JFIF.setAttribute("Ydensity", attributes.getDensity() + ""); app0JFIF.setAttribute("resUnits", "1"); // density is dots per inch } if (attributes.isEmbedProfile() && attributes.getProfile() != null) { IIOMetadataNode app2ICC = new IIOMetadataNode("app2ICC"); app0JFIF.appendChild(app2ICC); app2ICC.setUserObject(attributes.getProfile()); } metaData.mergeTree(nativeFormat, root); return metaData; } catch (Exception e) { logger.error(e.toString()); return null; } }
Example 9
Source File: ImageTiffFile.java From MyBox with Apache License 2.0 | 4 votes |
public static IIOMetadata getWriterMeta(ImageAttributes attributes, BufferedImage image, ImageWriter writer, ImageWriteParam param) { try { IIOMetadata metaData; if (attributes != null && attributes.getColorType() != null) { ImageTypeSpecifier imageTypeSpecifier; switch (attributes.getColorType()) { case ARGB: imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB); break; case RGB: imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB); break; case BINARY: imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_BINARY); break; case GRAY: imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_GRAY); break; default: imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB); break; } metaData = writer.getDefaultImageMetadata(imageTypeSpecifier, param); } else if (image != null) { metaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(image), param); } else { metaData = writer.getDefaultImageMetadata( ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB), param); } if (metaData == null || metaData.isReadOnly() || attributes == null) { return metaData; } if (attributes.getDensity() > 0) { String nativeName = metaData.getNativeMetadataFormatName(); // javax_imageio_tiff_image_1.0 Node nativeTree = metaData.getAsTree(nativeName); long[] xRes = new long[]{attributes.getDensity(), 1}; long[] yRes = new long[]{attributes.getDensity(), 1}; TIFFField fieldXRes = new TIFFField( BaselineTIFFTagSet.getInstance().getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION), TIFFTag.TIFF_RATIONAL, 1, new long[][]{xRes}); TIFFField fieldYRes = new TIFFField( BaselineTIFFTagSet.getInstance().getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION), TIFFTag.TIFF_RATIONAL, 1, new long[][]{yRes}); nativeTree.getFirstChild().appendChild(fieldXRes.getAsNativeNode()); nativeTree.getFirstChild().appendChild(fieldYRes.getAsNativeNode()); char[] fieldUnit = new char[]{BaselineTIFFTagSet.RESOLUTION_UNIT_INCH}; TIFFField fieldResUnit = new TIFFField( BaselineTIFFTagSet.getInstance().getTag(BaselineTIFFTagSet.TAG_RESOLUTION_UNIT), TIFFTag.TIFF_SHORT, 1, fieldUnit); nativeTree.getFirstChild().appendChild(fieldResUnit.getAsNativeNode()); metaData.mergeTree(nativeName, nativeTree); } return metaData; } catch (Exception e) { logger.error(e.toString()); return null; } }