Java Code Examples for javax.imageio.ImageWriteParam#setCompressionQuality()
The following examples show how to use
javax.imageio.ImageWriteParam#setCompressionQuality() .
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: HeatChart.java From JuiceboxLegacy with MIT License | 6 votes |
private void saveGraphicJpeg(BufferedImage chart, File outputFile, float quality) throws IOException { // Setup correct compression for jpeg. Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(quality); // Output the image. FileImageOutputStream output = new FileImageOutputStream(outputFile); writer.setOutput(output); IIOImage image = new IIOImage(chart, null, null); writer.write(null, image, iwp); writer.dispose(); }
Example 2
Source File: CommUtils.java From tools-ocr with GNU Lesser General Public License v3.0 | 6 votes |
public static byte[] imageToBytes(BufferedImage img) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); MemoryCacheImageOutputStream outputStream = new MemoryCacheImageOutputStream(byteArrayOutputStream); try { Iterator iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = (ImageWriter) iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(IMAGE_QUALITY); writer.setOutput(outputStream); IIOImage image = new IIOImage(img, null, null); writer.write(null, image, iwp); writer.dispose(); byte[] result = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); outputStream.close(); return result; } catch (IOException e) { StaticLog.error(e); return new byte[0]; } }
Example 3
Source File: SunJPEGEncoderAdapter.java From buffer_bci with GNU General Public License v3.0 | 6 votes |
/** * Encodes an image in JPEG format and writes it to an output stream. * * @param bufferedImage the image to be encoded (<code>null</code> not * permitted). * @param outputStream the OutputStream to write the encoded image to * (<code>null</code> not permitted). * * @throws IOException if there is an I/O problem. * @throws NullPointerException if <code>bufferedImage</code> is * <code>null</code>. */ @Override public void encode(BufferedImage bufferedImage, OutputStream outputStream) throws IOException { ParamChecks.nullNotPermitted(bufferedImage, "bufferedImage"); ParamChecks.nullNotPermitted(outputStream, "outputStream"); Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = (ImageWriter) iterator.next(); ImageWriteParam p = writer.getDefaultWriteParam(); p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); p.setCompressionQuality(this.quality); ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream); writer.setOutput(ios); writer.write(null, new IIOImage(bufferedImage, null, null), p); ios.flush(); writer.dispose(); ios.close(); }
Example 4
Source File: ImageFileWriters.java From MyBox with Apache License 2.0 | 6 votes |
public static ImageWriteParam getWriterParam(ImageAttributes attributes, ImageWriter writer) { try { ImageWriteParam param = writer.getDefaultWriteParam(); if (param.canWriteCompressed()) { param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); if (attributes != null && attributes.getCompressionType() != null) { param.setCompressionType(attributes.getCompressionType()); } else { String[] compressionTypes = param.getCompressionTypes(); if (compressionTypes != null) { param.setCompressionType(compressionTypes[0]); } } if (attributes != null && attributes.getQuality() > 0) { param.setCompressionQuality(attributes.getQuality() / 100.0f); } else { param.setCompressionQuality(1.0f); } } return param; } catch (Exception e) { logger.error(e.toString()); return null; } }
Example 5
Source File: ImageHelper.java From olat with Apache License 2.0 | 6 votes |
private static ImageWriteParam getOptimizedImageWriteParam(ImageWriter writer, Size scaledSize) { ImageWriteParam iwp = writer.getDefaultWriteParam(); try { 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; }
Example 6
Source File: SunJPEGEncoderAdapter.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Encodes an image in JPEG format and writes it to an output stream. * * @param bufferedImage the image to be encoded (<code>null</code> not * permitted). * @param outputStream the OutputStream to write the encoded image to * (<code>null</code> not permitted). * * @throws IOException if there is an I/O problem. * @throws NullPointerException if <code>bufferedImage</code> is * <code>null</code>. */ public void encode(BufferedImage bufferedImage, OutputStream outputStream) throws IOException { if (bufferedImage == null) { throw new IllegalArgumentException("Null 'image' argument."); } if (outputStream == null) { throw new IllegalArgumentException("Null 'outputStream' argument."); } Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = (ImageWriter) iterator.next(); ImageWriteParam p = writer.getDefaultWriteParam(); p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); p.setCompressionQuality(this.quality); ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream); writer.setOutput(ios); writer.write(null, new IIOImage(bufferedImage, null, null), p); ios.flush(); writer.dispose(); ios.close(); }
Example 7
Source File: ImagePnmFile.java From MyBox with Apache License 2.0 | 6 votes |
public static ImageWriteParam getPara(ImageAttributes attributes, ImageWriter writer) { try { ImageWriteParam param = writer.getDefaultWriteParam(); if (param.canWriteCompressed()) { param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); if (attributes != null && attributes.getCompressionType() != null) { param.setCompressionType(attributes.getCompressionType()); } if (attributes != null && attributes.getQuality() > 0) { param.setCompressionQuality(attributes.getQuality() / 100.0f); } else { param.setCompressionQuality(1.0f); } } return param; } catch (Exception e) { logger.error(e.toString()); return null; } }
Example 8
Source File: SunJPEGEncoderAdapter.java From ECG-Viewer with GNU General Public License v2.0 | 6 votes |
/** * Encodes an image in JPEG format and writes it to an output stream. * * @param bufferedImage the image to be encoded (<code>null</code> not * permitted). * @param outputStream the OutputStream to write the encoded image to * (<code>null</code> not permitted). * * @throws IOException if there is an I/O problem. * @throws NullPointerException if <code>bufferedImage</code> is * <code>null</code>. */ @Override public void encode(BufferedImage bufferedImage, OutputStream outputStream) throws IOException { ParamChecks.nullNotPermitted(bufferedImage, "bufferedImage"); ParamChecks.nullNotPermitted(outputStream, "outputStream"); Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = (ImageWriter) iterator.next(); ImageWriteParam p = writer.getDefaultWriteParam(); p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); p.setCompressionQuality(this.quality); ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream); writer.setOutput(ios); writer.write(null, new IIOImage(bufferedImage, null, null), p); ios.flush(); writer.dispose(); ios.close(); }
Example 9
Source File: ScreenCapture.java From logbook-kai with MIT License | 6 votes |
/** * BufferedImageをJPEG形式にエンコードします * * @param image BufferedImage * @return JPEG形式の画像 * @throws IOException 入出力例外 */ static byte[] encodeJpeg(BufferedImage image) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try (ImageOutputStream ios = ImageIO.createImageOutputStream(out)) { ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next(); try { ImageWriteParam iwp = writer.getDefaultWriteParam(); if (iwp.canWriteCompressed()) { iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(QUALITY); } writer.setOutput(ios); writer.write(null, new IIOImage(image, null, null), iwp); } finally { writer.dispose(); } } return out.toByteArray(); }
Example 10
Source File: SunJPEGEncoderAdapter.java From buffer_bci with GNU General Public License v3.0 | 6 votes |
/** * Encodes an image in JPEG format and writes it to an output stream. * * @param bufferedImage the image to be encoded (<code>null</code> not * permitted). * @param outputStream the OutputStream to write the encoded image to * (<code>null</code> not permitted). * * @throws IOException if there is an I/O problem. * @throws NullPointerException if <code>bufferedImage</code> is * <code>null</code>. */ @Override public void encode(BufferedImage bufferedImage, OutputStream outputStream) throws IOException { ParamChecks.nullNotPermitted(bufferedImage, "bufferedImage"); ParamChecks.nullNotPermitted(outputStream, "outputStream"); Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = (ImageWriter) iterator.next(); ImageWriteParam p = writer.getDefaultWriteParam(); p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); p.setCompressionQuality(this.quality); ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream); writer.setOutput(ios); writer.write(null, new IIOImage(bufferedImage, null, null), p); ios.flush(); writer.dispose(); ios.close(); }
Example 11
Source File: SunJPEGEncoderAdapter.java From SIMVA-SoS with Apache License 2.0 | 6 votes |
/** * Encodes an image in JPEG format and writes it to an output stream. * * @param bufferedImage the image to be encoded (<code>null</code> not * permitted). * @param outputStream the OutputStream to write the encoded image to * (<code>null</code> not permitted). * * @throws IOException if there is an I/O problem. * @throws NullPointerException if <code>bufferedImage</code> is * <code>null</code>. */ @Override public void encode(BufferedImage bufferedImage, OutputStream outputStream) throws IOException { ParamChecks.nullNotPermitted(bufferedImage, "bufferedImage"); ParamChecks.nullNotPermitted(outputStream, "outputStream"); Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = (ImageWriter) iterator.next(); ImageWriteParam p = writer.getDefaultWriteParam(); p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); p.setCompressionQuality(this.quality); ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream); writer.setOutput(ios); writer.write(null, new IIOImage(bufferedImage, null, null), p); ios.flush(); writer.dispose(); ios.close(); }
Example 12
Source File: ImageHandler.java From density-converter with Apache License 2.0 | 5 votes |
private void compressJpeg(BufferedImage bufferedImage, CompoundDirectory exif, float quality, File targetFile) throws IOException { ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next(); ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam(); jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); jpgWriteParam.setCompressionQuality(quality); ImageWriter writer = null; try (ImageOutputStream outputStream = new FileImageOutputStream(targetFile)) { writer = ImageIO.getImageWritersByFormatName("jpg").next(); writer.setOutput(outputStream); writer.write(null, new IIOImage(bufferedImage, null, null), jpgWriteParam); } finally { if (writer != null) writer.dispose(); } }
Example 13
Source File: WebPWriterTest.java From webp-imageio with Apache License 2.0 | 5 votes |
/** * Test method for * {@link com.luciad.imageio.webp.WebPWriter#write(javax.imageio.metadata.IIOMetadata, javax.imageio.IIOImage, javax.imageio.ImageWriteParam)} * . * * @throws IOException */ @Test(dataProvider = "createImagesWithCompressionOptions", enabled = true) public void testImageWriterCompression(final RenderedImage im, final String compressionType, final float compressionQuality, final String outputName) throws IOException { final String extension = outputName.substring(outputName.lastIndexOf(".") + 1); // get writer final ImageWriter imgWriter = ImageIO.getImageWritersByFormatName(extension).next(); final ImageWriteParam imgWriteParams = new WebPWriteParam(null); imgWriteParams.setCompressionType(compressionType); // 0~1 imgWriteParams.setCompressionQuality(compressionQuality); final String testName = "CompressionOptions"; final File file = createOutputFile(testName, outputName); final ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(file); try { imgWriter.setOutput(imageOutputStream); imgWriter.write(null, new IIOImage(im, null, null), imgWriteParams); final int length = (int) imageOutputStream.length(); assertTrue(length > 0); } finally { try { imageOutputStream.close(); } catch (final IOException e) { } } }
Example 14
Source File: JPEGMovWriter.java From pumpernickel with MIT License | 5 votes |
@Override protected void writeFrame(OutputStream out, BufferedImage image, Map<String, Object> settings) throws IOException { if (image.getType() == BufferedImage.TYPE_INT_ARGB || image.getType() == BufferedImage.TYPE_INT_ARGB_PRE) { if (printWarning == false) { printWarning = true; System.err .println("JPEGMovWriter Warning: a BufferedImage of type TYPE_INT_ARGB may produce unexpected results. The recommended type is TYPE_INT_RGB."); } } float quality; if (settings != null && settings.get(PROPERTY_QUALITY) instanceof Number) { quality = ((Number) settings.get(PROPERTY_QUALITY)).floatValue(); } else if (settings != null && settings.get(PROPERTY_QUALITY) instanceof String) { quality = Float.parseFloat((String) settings.get(PROPERTY_QUALITY)); } else { quality = defaultQuality; } MemoryCacheImageOutputStream iOut = new MemoryCacheImageOutputStream( out); ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/jpeg").next(); ImageWriteParam iwParam = iw.getDefaultWriteParam(); iwParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwParam.setCompressionQuality(quality); iw.setOutput(iOut); IIOImage img = new IIOImage(image, null, null); iw.write(null, img, iwParam); }
Example 15
Source File: JpegWriter.java From scrimage with Apache License 2.0 | 5 votes |
@Override public void write(AwtImage image, ImageMetadata metadata, OutputStream out) throws IOException { javax.imageio.ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam params = writer.getDefaultWriteParam(); if (compression < 100) { params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); params.setCompressionQuality(compression / 100f); } if (progressive) { params.setProgressiveMode(ImageWriteParam.MODE_DEFAULT); } else { params.setProgressiveMode(ImageWriteParam.MODE_DISABLED); } // in openjdk, awt cannot write out jpegs that have a transparency bit, even if that is set to 255. // see http://stackoverflow.com/questions/464825/converting-transparent-gif-png-to-jpeg-using-java // so have to convert to a non alpha type BufferedImage noAlpha; if (image.awt().getColorModel().hasAlpha()) { noAlpha = image.toImmutableImage().removeTransparency(java.awt.Color.WHITE).toNewBufferedImage(BufferedImage.TYPE_INT_RGB); } else { noAlpha = image.awt(); } MemoryCacheImageOutputStream output = new MemoryCacheImageOutputStream(out); writer.setOutput(output); writer.write(null, new IIOImage(noAlpha, null, null), params); output.close(); writer.dispose(); // IOUtils.closeQuietly(out); }
Example 16
Source File: ImageScalarImpl.java From jweb-cms with GNU Affero General Public License v3.0 | 5 votes |
private void writeJPG(BufferedImage bufferedImage, OutputStream outputStream, double quality) throws IOException { Iterator<ImageWriter> iterator = ImageIO.getImageWritersByFormatName("jpg"); ImageWriter imageWriter = iterator.next(); ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality((float) quality); ImageOutputStream imageOutputStream = new MemoryCacheImageOutputStream(outputStream); imageWriter.setOutput(imageOutputStream); IIOImage iioimage = new IIOImage(bufferedImage, null, null); imageWriter.write(null, iioimage, imageWriteParam); imageOutputStream.flush(); }
Example 17
Source File: ShrinkPDF.java From shrink-pdf with MIT License | 5 votes |
/** * Shrink a PDF * @param f {@code File} pointing to the PDF to shrink * @param compQual Compression quality parameter. 0 is * smallest file, 1 is highest quality. * @return The compressed {@code PDDocument} * @throws FileNotFoundException * @throws IOException */ private PDDocument shrinkMe() throws FileNotFoundException, IOException { if(compQual < 0) compQual = compQualDefault; final RandomAccessBufferedFileInputStream rabfis = new RandomAccessBufferedFileInputStream(input); final PDFParser parser = new PDFParser(rabfis); parser.parse(); final PDDocument doc = parser.getPDDocument(); final PDPageTree pages = doc.getPages(); final ImageWriter imgWriter; final ImageWriteParam iwp; if(tiff) { final Iterator<ImageWriter> tiffWriters = ImageIO.getImageWritersBySuffix("png"); imgWriter = tiffWriters.next(); iwp = imgWriter.getDefaultWriteParam(); //iwp.setCompressionMode(ImageWriteParam.MODE_DISABLED); } else { final Iterator<ImageWriter> jpgWriters = ImageIO.getImageWritersByFormatName("jpeg"); imgWriter = jpgWriters.next(); iwp = imgWriter.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(compQual); } for(PDPage p : pages) { scanResources(p.getResources(), doc, imgWriter, iwp); } return doc; }
Example 18
Source File: ImageUtils.java From RemoteSupportTool with Apache License 2.0 | 5 votes |
public static void write(BufferedImage image, OutputStream output, float compression) throws IOException { ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next(); ImageOutputStream jpgStream = ImageIO.createImageOutputStream(output); // Configure JPEG compression ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam(); jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); jpgWriteParam.setCompressionQuality(compression); jpgWriter.setOutput(jpgStream); jpgWriter.write(null, new IIOImage(image, null, null), jpgWriteParam); jpgWriter.dispose(); output.flush(); }
Example 19
Source File: PhotographUploadBean.java From fenixedu-academic with GNU Lesser General Public License v3.0 | 4 votes |
public void processImage() throws IOException, UnableToProcessTheImage { if (base64RawContent != null && base64RawThumbnail != null) { return; } try { BufferedImage image = ImageIO.read(new ByteArrayInputStream(rawContents)); if (image == null) { throw new UnableToProcessTheImage(); } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // calculate resize factor double resizeFactor = Math.min((double) OUTPUT_PHOTO_WIDTH / image.getWidth(), (double) OUTPUT_PHOTO_HEIGHT / image.getHeight()); if (resizeFactor == 1) { compressedContents = rawContents; } else { // resize image AffineTransform tx = new AffineTransform(); tx.scale(resizeFactor, resizeFactor); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); image = op.filter(image, null); // set compression ImageWriter writer = ImageIO.getImageWritersByMIMEType(ContentType.getContentType(contentType).getMimeType()).next(); ImageWriteParam param = writer.getDefaultWriteParam(); if (contentType.equals(ContentType.JPG)) { param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(1); } // write to stream writer.setOutput(ImageIO.createImageOutputStream(outputStream)); writer.write(null, new IIOImage(image, null, null), param); compressedContents = outputStream.toByteArray(); } } catch (final CMMException ex) { throw new UnableToProcessTheImage(); } }
Example 20
Source File: ImageGifFile.java From MyBox with Apache License 2.0 | 4 votes |
public static boolean getParaMeta(long interval, boolean loop, GIFImageWriter gifWriter, ImageWriteParam param, GIFImageMetadata metaData) { try { param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionType("LZW"); param.setCompressionQuality(1); String delay; if (interval > 0) { delay = interval / 10 + ""; } else { delay = "100"; } String format = metaData.getNativeMetadataFormatName(); IIOMetadataNode tree = (IIOMetadataNode) metaData.getAsTree(format); IIOMetadataNode graphicsControlExtensionNode = new IIOMetadataNode("GraphicControlExtension"); graphicsControlExtensionNode.setAttribute("delayTime", delay); graphicsControlExtensionNode.setAttribute("disposalMethod", "restoreToBackgroundColor"); graphicsControlExtensionNode.setAttribute("userInputFlag", "false"); graphicsControlExtensionNode.setAttribute("transparentColorFlag", "false"); graphicsControlExtensionNode.setAttribute("delayTime", delay); graphicsControlExtensionNode.setAttribute("transparentColorIndex", "0"); tree.appendChild(graphicsControlExtensionNode); if (loop) { IIOMetadataNode applicationExtensionsNode = new IIOMetadataNode("ApplicationExtensions"); IIOMetadataNode applicationExtensionNode = new IIOMetadataNode("ApplicationExtension"); applicationExtensionNode.setAttribute("applicationID", "NETSCAPE"); applicationExtensionNode.setAttribute("authenticationCode", "2.0"); byte[] k = {1, 0, 0}; applicationExtensionNode.setUserObject(k); applicationExtensionsNode.appendChild(applicationExtensionNode); tree.appendChild(applicationExtensionsNode); } metaData.mergeTree(format, tree); return true; } catch (Exception e) { logger.error(e.toString()); return false; } }