List of usage examples for javax.imageio ImageWriter dispose
public void dispose()
From source file:editeurpanovisu.ReadWriteImage.java
/** * * @param img// w w w. jav a 2 s. c o m * @param destFile * @param quality * @param sharpen * @param sharpenLevel * @throws IOException */ public static void writeJpeg(Image img, String destFile, float quality, boolean sharpen, float sharpenLevel) throws IOException { sharpenMatrix = calculeSharpenMatrix(sharpenLevel); BufferedImage imageRGBSharpen = null; IIOImage iioImage = null; BufferedImage image = SwingFXUtils.fromFXImage(img, null); // Get buffered image. BufferedImage imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.OPAQUE); // Remove alpha-channel from buffered image. Graphics2D graphics = imageRGB.createGraphics(); graphics.drawImage(image, 0, 0, null); if (sharpen) { imageRGBSharpen = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); Kernel kernel = new Kernel(3, 3, sharpenMatrix); ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); cop.filter(imageRGB, imageRGBSharpen); } ImageWriter writer = null; FileImageOutputStream output = null; try { writer = ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(quality); output = new FileImageOutputStream(new File(destFile)); writer.setOutput(output); if (sharpen) { iioImage = new IIOImage(imageRGBSharpen, null, null); } else { iioImage = new IIOImage(imageRGB, null, null); } writer.write(null, iioImage, param); } catch (IOException ex) { throw ex; } finally { if (writer != null) { writer.dispose(); } if (output != null) { output.close(); } } graphics.dispose(); }
From source file:de.jwic.ecolib.controls.chart.ChartControl.java
public void renderImage() throws IOException { // create image to draw into BufferedImage bi = new BufferedImage(width < 10 ? 10 : width, height < 10 ? 10 : height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = bi.createGraphics(); if (chart != null) { chart.setBackgroundPaint(Color.WHITE); chart.draw(g2d, new Rectangle2D.Double(0, 0, width < 10 ? 10 : width, height < 10 ? 10 : height)); } else {//from ww w .j av a2s .co m g2d.setColor(Color.BLACK); g2d.drawString("No chart has been assigned.", 1, 20); } // finish drawing g2d.dispose(); // write image data into output stream ByteArrayOutputStream out = getImageOutputStream(); out.reset(); // create a PNG image ImageWriter imageWriter = new PNGImageWriter(null); ImageWriteParam param = imageWriter.getDefaultWriteParam(); imageWriter.setOutput(new MemoryCacheImageOutputStream(out)); imageWriter.write(null, new IIOImage(bi, null, null), param); imageWriter.dispose(); setMimeType(MIME_TYPE_PNG); }
From source file:org.shredzone.cilla.service.resource.ImageProcessorImpl.java
/** * Writes a JPEG file with adjustable compression quality. * * @param image/*from w w w. j a v a 2s . c o m*/ * {@link BufferedImage} to write * @param out * {@link ImageOutputStream} to write to * @param quality * Compression quality between 0.0f (worst) and 1.0f (best) */ private void jpegQualityWriter(BufferedImage image, ImageOutputStream out, float quality) throws IOException { ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(quality); IIOImage ioImage = new IIOImage(image, null, null); writer.setOutput(out); writer.write(null, ioImage, iwp); writer.dispose(); }
From source file:gr.iti.mklab.reveal.forensics.util.Util.java
public static BufferedImage recompressImage(BufferedImage imageIn, int quality) { // Apply in-memory JPEG compression to a BufferedImage given a quality setting (0-100) // and return the resulting BufferedImage float fQuality = (float) (quality / 100.0); BufferedImage outputImage = null; try {/*from ww w . j ava 2 s . c om*/ ImageWriter writer; Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg"); writer = iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(fQuality); byte[] imageInByte; try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos); writer.setOutput(mcios); IIOImage tmpImage = new IIOImage(imageIn, null, null); writer.write(null, tmpImage, iwp); writer.dispose(); baos.flush(); imageInByte = baos.toByteArray(); } InputStream in = new ByteArrayInputStream(imageInByte); outputImage = ImageIO.read(in); } catch (Exception ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } return outputImage; }
From source file:compressor.Compressor.java
void compress_images(String src, String dest) throws IOException { File f = null;/*from ww w . j a v a 2s .c o m*/ String[] paths; try { // create new file f = new File(src); // array of files and directory paths = f.list(); File file = new File(dest + "compressed"); if (!file.exists()) { if (file.mkdir()) { System.out.println("Directory is created!"); } else { System.out.println("Failed to create directory!"); } } dest = dest + "compressed/"; // for each name in the path array for (String path : paths) { // prints filename and directory name File input = new File(src + path); BufferedImage image = ImageIO.read(input); File compressedImageFile = new File(dest + path); OutputStream os = new FileOutputStream(compressedImageFile); Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); ImageWriter writer = (ImageWriter) writers.next(); ImageOutputStream ios = ImageIO.createImageOutputStream(os); writer.setOutput(ios); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(0.05f); writer.write(null, new IIOImage(image, null, null), param); os.close(); ios.close(); writer.dispose(); } } catch (Exception e) { } }
From source file:io.warp10.script.processing.Pencode.java
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object top = stack.pop();//from w w w. j ava 2s . com if (!(top instanceof processing.core.PGraphics)) { throw new WarpScriptException(getName() + " operates on a PGraphics instance."); } processing.core.PGraphics pg = (processing.core.PGraphics) top; pg.endDraw(); BufferedImage bimage = new BufferedImage(pg.pixelWidth, pg.pixelHeight, BufferedImage.TYPE_INT_ARGB); bimage.setRGB(0, 0, pg.pixelWidth, pg.pixelHeight, pg.pixels, 0, pg.pixelWidth); Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("png"); ImageWriter writer = null; if (iter.hasNext()) { writer = iter.next(); } ImageWriteParam param = writer.getDefaultWriteParam(); IIOMetadata metadata = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedOutputStream output = new BufferedOutputStream(baos); try { writer.setOutput(ImageIO.createImageOutputStream(output)); writer.write(metadata, new IIOImage(bimage, null, metadata), param); } catch (IOException ioe) { throw new WarpScriptException(getName() + " error while encoding PGraphics.", ioe); } writer.dispose(); StringBuilder sb = new StringBuilder("data:image/png;base64,"); sb.append(Base64.encodeBase64String(baos.toByteArray())); stack.push(sb.toString()); // // Re-issue a 'beginDraw' so we can continue using the PGraphics instance // pg.beginDraw(); return stack; }
From source file:com.alkacon.opencms.v8.weboptimization.CmsOptimizationSprite.java
/** * Writes the given image as of the given type to the servlet output stream.<p> * //w w w.jav a 2s . c o m * @param image the image to write * @param type the type * * @throws IOException if something goes wrong */ protected void writeImage(BufferedImage image, String type) throws IOException { ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName(type).next(); ImageOutputStream stream = ImageIO.createImageOutputStream(getJspContext().getResponse().getOutputStream()); writer.setOutput(stream); writer.write(image); // We must close the stream now because if we are wrapping a ServletOutputStream, // a future gc can commit a stream that used in another thread (very very bad) stream.flush(); stream.close(); writer.dispose(); }
From source file:com.sire.web.CajFacturaEnviadaBean.java
private void savePicture() { if (file != null) { try {//from w w w . j av a2s . c o m BufferedImage originalImage = ImageIO.read(file.getInputstream()); BufferedImage outputImage = new BufferedImage((int) (originalImage.getWidth() * 0.25), (int) (originalImage.getHeight() * 0.25), originalImage.getType()); Graphics2D g2d = outputImage.createGraphics(); g2d.drawImage(originalImage, 0, 0, (int) (originalImage.getWidth() * 0.25), (int) (originalImage.getHeight() * 0.25), null); g2d.dispose(); String imagesFolder = System.getProperty("imagesFolder"); if (imagesFolder == null) { String currentUsersHomeDir = System.getProperty("user.home"); imagesFolder = currentUsersHomeDir + File.separator + "photos"; } Iterator iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = (ImageWriter) iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); float quality = 1.0f; // reduce quality by 0% iwp.setCompressionQuality(quality); File f = new File(imagesFolder + File.separator + fileName); try (FileImageOutputStream output = new FileImageOutputStream(f)) { writer.setOutput(output); IIOImage image = new IIOImage(outputImage, null, null); writer.write(null, image, iwp); writer.dispose(); } } catch (IOException ex) { LOGGER.severe(ex.getMessage()); } } }
From source file:ar.com.zauber.common.image.impl.AbstractImage.java
/** * Creates a thumbnail/* w w w . jav a 2s. com*/ * * @param is data source * @param os data source * @throws IOException if there is a problem reading is */ public static void createThumbnail(final InputStream is, final OutputStream os, final int target) throws IOException { final float compression = 0.85F; ImageWriter writer = null; MemoryCacheImageOutputStream mos = null; Graphics2D graphics2D = null; try { final BufferedImage bi = ImageIO.read(is); final Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("JPG"); if (!iter.hasNext()) { throw new IllegalStateException("can't find JPG subsystem"); } int w = bi.getWidth(), h = bi.getHeight(); if (w < target && h < target) { // nothing to recalculate, ya es chiquita. } else { if (w > h) { h = target * bi.getHeight() / bi.getWidth(); w = target; } else { w = target * bi.getWidth() / bi.getHeight(); h = target; } } // draw original image to thumbnail image object and // scale it to the new size on-the-fly final BufferedImage thumbImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(bi, 0, 0, w, h, null); writer = (ImageWriter) iter.next(); final ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(compression); mos = new MemoryCacheImageOutputStream(os); writer.setOutput(mos); writer.write(null, new IIOImage(thumbImage, null, null), iwp); } finally { if (writer != null) { writer.dispose(); } if (mos != null) { mos.close(); } if (graphics2D != null) { graphics2D.dispose(); } is.close(); os.close(); } }
From source file:com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpRenderListener.java
private byte[] getJPGBytes(BufferedImage image) { ByteArrayOutputStream outputStream = null; try {/*from w ww.j a v a 2s . com*/ ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next(); ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam(); jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); jpgWriteParam.setCompressionQuality(1.0f); outputStream = new ByteArrayOutputStream(); jpgWriter.setOutput(new MemoryCacheImageOutputStream((outputStream))); IIOImage outputImage = new IIOImage(image, null, null); jpgWriter.write(null, outputImage, jpgWriteParam); jpgWriter.dispose(); outputStream.flush(); return outputStream.toByteArray(); } catch (Exception e) { throw new RuntimeException(e); } finally { closeOutputStream(outputStream); } }