List of usage examples for javax.imageio.plugins.jpeg JPEGImageWriteParam JPEGImageWriteParam
public JPEGImageWriteParam(Locale locale)
From source file:org.modelibra.util.ImageHandler.java
/** * Saves an image to a jpeg file./*from w ww . j a v a 2 s .c o m*/ * * @param bi * buffered image * @param file * jpeg file * @param quality * image quality */ public static void saveImageToJPEGFile14(BufferedImage bi, File file, float quality) throws IOException { JPEGImageWriteParam param = new JPEGImageWriteParam(null); ImageTypeSpecifier type = new ImageTypeSpecifier(bi); param.setDestinationType(type); ImageOutputStream imgos = ImageIO.createImageOutputStream(file); Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = writers.next(); writer.setOutput(imgos); writer.write(bi); imgos.close(); }
From source file:org.mycore.iview2.frontend.MCRTileCombineServlet.java
/** * Initializes this instance.//from www . j a va 2 s .com * * Use parameter <code>org.mycore.iview2.frontend.MCRFooterInterface</code> to specify implementation of {@link MCRFooterInterface} (can be omitted). */ @Override public void init() throws ServletException { super.init(); imageWriteParam = new JPEGImageWriteParam(Locale.getDefault()); try { imageWriteParam.setProgressiveMode(ImageWriteParam.MODE_DEFAULT); } catch (UnsupportedOperationException e) { LOGGER.warn("Your JPEG encoder does not support progressive JPEGs."); } imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality(QUALITY); String footerClassName = getInitParameter(MCRFooterInterface.class.getName()); if (footerClassName != null) { try { footerImpl = (MCRFooterInterface) Class.forName(footerClassName).newInstance(); } catch (Exception e) { throw new ServletException("Could not initialize MCRFooterInterface", e); } } }
From source file:tvbrowserdataservice.file.ProgramField.java
/** * This Function loads an image using imageio, * resizes it to the Max-Size of Images in TVBrowser and * stores it as an compressed jpg./*from ww w .j av a 2s .c o m*/ * <p/> * If the Image is smaller than the Max-Size, it isn't altered * * @param data Image-Data * @return resized Image-Data */ private static byte[] recreateImage(byte[] data) { byte[] newdata = null; BufferedImage image = null; try { // Read Image image = ImageIO.read(new ByteArrayInputStream(data)); int curx = image.getWidth(null); int cury = image.getHeight(null); // If the Size is < than max, use the original Data to reduce compression // artefacts if ((curx <= MAX_IMAGE_SIZE_X) && (cury <= MAX_IMAGE_SIZE_Y)) { return data; } int newx = MAX_IMAGE_SIZE_X; int newy = (int) ((MAX_IMAGE_SIZE_X / (float) curx) * cury); if (newy > MAX_IMAGE_SIZE_Y) { newy = MAX_IMAGE_SIZE_Y; newx = (int) ((MAX_IMAGE_SIZE_Y / (float) cury) * curx); } BufferedImage tempPic = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2d = tempPic.createGraphics(); g2d.drawImage(image, null, null); g2d.dispose(); BufferedImage newImage = UiUtilities.scaleIconToBufferedImage(tempPic, newx, newy); ByteArrayOutputStream out = new ByteArrayOutputStream(); // Find a jpeg writer ImageWriter writer = null; Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg"); if (iter.hasNext()) { writer = iter.next(); } if (writer != null) { // Prepare output file ImageOutputStream ios = ImageIO.createImageOutputStream(out); writer.setOutput(ios); JPEGImageWriteParam param = new JPEGImageWriteParam(null); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(0.85f); // Write the image writer.write(null, new IIOImage(newImage, null, null), param); // Cleanup ios.flush(); writer.dispose(); ios.close(); newdata = out.toByteArray(); } else { mLog.severe("No JPEG-Exporter found. Image is not stored in Data"); } } catch (IOException e) { e.printStackTrace(); newdata = null; } return newdata; }