List of usage examples for javax.imageio ImageWriteParam setCompressionMode
public void setCompressionMode(int mode)
From source file:edu.ku.brc.specify.utilapps.ERDVisualizer.java
public void writeJPEG(File outfile, BufferedImage bufferedImage, float compressionQuality) { try {//from w ww .j a v a 2 s.com long start = System.currentTimeMillis(); // Find a jpeg writer ImageWriter writer = null; Iterator<?> iter = ImageIO.getImageWritersByFormatName("jpg"); if (iter.hasNext()) { writer = (ImageWriter) iter.next(); } // Prepare output file ImageOutputStream ios = ImageIO.createImageOutputStream(outfile); writer.setOutput(ios); // Set the compression quality ImageWriteParam iwparam = new MyImageWriteParam(); iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwparam.setCompressionQuality(compressionQuality); // Write the image writer.write(null, new IIOImage(bufferedImage, null, null), null); // Cleanup ios.flush(); writer.dispose(); ios.close(); System.out.println(System.currentTimeMillis() - start); } catch (IOException e) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ERDVisualizer.class, e); e.printStackTrace(); } }
From source file:de.unigoettingen.sub.commons.contentlib.imagelib.JpegInterpreter.java
@Override public byte[] writeToStreamAndByteArray(OutputStream outStream) { byte[] data = null; if (this.renderedimage == null) { // no image available return data; }/*ww w . j a v a 2 s . com*/ try { // create a buffered Image, which has no Alpha channel // as JPEG does not support Alpha Channels and the // ImageIO doesn't care - but will create a corrupt JPEG BufferedImage noAlphaBi = ImageManipulator.fromRenderedToBufferedNoAlpha(renderedimage); ImageOutputStream imageOutStream = ImageIO.createImageOutputStream(outStream); // Iterator<ImageWriter> writerIter = ImageIO // .getImageWritersByFormatName("jpg"); // ImageWriter writer = writerIter.next(); // get writer from ImageIO ImageWriter writer = new JPEGImageWriter(new JPEGImageWriterSpi()); // create metadata by creating an XML tree ImageWriteParam writerParam = writer.getDefaultWriteParam(); ImageTypeSpecifier its = new ImageTypeSpecifier(noAlphaBi); // ImageTypeSpecifier its = new // ImageTypeSpecifier(image.getColorModel(), // image.getSampleModel()); // IIOMetadata iomd = writer.getDefaultImageMetadata(new // ImageTypeSpecifier(image), writerParam); // Element tree = // (Element)iomd.getAsTree("javax_imageio_jpeg_image_1.0"); // Element tree = (Element)iomd.getAsTree("javax_imageio_1.0"); // IIOMetadata iomd = writer.getDefaultImageMetadata(its, writerParam); // create the XML tree and modify the appropriate DOM elements // to set the metadata setMetadata(iomd); // set compression writerParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); float comprvalue = ((float) writerCompressionValue) / 100; writerParam.setCompressionQuality(comprvalue); // set output writer.setOutput(imageOutStream); writer.prepareWriteSequence(null); // create new image parameters to set the compression // Locale locale = new Locale("en"); // JPEGImageWriteParam jpegWriteParam = new // JPEGImageWriteParam(locale); // IIOImage iioImage = new IIOImage(renderedimage, null, iomd); IIOImage iioImage = new IIOImage(noAlphaBi, null, iomd); writer.write(null, iioImage, writerParam); writer.endWriteSequence(); imageOutStream.flush(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageOutputStream imageToFile = ImageIO.createImageOutputStream(baos); writer.setOutput(imageToFile); writer.prepareWriteSequence(null); writer.write(null, iioImage, writerParam); writer.endWriteSequence(); imageToFile.flush(); imageToFile.close(); baos.flush(); data = baos.toByteArray(); baos.close(); writer.dispose(); imageOutStream.close(); } catch (IOException e) { LOGGER.error("IOException occured", e); } return data; }
From source file:de.unigoettingen.sub.commons.contentlib.imagelib.JpegInterpreter.java
/************************************************************************************ * Write the renderedimage to an {@link OutputStream} * /*from ww w . j av a2s. c o m*/ * @param outStream the {@link OutputStream} to write to ************************************************************************************/ @Override public void writeToStream(FileOutputStream fos, OutputStream outStream) { if (this.renderedimage == null) { // no image available return; } try { // create a buffered Image, which has no Alpha channel // as JPEG does not support Alpha Channels and the // ImageIO doesn't care - but will create a corrupt JPEG BufferedImage noAlphaBi = ImageManipulator.fromRenderedToBufferedNoAlpha(renderedimage); ImageOutputStream imageOutStream = ImageIO.createImageOutputStream(outStream); // Iterator<ImageWriter> writerIter = ImageIO // .getImageWritersByFormatName("jpg"); // ImageWriter writer = writerIter.next(); // get writer from ImageIO ImageWriter writer = new JPEGImageWriter(new JPEGImageWriterSpi()); // create metadata by creating an XML tree ImageWriteParam writerParam = writer.getDefaultWriteParam(); ImageTypeSpecifier its = new ImageTypeSpecifier(noAlphaBi); // ImageTypeSpecifier its = new // ImageTypeSpecifier(image.getColorModel(), // image.getSampleModel()); // IIOMetadata iomd = writer.getDefaultImageMetadata(new // ImageTypeSpecifier(image), writerParam); // Element tree = // (Element)iomd.getAsTree("javax_imageio_jpeg_image_1.0"); // Element tree = (Element)iomd.getAsTree("javax_imageio_1.0"); // IIOMetadata iomd = writer.getDefaultImageMetadata(its, writerParam); // create the XML tree and modify the appropriate DOM elements // to set the metadata setMetadata(iomd); // set compression writerParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); float comprvalue = ((float) writerCompressionValue) / 100; writerParam.setCompressionQuality(comprvalue); // set output writer.setOutput(imageOutStream); writer.prepareWriteSequence(null); // create new image parameters to set the compression // Locale locale = new Locale("en"); // JPEGImageWriteParam jpegWriteParam = new // JPEGImageWriteParam(locale); // IIOImage iioImage = new IIOImage(renderedimage, null, iomd); IIOImage iioImage = new IIOImage(noAlphaBi, null, iomd); writer.write(null, iioImage, writerParam); writer.endWriteSequence(); imageOutStream.flush(); if (fos != null) { ImageOutputStream imageToFile = ImageIO.createImageOutputStream(fos); writer.setOutput(imageToFile); writer.prepareWriteSequence(null); writer.write(null, iioImage, writerParam); writer.endWriteSequence(); imageToFile.flush(); imageToFile.close(); fos.flush(); fos.close(); } // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // byte [] data = wi.getImageAsByteArray(); writer.dispose(); imageOutStream.close(); } catch (IOException e) { LOGGER.error("IOException occured", e); } }
From source file:lucee.runtime.img.Image.java
private void setCompressionModeEL(ImageWriteParam iwp, int mode) { try {/* w w w .j a v a 2 s. c o m*/ iwp.setCompressionMode(mode); } catch (Throwable t) { } }
From source file:org.apache.flex.compiler.internal.embedding.transcoders.JPEGTranscoder.java
private byte[] bufferedImageToJPEG(ImageInfo imageInfo, int[] pixels) throws Exception { BufferedImage bufferedImage = new BufferedImage(imageInfo.width, imageInfo.height, BufferedImage.TYPE_INT_ARGB); bufferedImage.setRGB(0, 0, imageInfo.width, imageInfo.height, pixels, 0, imageInfo.width); ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam writeParam = writer.getDefaultWriteParam(); ColorModel colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff); ImageTypeSpecifier imageTypeSpecifier = new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(1, 1)); writeParam.setDestinationType(imageTypeSpecifier); writeParam.setSourceBands(new int[] { 0, 1, 2 }); writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); float q = 1.0f; if (quality != null) q = quality.floatValue();/*from ww w . ja v a2s .co m*/ writeParam.setCompressionQuality(q); DAByteArrayOutputStream buffer = new DAByteArrayOutputStream(); writer.setOutput(new MemoryCacheImageOutputStream(buffer)); IIOImage ioImage = new IIOImage(bufferedImage, null, null); writer.write(null, ioImage, writeParam); writer.dispose(); return buffer.getDirectByteArray(); }
From source file:org.bigbluebuttonproject.fileupload.document.impl.FileSystemSlideManager.java
/** * This method create thumbImage of the image file given and save it in outFile. * Compression quality is also given. thumbBounds is used for calculating the size of the thumb. * /* www . ja v a 2s . c om*/ * @param infile slide image to create thumb * @param outfile output thumb file * @param compressionQuality the compression quality * @param thumbBounds the thumb bounds * * @throws IOException Signals that an I/O exception has occurred. */ public void resizeImage(File infile, File outfile, float compressionQuality, int thumbBounds) throws IOException { // Retrieve jpg image to be resized Image image = ImageIO.read(infile); // get original image size for thumb size calculation int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); float thumbRatio = (float) thumbBounds / Math.max(imageWidth, imageHeight); int thumbWidth = (int) (imageWidth * thumbRatio); int thumbHeight = (int) (imageHeight * thumbRatio); // draw original image to thumbnail image object and // scale it to the new size on-the-fly BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); // Find a jpeg writer ImageWriter writer = null; Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg"); if (iter.hasNext()) { writer = (ImageWriter) iter.next(); } ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(compressionQuality); // Prepare output file ImageOutputStream ios = ImageIO.createImageOutputStream(outfile); writer.setOutput(ios); // write to the thumb image writer.write(thumbImage); // Cleanup ios.flush(); writer.dispose(); ios.close(); }
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 .ja va2 s. c om 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.deegree.securityproxy.wms.responsefilter.clipping.SimpleRasterClipper.java
private ImageWriteParam configureWriterParameters(String format, ImageWriter imageWriter) { ImageWriteParam writerParam = imageWriter.getDefaultWriteParam(); if (JPG_FORMAT.equals(format)) { writerParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writerParam.setCompressionQuality(1f); }//from w w w .ja v a 2s . co m return writerParam; }
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);//w w w . j a va 2 s .c o 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.egov.works.abstractestimate.service.EstimatePhotographService.java
public File compressImage(final MultipartFile[] files) throws IOException, FileNotFoundException { final BufferedImage image = ImageIO.read(files[0].getInputStream()); final File compressedImageFile = new File(files[0].getOriginalFilename()); final OutputStream os = new FileOutputStream(compressedImageFile); String fileExtenstion = files[0].getOriginalFilename(); fileExtenstion = fileExtenstion.substring(fileExtenstion.lastIndexOf(".") + 1); final Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(fileExtenstion); final ImageWriter writer = writers.next(); final ImageOutputStream ios = ImageIO.createImageOutputStream(os); writer.setOutput(ios);/* w w w. ja va 2 s . com*/ final ImageWriteParam param = writer.getDefaultWriteParam(); if (!fileExtenstion.equalsIgnoreCase("png")) { param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(0.5f); } writer.write(null, new IIOImage(image, null, null), param); os.close(); ios.close(); writer.dispose(); return compressedImageFile; }