List of usage examples for javax.imageio ImageWriter setOutput
public void setOutput(Object output)
From source file:doge.photo.DogePhotoManipulator.java
@Override public Photo manipulate(Photo photo) throws IOException { BufferedImage sourceImage = readImage(photo); BufferedImage destinationImage = manipulate(sourceImage); Photo resultPhoto = () -> {//from w ww.jav a 2 s . c o m ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream); ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(0.85f); writer.setOutput(ios); writer.write(null, new IIOImage(destinationImage, null, null), param); ImageIO.write(destinationImage, "jpeg", outputStream); return new ByteArrayInputStream(outputStream.toByteArray()); }; return resultPhoto; }
From source file:io.warp10.script.processing.Pencode.java
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object top = stack.pop();/*ww w. j a v a 2 s. c o m*/ 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:org.eclipse.birt.chart.device.svg.SVGImage.java
public SVGImage(Image image, URL url, byte[] data) { super();//from w ww. ja v a 2 s.c om this.image = image; this.url = url; this.data = data; if (url == null && data == null && image instanceof BufferedImage) { ImageWriter iw = ImageWriterFactory.instance().createImageWriter("png", "html"); //$NON-NLS-1$ //$NON-NLS-2$ try { ByteArrayOutputStream baos = new ByteArrayOutputStream(2048); ImageOutputStream ios = SecurityUtil.newImageOutputStream(baos); ImageWriteParam iwp = iw.getDefaultWriteParam(); iw.setOutput(ios); iw.write((IIOMetadata) null, new IIOImage((BufferedImage) image, null, null), iwp); ios.close(); this.data = baos.toByteArray(); baos.close(); } catch (IOException e) { // do nothing } finally { iw.dispose(); } } }
From source file:org.gallery.web.controller.ImageController.java
public static boolean compressImg(BufferedImage src, File outfile, double d) { FileOutputStream out = null;/*from w w w. j ava2 s .c o m*/ ImageWriter imgWrier; ImageWriteParam imgWriteParams; // ? jpg imgWrier = ImageIO.getImageWritersByFormatName("jpg").next(); imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(null); // ??MODE_EXPLICIT imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT); // ?qality?0~1 imgWriteParams.setCompressionQuality((float) d); imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED); ColorModel colorModel = ColorModel.getRGBdefault(); // ? imgWriteParams.setDestinationType( new javax.imageio.ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16))); try { out = new FileOutputStream(outfile); imgWrier.reset(); // out?write, ImageOutputStream? // OutputStream imgWrier.setOutput(ImageIO.createImageOutputStream(out)); // write??? imgWrier.write(null, new IIOImage(src, null, null), imgWriteParams); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
From source file:org.mycore.iview2.frontend.MCRTileCombineServlet.java
/** * Transmits combined file or sends thumbnail. * Uses {@link HttpServletRequest#getAttribute(String)} to retrieve information generated by {@link #think(MCRServletJob)}. * <table>// ww w. j a v a 2 s. c o m * <caption>description of {@link HttpServletRequest} attributes</caption> * <tr><th>keyName</th><th>type</th><th>description</th></tr> * <tr><td>{@link #THUMBNAIL_KEY}</td><td>{@link File}</td><td>.iview2 File with all tiles in it</td></tr> * <tr><td>{@link #IMAGE_KEY}</td><td>{@link BufferedImage}</td> * <td>generated image if <code>zoomLevel != 0</code> and no implementation of {@link MCRFooterInterface} defined</td></tr> * </table> */ @Override protected void render(final MCRServletJob job, final Exception ex) throws Exception { if (job.getResponse().isCommitted()) { return; } if (ex != null) { throw ex; } //check for thumnail final File iviewFile = ((Path) job.getRequest().getAttribute(THUMBNAIL_KEY)).toFile(); final BufferedImage combinedImage = (BufferedImage) job.getRequest().getAttribute(IMAGE_KEY); if (iviewFile != null && combinedImage == null) { sendThumbnail(iviewFile, job.getResponse()); return; } //send combined image job.getResponse().setHeader("Cache-Control", "max-age=" + MCRTileServlet.MAX_AGE); job.getResponse().setContentType("image/jpeg"); job.getResponse().setDateHeader("Last-Modified", iviewFile.lastModified()); final Date expires = new Date(System.currentTimeMillis() + MCRTileServlet.MAX_AGE * 1000); LOGGER.info("Last-Modified: " + new Date(iviewFile.lastModified()) + ", expire on: " + expires); job.getResponse().setDateHeader("Expires", expires.getTime()); final ImageWriter curImgWriter = imageWriter.get(); try (ServletOutputStream sout = job.getResponse().getOutputStream(); ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(sout);) { curImgWriter.setOutput(imageOutputStream); final IIOImage iioImage = new IIOImage(combinedImage, null, null); curImgWriter.write(null, iioImage, imageWriteParam); } finally { curImgWriter.reset(); } }
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> * /*from w w w.j a v a2 s. 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:org.apache.jetspeed.security.mfa.impl.CaptchaImageResource.java
/** * @param image/* ww w . j a v a2s . c om*/ * The image to turn into data * @return The image data for this dynamic image */ protected byte[] toImageData(final BufferedImage image) throws IOException { // Create output stream final ByteArrayOutputStream out = new ByteArrayOutputStream(); String format = config.getImageFormat().substring(1); // Get image writer for format // FIXME: config.getImageFormat() final ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName(format).next(); // Write out image writer.setOutput(ImageIO.createImageOutputStream(out)); writer.write(image); // Return the image data return out.toByteArray(); }
From source file:ScreenCapture.java
private void save() { if (ia.getImage() == null) { System.out.println("No captured image."); return;//from w w w .j a v a2s . c om } ImageWriter writer = null; ImageOutputStream ios = null; try { Iterator iter = ImageIO.getImageWritersByFormatName("jpeg"); if (!iter.hasNext()) { System.out.println("Unable to save image to jpeg file type."); return; } writer = (ImageWriter) iter.next(); ios = ImageIO.createImageOutputStream(new File("c:\\a.jpg")); writer.setOutput(ios); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(0.95f); writer.write(null, new IIOImage((BufferedImage) ia.getImage(), null, null), iwp); } catch (Exception e2) { e2.printStackTrace(); } }
From source file:de.inren.service.picture.PictureModificationServiceImpl.java
private void writeJpeg(BufferedImage image, File file, float quality) throws IOException { ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(quality);//from w ww. j a va2 s . co m FileImageOutputStream output = new FileImageOutputStream(file); writer.setOutput(output); writer.write(null, new IIOImage(image, null, null), iwp); }
From source file:org.freecine.filmscan.Project.java
private void saveImage(RenderedImage img, File file) { // Find a writer for that file extensions // Try to determine the file type based on extension String ftype = "jpg"; String imageFname = file.getName(); int extIndex = imageFname.lastIndexOf(".") + 1; if (extIndex > 0) { ftype = imageFname.substring(extIndex); }//from w w w . j av a 2 s .c o m ImageWriter writer = null; Iterator iter = ImageIO.getImageWritersBySuffix(ftype); writer = (ImageWriter) iter.next(); if (writer != null) { ImageOutputStream ios = null; try { // Prepare output file ios = ImageIO.createImageOutputStream(file); writer.setOutput(ios); // Set some parameters ImageWriteParam param = writer.getDefaultWriteParam(); writer.write(null, new IIOImage(img, null, null), param); // Cleanup ios.flush(); } catch (IOException ex) { log.severe("Error saving image " + file.getAbsolutePath() + ": " + ex.getMessage()); } finally { if (ios != null) { try { ios.close(); } catch (IOException e) { log.severe("Error closing output stream: " + e.getMessage()); } } writer.dispose(); } } }