List of usage examples for javax.imageio ImageWriter reset
public void reset()
From source file:org.gallery.web.controller.ImageController.java
public static boolean compressImg(BufferedImage src, File outfile, double d) { FileOutputStream out = null;// w ww . j a va 2 s . com 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:net.rptools.tokentool.AppActions.java
public static void saveToken(File rptok_file, boolean overwrite) { if (rptok_file != null) { if (!rptok_file.getName().toUpperCase().endsWith(".RPTOK")) { rptok_file = new File(rptok_file.getAbsolutePath() + ".rptok"); }//ww w .j a v a2 s. c om if (rptok_file.exists() && !overwrite) { if (!TokenTool.confirm("File exists. Overwrite?")) { return; } else { rptok_file.delete(); } } String tokenName = FilenameUtils.removeExtension(rptok_file.getName()); try { // Write out the token image first or aka POG image. File tokenImageFile = File.createTempFile("tokenImage", ".png"); // PW: This code addes the pHYs chunk to the // output png file with X & Y dpi set. BufferedImage tokenImg = TokenTool.getFrame().getComposedToken(); BufferedImage portraitImg = TokenTool.getFrame().getTokenCompositionPanel().getBaseImage(); ImageWriter writer = getImageWriterBySuffix("png"); // Created object for outputStream so we can properly close it! No longer locks .png files until app closes! ImageOutputStream ios = ImageIO.createImageOutputStream(tokenImageFile); writer.setOutput(ios); ImageWriteParam param = writer.getDefaultWriteParam(); PNGMetadata png = new PNGMetadata(); // 39.375 inches per meter // I'm using the image width for the DPI under // the assumption that the token fits within // one cell. int resX = (int) (tokenImg.getWidth() * 39.375f); png.pHYs_pixelsPerUnitXAxis = resX; png.pHYs_pixelsPerUnitYAxis = resX; png.pHYs_unitSpecifier = 1; // Meters - alternative is "unknown" png.pHYs_present = true; writer.write(null, new IIOImage(tokenImg, null, png), param); ios.close(); // Now write out the Portrait image, here we'll use JPEG to save space File portraitImageFile = File.createTempFile("portraitImage", ".jpg"); writer.reset(); writer = getImageWriterBySuffix("jpg"); ios = ImageIO.createImageOutputStream(portraitImageFile); writer.setOutput(ios); param = writer.getDefaultWriteParam(); writer.write(null, new IIOImage(portraitImg, null, null), param); writer.dispose(); ios.close(); // Lets create the token! Token _token = new Token(); Asset tokenImage = null; tokenImage = AssetManager.createAsset(tokenImageFile); AssetManager.putAsset(tokenImage); _token = new Token(tokenName, tokenImage.getId()); _token.setGMName(tokenName); // Jamz: Below calls not needed, creates extra entries in XML preventing token image from changing inside MapTool //_token.setImageAsset(tokenImage.getName()); //_token.setImageAsset(tokenImage.getName(), tokenImage.getId()); // set the image shape Image image = ImageIO.read(tokenImageFile); _token.setShape(TokenUtil.guessTokenType(image)); // set the height/width, fixes dragging to stamp layer issue _token.setHeight(tokenImg.getHeight()); _token.setWidth(tokenImg.getWidth()); // set the portrait image asset Asset portrait = AssetManager.createAsset(portraitImageFile); // Change for portrait AssetManager.putAsset(portrait); _token.setPortraitImage(portrait.getId()); // Time to write out the .rptok token file... PackedFile pakFile = null; try { pakFile = new PackedFile(rptok_file); saveAssets(_token.getAllImageAssets(), pakFile); pakFile.setContent(_token); BufferedImage thumb = ImageUtil.createCompatibleImage(image, THUMB_SIZE, THUMB_SIZE, null); pakFile.putFile(FILE_THUMBNAIL, ImageUtil.imageToBytes(thumb, "png")); pakFile.setProperty(PROP_VERSION, TokenToolFrame.VERSION); pakFile.save(); } catch (Exception e) { e.printStackTrace(); } finally { if (pakFile != null) pakFile.close(); tokenImageFile.delete(); portraitImageFile.delete(); } } catch (IOException ioe) { ioe.printStackTrace(); TokenTool.showError("Unable to write image: " + ioe); } } }
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>//from www .jav a 2s .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(); } }