Java Code Examples for javax.imageio.ImageWriteParam#setCompressionMode()
The following examples show how to use
javax.imageio.ImageWriteParam#setCompressionMode() .
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: 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 2
Source File: HeatChart.java From SAX with GNU General Public License v2.0 | 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 = (ImageWriter) 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 3
Source File: SunJPEGEncoderAdapter.java From opensim-gui 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>. */ 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 4
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 5
Source File: TiffSingleImageWriter.java From sejda with GNU Affero General Public License v3.0 | 5 votes |
@Override ImageWriteParam newImageWriterParams(PdfToMultipleTiffParameters params) { ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionType(TIFF_COMPRESSION_TYPE_CACHE .get(ofNullable(params.getCompressionType()).orElse(TiffCompressionType.NONE))); return param; }
Example 6
Source File: TiffMultiImageWriter.java From sejda with GNU Affero General Public License v3.0 | 5 votes |
@Override ImageWriteParam newImageWriterParams(PdfToSingleTiffParameters params) { ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionType(TIFF_COMPRESSION_TYPE_CACHE .get(ofNullable(params.getCompressionType()).orElse(TiffCompressionType.NONE))); return param; }
Example 7
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 8
Source File: JpegOutput.java From Pixelitor with GNU General Public License v3.0 | 5 votes |
private static void writeJPGtoStream(BufferedImage image, ImageInputStream ios, JpegInfo config, ProgressTracker tracker) throws IOException { Iterator<ImageWriter> jpgWriters = ImageIO.getImageWritersByFormatName("jpg"); if (!jpgWriters.hasNext()) { throw new IllegalStateException("No JPG writers found"); } ImageWriter writer = jpgWriters.next(); ImageWriteParam imageWriteParam = writer.getDefaultWriteParam(); if (config.isProgressive()) { imageWriteParam.setProgressiveMode(MODE_DEFAULT); } else { imageWriteParam.setProgressiveMode(MODE_DISABLED); } imageWriteParam.setCompressionMode(MODE_EXPLICIT); imageWriteParam.setCompressionQuality(config.getQuality()); IIOImage iioImage = new IIOImage(image, null, null); writer.setOutput(ios); if (tracker != null) { writer.addIIOWriteProgressListener(new TrackerWriteProgressListener(tracker)); } writer.write(null, iioImage, imageWriteParam); ios.flush(); ios.close(); }
Example 9
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 10
Source File: BMPSubsamplingTest.java From hottub with GNU General Public License v2.0 | 4 votes |
public BMPSubsamplingTest() throws IOException { ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next(); ImageWriteParam wparam = writer.getDefaultWriteParam(); wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); String[] types = wparam.getCompressionTypes(); for (int t = 0; t < img_types.length; t++) { int img_type = img_types[t]; System.out.println("Test for " + getImageTypeName(img_type)); BufferedImage image = getTestImage(img_type); ImageTypeSpecifier specifier = new ImageTypeSpecifier(image); if (!writer.getOriginatingProvider().canEncodeImage(specifier)) { System.out.println("Writer does not support encoding this buffered image type."); continue; } for(int i=0; i<types.length; i++) { if ("BI_JPEG".equals(types[i])) { // exclude BI_JPEG from automatic test // due to color diffusion effect on the borders. continue; } if (canEncodeImage(types[i], specifier, img_type)) { System.out.println("compression type: " + types[i] + " Supported for " + getImageTypeName(img_type)); } else { System.out.println("compression type: " + types[i] + " NOT Supported for " + getImageTypeName(img_type)); continue; } ImageWriteParam imageWriteParam = getImageWriteParam(writer, types[i]); imageWriteParam.setSourceSubsampling(srcXSubsampling, srcYSubsampling, 0, 0); File outputFile = new File("subsampling_test_" + getImageTypeName(img_type) + "__" + types[i] + ".bmp"); ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile); writer.setOutput(ios); IIOImage iioImg = new IIOImage(image, null, null); writer.write(null, iioImg, imageWriteParam); ios.flush(); ios.close(); BufferedImage outputImage = ImageIO.read(outputFile); checkTestImage(outputImage); } } }
Example 11
Source File: BMPSubsamplingTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) { ImageWriteParam imageWriteParam = writer.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionType(value); return imageWriteParam; }
Example 12
Source File: BMPSubsamplingTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
public BMPSubsamplingTest() throws IOException { ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next(); ImageWriteParam wparam = writer.getDefaultWriteParam(); wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); String[] types = wparam.getCompressionTypes(); for (int t = 0; t < img_types.length; t++) { int img_type = img_types[t]; System.out.println("Test for " + getImageTypeName(img_type)); BufferedImage image = getTestImage(img_type); ImageTypeSpecifier specifier = new ImageTypeSpecifier(image); if (!writer.getOriginatingProvider().canEncodeImage(specifier)) { System.out.println("Writer does not support encoding this buffered image type."); continue; } for(int i=0; i<types.length; i++) { if ("BI_JPEG".equals(types[i])) { // exclude BI_JPEG from automatic test // due to color diffusion effect on the borders. continue; } if (canEncodeImage(types[i], specifier, img_type)) { System.out.println("compression type: " + types[i] + " Supported for " + getImageTypeName(img_type)); } else { System.out.println("compression type: " + types[i] + " NOT Supported for " + getImageTypeName(img_type)); continue; } ImageWriteParam imageWriteParam = getImageWriteParam(writer, types[i]); imageWriteParam.setSourceSubsampling(srcXSubsampling, srcYSubsampling, 0, 0); File outputFile = new File("subsampling_test_" + getImageTypeName(img_type) + "__" + types[i] + ".bmp"); ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile); writer.setOutput(ios); IIOImage iioImg = new IIOImage(image, null, null); writer.write(null, iioImg, imageWriteParam); ios.flush(); ios.close(); BufferedImage outputImage = ImageIO.read(outputFile); checkTestImage(outputImage); } } }
Example 13
Source File: BMPSubsamplingTest.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) { ImageWriteParam imageWriteParam = writer.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionType(value); return imageWriteParam; }
Example 14
Source File: BMPSubsamplingTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) { ImageWriteParam imageWriteParam = writer.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionType(value); return imageWriteParam; }
Example 15
Source File: BMPSubsamplingTest.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
public BMPSubsamplingTest() throws IOException { ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next(); ImageWriteParam wparam = writer.getDefaultWriteParam(); wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); String[] types = wparam.getCompressionTypes(); for (int t = 0; t < img_types.length; t++) { int img_type = img_types[t]; System.out.println("Test for " + getImageTypeName(img_type)); BufferedImage image = getTestImage(img_type); ImageTypeSpecifier specifier = new ImageTypeSpecifier(image); if (!writer.getOriginatingProvider().canEncodeImage(specifier)) { System.out.println("Writer does not support encoding this buffered image type."); continue; } for(int i=0; i<types.length; i++) { if ("BI_JPEG".equals(types[i])) { // exclude BI_JPEG from automatic test // due to color diffusion effect on the borders. continue; } if (canEncodeImage(types[i], specifier, img_type)) { System.out.println("compression type: " + types[i] + " Supported for " + getImageTypeName(img_type)); } else { System.out.println("compression type: " + types[i] + " NOT Supported for " + getImageTypeName(img_type)); continue; } ImageWriteParam imageWriteParam = getImageWriteParam(writer, types[i]); imageWriteParam.setSourceSubsampling(srcXSubsampling, srcYSubsampling, 0, 0); File outputFile = new File("subsampling_test_" + getImageTypeName(img_type) + "__" + types[i] + ".bmp"); ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile); writer.setOutput(ios); IIOImage iioImg = new IIOImage(image, null, null); writer.write(null, iioImg, imageWriteParam); ios.flush(); ios.close(); BufferedImage outputImage = ImageIO.read(outputFile); checkTestImage(outputImage); } } }
Example 16
Source File: JpegRendererImpl.java From birt with Eclipse Public License 1.0 | 4 votes |
protected void updateWriterParameters( ImageWriteParam iwp ) { float quality = isQualitySet ? jpegQuality : 0.95f; iwp.setCompressionMode( ImageWriteParam.MODE_EXPLICIT ); iwp.setCompressionQuality( quality ); }
Example 17
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 18
Source File: BMPSubsamplingTest.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
public BMPSubsamplingTest() throws IOException { ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next(); ImageWriteParam wparam = writer.getDefaultWriteParam(); wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); String[] types = wparam.getCompressionTypes(); for (int t = 0; t < img_types.length; t++) { int img_type = img_types[t]; System.out.println("Test for " + getImageTypeName(img_type)); BufferedImage image = getTestImage(img_type); ImageTypeSpecifier specifier = new ImageTypeSpecifier(image); if (!writer.getOriginatingProvider().canEncodeImage(specifier)) { System.out.println("Writer does not support encoding this buffered image type."); continue; } for(int i=0; i<types.length; i++) { if ("BI_JPEG".equals(types[i])) { // exclude BI_JPEG from automatic test // due to color diffusion effect on the borders. continue; } if (canEncodeImage(types[i], specifier, img_type)) { System.out.println("compression type: " + types[i] + " Supported for " + getImageTypeName(img_type)); } else { System.out.println("compression type: " + types[i] + " NOT Supported for " + getImageTypeName(img_type)); continue; } ImageWriteParam imageWriteParam = getImageWriteParam(writer, types[i]); imageWriteParam.setSourceSubsampling(srcXSubsampling, srcYSubsampling, 0, 0); File outputFile = new File("subsampling_test_" + getImageTypeName(img_type) + "__" + types[i] + ".bmp"); ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile); writer.setOutput(ios); IIOImage iioImg = new IIOImage(image, null, null); writer.write(null, iioImg, imageWriteParam); ios.flush(); ios.close(); BufferedImage outputImage = ImageIO.read(outputFile); checkTestImage(outputImage); } } }
Example 19
Source File: ReaderListenersTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
private static void doTest(String compression) { try { BufferedImage img = createTestImage(); ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByFormatName("bmp").next(); if (iw == null) { throw new RuntimeException("No writers for bmp format." + " Test failed."); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); iw.setOutput(ImageIO.createImageOutputStream(baos)); ImageWriteParam param = iw.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionType(compression); iw.write(null, new IIOImage(img, null, null), param); baos.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName("bmp").next(); if (ir == null) { throw new RuntimeException("No readers for bmp format." + " Test failed."); } IIOReadUpdateAdapter updateAdapter = new IIOReadUpdateAdapter(); IIOReadProgressAdapter progressAdapter = new IIOReadProgressAdapter(); ir.addIIOReadProgressListener(progressAdapter); ir.addIIOReadUpdateListener(updateAdapter); ir.setInput(ImageIO.createImageInputStream(bais)); BufferedImage dst = ir.read(0); progressAdapter.checkResults(); if (!updateAdapter.isImageUpdateUsed) { throw new RuntimeException("imageUpdate was not used." + " Test failed."); } } catch(IOException e) { e.printStackTrace(); throw new RuntimeException("Test failed"); } }
Example 20
Source File: BMPSubsamplingTest.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
public BMPSubsamplingTest() throws IOException { ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next(); ImageWriteParam wparam = writer.getDefaultWriteParam(); wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); String[] types = wparam.getCompressionTypes(); for (int t = 0; t < img_types.length; t++) { int img_type = img_types[t]; System.out.println("Test for " + getImageTypeName(img_type)); BufferedImage image = getTestImage(img_type); ImageTypeSpecifier specifier = new ImageTypeSpecifier(image); if (!writer.getOriginatingProvider().canEncodeImage(specifier)) { System.out.println("Writer does not support encoding this buffered image type."); continue; } for(int i=0; i<types.length; i++) { if ("BI_JPEG".equals(types[i])) { // exclude BI_JPEG from automatic test // due to color diffusion effect on the borders. continue; } if (canEncodeImage(types[i], specifier, img_type)) { System.out.println("compression type: " + types[i] + " Supported for " + getImageTypeName(img_type)); } else { System.out.println("compression type: " + types[i] + " NOT Supported for " + getImageTypeName(img_type)); continue; } ImageWriteParam imageWriteParam = getImageWriteParam(writer, types[i]); imageWriteParam.setSourceSubsampling(srcXSubsampling, srcYSubsampling, 0, 0); File outputFile = new File("subsampling_test_" + getImageTypeName(img_type) + "__" + types[i] + ".bmp"); ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile); writer.setOutput(ios); IIOImage iioImg = new IIOImage(image, null, null); writer.write(null, iioImg, imageWriteParam); ios.flush(); ios.close(); BufferedImage outputImage = ImageIO.read(outputFile); checkTestImage(outputImage); } } }