List of usage examples for javax.imageio ImageWriteParam canWriteCompressed
boolean canWriteCompressed
To view the source code for javax.imageio ImageWriteParam canWriteCompressed.
Click Source Link
From source file:com.ackpdfbox.app.imageio.ImageIOUtil.java
/** * Writes a buffered image to a file using the given image format. * Compression is fixed for PNG, GIF, BMP and WBMP, dependent of the quality * parameter for JPG, and dependent of bit count for TIFF (a bitonal image * will be compressed with CCITT G4, a color image with LZW). Creating a * TIFF image is only supported if the jai_imageio library is in the class * path.// ww w. j ava2 s. c o m * * @param image the image to be written * @param formatName the target format (ex. "png") * @param output the output stream to be used for writing * @param dpi the resolution in dpi (dots per inch) to be used in metadata * @param quality quality to be used when compressing the image (0 < * quality < 1.0f) * @return true if the image file was produced, false if there was an error. * @throws IOException if an I/O error occurs */ public static boolean writeImage(BufferedImage image, String formatName, OutputStream output, int dpi, float quality) throws IOException { ImageOutputStream imageOutput = null; ImageWriter writer = null; try { // find suitable image writer Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(formatName); ImageWriteParam param = null; IIOMetadata metadata = null; // Loop until we get the best driver, i.e. one that supports // setting dpi in the standard metadata format; however we'd also // accept a driver that can't, if a better one can't be found while (writers.hasNext()) { if (writer != null) { writer.dispose(); } writer = writers.next(); param = writer.getDefaultWriteParam(); metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(image), param); if (metadata != null && !metadata.isReadOnly() && metadata.isStandardMetadataFormatSupported()) { break; } } if (writer == null) { LOG.error("No ImageWriter found for '" + formatName + "' format"); StringBuilder sb = new StringBuilder(); String[] writerFormatNames = ImageIO.getWriterFormatNames(); for (String fmt : writerFormatNames) { sb.append(fmt); sb.append(' '); } LOG.error("Supported formats: " + sb); return false; } // compression if (param != null && param.canWriteCompressed()) { param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); if (formatName.toLowerCase().startsWith("tif")) { // TIFF compression TIFFUtil.setCompressionType(param, image); } else { param.setCompressionType(param.getCompressionTypes()[0]); param.setCompressionQuality(quality); } } if (formatName.toLowerCase().startsWith("tif")) { // TIFF metadata TIFFUtil.updateMetadata(metadata, image, dpi); } else if ("jpeg".equals(formatName.toLowerCase()) || "jpg".equals(formatName.toLowerCase())) { // This segment must be run before other meta operations, // or else "IIOInvalidTreeException: Invalid node: app0JFIF" // The other (general) "meta" methods may not be used, because // this will break the reading of the meta data in tests JPEGUtil.updateMetadata(metadata, dpi); } else { // write metadata is possible if (metadata != null && !metadata.isReadOnly() && metadata.isStandardMetadataFormatSupported()) { setDPI(metadata, dpi, formatName); } } // write imageOutput = ImageIO.createImageOutputStream(output); writer.setOutput(imageOutput); writer.write(null, new IIOImage(image, null, metadata), param); } finally { if (writer != null) { writer.dispose(); } if (imageOutput != null) { imageOutput.close(); } } return true; }
From source file:org.dcm4che.tool.dcm2jpg.Dcm2Jpg.java
public static void listSupportedImageWriters(String format) { System.out.println(MessageFormat.format(rb.getString("writers"), format)); Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(format); while (it.hasNext()) { ImageWriter writer = it.next(); ImageWriteParam param = writer.getDefaultWriteParam(); System.out.println(MessageFormat.format(rb.getString("writer"), writer.getClass().getName(), param.canWriteCompressed(), param.canWriteProgressive(), param.canWriteTiles(), param.canOffsetTiles(),//from w w w .j a v a 2 s . c o m param.canWriteCompressed() ? Arrays.toString(param.getCompressionTypes()) : null)); } }
From source file:org.dcm4che2.tool.dcm2jpg.Dcm2Jpg.java
private void encodeByImageIO(BufferedImage bi, File dest) throws IOException { ImageWriter writer = getImageWriter(imageWriterClassname); ImageOutputStream out = null; try {// w w w . j av a 2 s . com out = ImageIO.createImageOutputStream(dest); writer.setOutput(out); ImageWriteParam iwparam = writer.getDefaultWriteParam(); if (iwparam.canWriteCompressed()) { iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); String[] compressionTypes = iwparam.getCompressionTypes(); if (compressionTypes != null && compressionTypes.length > 0) { if (compressionType != null || iwparam.getCompressionType() == null) { for (int i = 0; i < compressionTypes.length; i++) { if (compressionType == null || compressionTypes[i].compareToIgnoreCase(compressionType) == 0) { iwparam.setCompressionType(compressionTypes[i]); break; } } } } if (imageQuality != null) iwparam.setCompressionQuality(imageQuality); } else if (imageQuality != null) { System.out.println("Selected Image Writer can not compress! imageQuality is ignored!"); } writer.write(null, new IIOImage(bi, null, null), iwparam); } finally { CloseUtils.safeClose(out); writer.dispose(); } }
From source file:org.dcm4che2.tool.dcm2jpg.Dcm2Jpg.java
private void showImageWriters() { ImageWriter writer;//from w w w . j a v a 2 s. c om System.out.println("ImageWriters for format name:" + formatName); int i = 0; for (Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(formatName); it.hasNext();) { writer = it.next(); System.out.println("Writer[" + (i++) + "]: " + writer.getClass().getName() + ":"); System.out.println(" Write Param:"); ImageWriteParam param = writer.getDefaultWriteParam(); System.out.println(" canWriteCompressed:" + param.canWriteCompressed()); System.out.println(" canWriteProgressive:" + param.canWriteProgressive()); System.out.println(" canWriteTiles:" + param.canWriteTiles()); System.out.println(" canOffsetTiles:" + param.canOffsetTiles()); if (param.canWriteCompressed()) { String[] types = param.getCompressionTypes(); System.out.println(" Compression Types:"); if (types != null && types.length > 0) { for (int j = 0; j < types.length; j++) { System.out.println(" Type[" + j + "]:" + types[j]); } } } System.out.println("-----------------------------"); } }
From source file:org.egov.infra.utils.ImageUtils.java
public static File compressImage(final InputStream imageStream, String imageFileName, boolean closeStream) throws IOException { File compressedImage = Paths.get(imageFileName).toFile(); try (final ImageOutputStream imageOutput = createImageOutputStream(compressedImage)) { ImageWriter writer = getImageWritersByFormatName( defaultString(getExtension(imageFileName), JPG_FORMAT_NAME)).next(); writer.setOutput(imageOutput);/*from www.j a va 2s. co m*/ ImageWriteParam writeParam = writer.getDefaultWriteParam(); if (writeParam.canWriteCompressed()) { writeParam.setCompressionMode(MODE_EXPLICIT); writeParam.setCompressionType(writeParam.getCompressionTypes()[0]); writeParam.setCompressionQuality(0.05F); } writer.write(null, new IIOImage(read(imageStream), null, null), writeParam); writer.dispose(); if (closeStream) imageStream.close(); } return compressedImage; }
From source file:org.hippoecm.frontend.plugins.gallery.imageutil.ImageUtils.java
/** * Returns the data of a {@link BufferedImage} as a binary output stream. If the image is <code>null</code>, * a stream of zero bytes is returned.//from www . j ava2 s. c o m * * @param writer the writer to use for writing the image data. * @param image the image to write. * @param compressionQuality a float between 0 and 1 that indicates the desired compression quality. Values lower than * 0 will be interpreted as 0, values higher than 1 will be interpreted as 1. * * @return an output stream with the data of the given image. * * @throws IOException when creating the binary output stream failed. */ public static ByteArrayOutputStream writeImage(ImageWriter writer, BufferedImage image, float compressionQuality) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); if (image != null) { ImageOutputStream ios = null; try { ios = ImageIO.createImageOutputStream(out); writer.setOutput(ios); // write compressed images with high quality final ImageWriteParam writeParam = writer.getDefaultWriteParam(); if (writeParam.canWriteCompressed()) { String[] compressionTypes = writeParam.getCompressionTypes(); if (compressionTypes != null && compressionTypes.length > 0) { writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writeParam.setCompressionType(compressionTypes[0]); // ensure a compression quality between 0 and 1 float trimmedCompressionQuality = Math.max(compressionQuality, 0); trimmedCompressionQuality = Math.min(trimmedCompressionQuality, 1f); writeParam.setCompressionQuality(trimmedCompressionQuality); } } final IIOImage iioImage = new IIOImage(image, null, null); writer.write(null, iioImage, writeParam); ios.flush(); } finally { if (ios != null) { ios.close(); } } } return out; }
From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java
private static ImageWriteParam getOptimizedImageWriteParam(ImageWriter writer, Size scaledSize) { ImageWriteParam iwp = writer.getDefaultWriteParam(); try {//from w ww .j a v a 2 s . co m if (iwp.canWriteCompressed()) { iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); int maxSize = Math.max(scaledSize.getWidth(), scaledSize.getHeight()); if (maxSize <= 50) { iwp.setCompressionQuality(0.95f); } else if (maxSize <= 100) { iwp.setCompressionQuality(0.90f); } else if (maxSize <= 200) { iwp.setCompressionQuality(0.85f); } else if (maxSize <= 500) { iwp.setCompressionQuality(0.80f); } else { iwp.setCompressionQuality(0.75f); } } } catch (Exception e) { //bmp can be compressed but don't allow it!!! return writer.getDefaultWriteParam(); } return iwp; }
From source file:org.onehippo.forge.gallerymagick.core.command.ScalrProcessorUtils.java
/** * Resize the given image {@code sourceFile} with resizing it to {@code width} and {@code height} * and store the resized image to {@code targetFile}, with appending {@code extraOptions} in the command line if provided. * @param sourceFile source image file/*from w w w . j av a2s. c o m*/ * @param targetFile target image file * @param dimension image dimension * @param extraOptions extra command line options * @throws IOException if IO exception occurs */ public static void resizeImage(File sourceFile, File targetFile, ImageDimension dimension, String... extraOptions) throws IOException { if (dimension == null) { throw new IllegalArgumentException("Invalid dimension: " + dimension); } ImageReader reader = null; ImageWriter writer = null; try { reader = getImageReader(sourceFile); if (reader == null) { throw new IllegalArgumentException( "Unsupported image file name extension for reading: " + sourceFile); } writer = getImageWriter(targetFile); if (writer == null) { throw new IllegalArgumentException( "Unsupported image file name extension for writing: " + targetFile); } BufferedImage sourceImage = reader.read(0); BufferedImage resizedImage = Scalr.resize(sourceImage, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, dimension.getWidth(), dimension.getHeight()); final ImageWriteParam writeParam = writer.getDefaultWriteParam(); if (writeParam.canWriteCompressed()) { String[] compressionTypes = writeParam.getCompressionTypes(); if (compressionTypes != null && compressionTypes.length > 0) { writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writeParam.setCompressionType(compressionTypes[0]); writeParam.setCompressionQuality(1.0f); } } final IIOImage iioImage = new IIOImage(resizedImage, null, null); writer.write(null, iioImage, writeParam); } finally { if (reader != null) { reader.dispose(); } if (writer != null) { writer.dispose(); } } }