List of usage examples for javax.imageio.stream ImageOutputStream flush
void flush() throws IOException;
From source file:org.hippoecm.frontend.plugins.gallery.imageutil.ImageUtils.java
/** * Returns the data of a {@link BufferedImage} as a binary output stream. If the image is <code>null</code>, * a stream of zero bytes is returned./* w w w .j a v a2 s . c o m*/ * * @param writer the writer to use for writing the image data. * @param image the image to write. * @param compressionQuality a float between 0 and 1 that indicates the desired compression quality. Values lower than * 0 will be interpreted as 0, values higher than 1 will be interpreted as 1. * * @return an output stream with the data of the given image. * * @throws IOException when creating the binary output stream failed. */ public static ByteArrayOutputStream writeImage(ImageWriter writer, BufferedImage image, float compressionQuality) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); if (image != null) { ImageOutputStream ios = null; try { ios = ImageIO.createImageOutputStream(out); writer.setOutput(ios); // write compressed images with high quality final ImageWriteParam writeParam = writer.getDefaultWriteParam(); if (writeParam.canWriteCompressed()) { String[] compressionTypes = writeParam.getCompressionTypes(); if (compressionTypes != null && compressionTypes.length > 0) { writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writeParam.setCompressionType(compressionTypes[0]); // ensure a compression quality between 0 and 1 float trimmedCompressionQuality = Math.max(compressionQuality, 0); trimmedCompressionQuality = Math.min(trimmedCompressionQuality, 1f); writeParam.setCompressionQuality(trimmedCompressionQuality); } } final IIOImage iioImage = new IIOImage(image, null, null); writer.write(null, iioImage, writeParam); ios.flush(); } finally { if (ios != null) { ios.close(); } } } return out; }
From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java
/** * Can change this to choose a better compression level as the default * @param image//from ww w .jav a 2 s . com * @param scaledImage * @return */ public static boolean writeTo(BufferedImage image, File scaledImage, Size scaledSize, String outputFormat) { try { if (!StringHelper.containsNonWhitespace(outputFormat)) { outputFormat = OUTPUT_FORMAT; } Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat); if (writers.hasNext()) { ImageWriter writer = writers.next(); ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize); IIOImage iiOImage = new IIOImage(image, null, null); ImageOutputStream iOut = new FileImageOutputStream(scaledImage); writer.setOutput(iOut); writer.write(null, iiOImage, iwp); writer.dispose(); iOut.flush(); iOut.close(); return true; } else { return ImageIO.write(image, outputFormat, scaledImage); } } catch (IOException e) { return false; } }
From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java
/** * Can change this to choose a better compression level as the default * @param image//from w ww. ja v a2s. co m * @param scaledImage * @return */ private static boolean writeTo(BufferedImage image, OutputStream scaledImage, Size scaledSize, String outputFormat) { try { if (!StringHelper.containsNonWhitespace(outputFormat)) { outputFormat = OUTPUT_FORMAT; } Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat); if (writers.hasNext()) { ImageWriter writer = writers.next(); ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize); IIOImage iiOImage = new IIOImage(image, null, null); ImageOutputStream iOut = new MemoryCacheImageOutputStream(scaledImage); writer.setOutput(iOut); writer.write(null, iiOImage, iwp); writer.dispose(); iOut.flush(); iOut.close(); return true; } else { return ImageIO.write(image, outputFormat, scaledImage); } } catch (IOException e) { return false; } }
From source file:org.tinymediamanager.core.ImageCache.java
/** * Scale image to fit in the given width. * /*from w ww. ja va 2 s.c o m*/ * @param imageUrl * the image url * @param width * the width * @return the input stream * @throws IOException * Signals that an I/O exception has occurred. * @throws InterruptedException */ public static InputStream scaleImage(String imageUrl, int width) throws IOException, InterruptedException { Url url = new Url(imageUrl); BufferedImage originalImage = null; try { originalImage = createImage(url.getBytes()); } catch (Exception e) { throw new IOException(e.getMessage()); } Point size = new Point(); size.x = width; size.y = size.x * originalImage.getHeight() / originalImage.getWidth(); // BufferedImage scaledImage = Scaling.scale(originalImage, size.x, size.y); BufferedImage scaledImage = Scalr.resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, size.x, size.y, Scalr.OP_ANTIALIAS); originalImage = null; ImageWriter imgWrtr = null; ImageWriteParam imgWrtrPrm = null; // here we have two different ways to create our thumb // a) a scaled down jpg/png (without transparency) which we have to modify since OpenJDK cannot call native jpg encoders // b) a scaled down png (with transparency) which we can store without any more modifying as png if (hasTransparentPixels(scaledImage)) { // transparent image -> png imgWrtr = ImageIO.getImageWritersByFormatName("png").next(); imgWrtrPrm = imgWrtr.getDefaultWriteParam(); } else { // non transparent image -> jpg // convert to rgb BufferedImage rgb = new BufferedImage(scaledImage.getWidth(), scaledImage.getHeight(), BufferedImage.TYPE_INT_RGB); ColorConvertOp xformOp = new ColorConvertOp(null); xformOp.filter(scaledImage, rgb); imgWrtr = ImageIO.getImageWritersByFormatName("jpg").next(); imgWrtrPrm = imgWrtr.getDefaultWriteParam(); imgWrtrPrm.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT); imgWrtrPrm.setCompressionQuality(0.80f); scaledImage = rgb; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageOutputStream output = ImageIO.createImageOutputStream(baos); imgWrtr.setOutput(output); IIOImage outputImage = new IIOImage(scaledImage, null, null); imgWrtr.write(null, outputImage, imgWrtrPrm); imgWrtr.dispose(); scaledImage = null; byte[] bytes = baos.toByteArray(); output.flush(); output.close(); baos.close(); return new ByteArrayInputStream(bytes); }
From source file:org.tinymediamanager.core.ImageCache.java
/** * Scale image to fit in the given width. * //from w w w . jav a2 s .c o m * @param file * the original image file * @param width * the width * @return the input stream * @throws IOException * Signals that an I/O exception has occurred. * @throws InterruptedException */ public static InputStream scaleImage(Path file, int width) throws IOException, InterruptedException { BufferedImage originalImage = null; try { originalImage = createImage(file); } catch (Exception e) { throw new IOException(e.getMessage()); } Point size = new Point(); size.x = width; size.y = size.x * originalImage.getHeight() / originalImage.getWidth(); // BufferedImage scaledImage = Scaling.scale(originalImage, size.x, size.y); BufferedImage scaledImage = Scalr.resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, size.x, size.y, Scalr.OP_ANTIALIAS); originalImage = null; ImageWriter imgWrtr = null; ImageWriteParam imgWrtrPrm = null; // here we have two different ways to create our thumb // a) a scaled down jpg/png (without transparency) which we have to modify since OpenJDK cannot call native jpg encoders // b) a scaled down png (with transparency) which we can store without any more modifying as png if (hasTransparentPixels(scaledImage)) { // transparent image -> png imgWrtr = ImageIO.getImageWritersByFormatName("png").next(); imgWrtrPrm = imgWrtr.getDefaultWriteParam(); } else { // non transparent image -> jpg // convert to rgb BufferedImage rgb = new BufferedImage(scaledImage.getWidth(), scaledImage.getHeight(), BufferedImage.TYPE_INT_RGB); ColorConvertOp xformOp = new ColorConvertOp(null); xformOp.filter(scaledImage, rgb); imgWrtr = ImageIO.getImageWritersByFormatName("jpg").next(); imgWrtrPrm = imgWrtr.getDefaultWriteParam(); imgWrtrPrm.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT); imgWrtrPrm.setCompressionQuality(0.80f); scaledImage = rgb; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageOutputStream output = ImageIO.createImageOutputStream(baos); imgWrtr.setOutput(output); IIOImage outputImage = new IIOImage(scaledImage, null, null); imgWrtr.write(null, outputImage, imgWrtrPrm); imgWrtr.dispose(); scaledImage = null; byte[] bytes = baos.toByteArray(); output.flush(); output.close(); baos.close(); return new ByteArrayInputStream(bytes); }
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.// w w w . j av a 2 s . 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; }