List of usage examples for javax.imageio ImageWriter write
public abstract void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException;
From source file:org.forgerock.doc.maven.PNGUtils.java
private static void saveBufferedImage(final BufferedImage bufferedImage, final File outputFile, final int dotsPerInch) throws IOException { for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName("png"); iw.hasNext();) { ImageWriter writer = iw.next(); ImageWriteParam writeParam = writer.getDefaultWriteParam(); ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier .createFromBufferedImageType(BufferedImage.TYPE_INT_RGB); IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam); if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) { continue; }//from w ww .j a v a2 s .c o m setDPI(metadata, dotsPerInch); final ImageOutputStream stream = ImageIO.createImageOutputStream(outputFile); try { writer.setOutput(stream); writer.write(metadata, new IIOImage(bufferedImage, null, metadata), writeParam); } finally { stream.close(); } break; } }
From source file:editeurpanovisu.ReadWriteImage.java
/** * * @param img// w w w. j a v a 2 s . c om * @param destFile * @param sharpen * @param sharpenLevel * @throws IOException */ public static void writePng(Image img, String destFile, 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.BITMASK); 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("png").next(); ImageWriteParam param = writer.getDefaultWriteParam(); 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:com.db.comserv.main.utilities.HttpCaller.java
public static String compress(String imageString, float quality) throws IOException { byte[] imageByte = Base64.decodeBase64(imageString); ByteArrayInputStream bis = new ByteArrayInputStream(imageByte); BufferedImage image = ImageIO.read(bis); bis.close();//from w w w .j a v a 2 s . c om // Get a ImageWriter for given extension format. Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("jpg"); if (!writers.hasNext()) throw new IllegalStateException("No writers found"); ImageWriter writer = (ImageWriter) writers.next(); // Create the ImageWriteParam to compress the image. ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(quality); // The output will be a ByteArrayOutputStream (in memory) ByteArrayOutputStream bos = new ByteArrayOutputStream(32768); ImageOutputStream ios = ImageIO.createImageOutputStream(bos); writer.setOutput(ios); writer.write(null, new IIOImage(image, null, null), param); ios.flush(); // otherwise the buffer size will be zero! byte[] encodedBytes = Base64.encodeBase64(bos.toByteArray()); return new String(encodedBytes); }
From source file:editeurpanovisu.ReadWriteImage.java
/** * * @param img//from w ww. j av a2 s . co m * @param destFile * @param sharpen * @param sharpenLevel * @throws IOException */ public static void writeBMP(Image img, String destFile, 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("bmp").next(); ImageWriteParam param = writer.getDefaultWriteParam(); 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:eu.europa.esig.dss.pades.signature.visible.ImageFactory.java
private static InputStream convertToInputStream(BufferedImage buffImage, int dpi) throws IOException { Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("jpeg"); if (!it.hasNext()) { throw new DSSException("No writer for JPEG found"); }/*from ww w . j av a 2 s . c om*/ ImageWriter writer = it.next(); JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) writer.getDefaultWriteParam(); jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT); jpegParams.setCompressionQuality(1); ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier .createFromBufferedImageType(BufferedImage.TYPE_INT_RGB); IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, jpegParams); initDpi(metadata, dpi); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageOutputStream imageOs = ImageIO.createImageOutputStream(os); writer.setOutput(imageOs); writer.write(metadata, new IIOImage(buffImage, null, metadata), jpegParams); InputStream is = new ByteArrayInputStream(os.toByteArray()); return is; }
From source file:editeurpanovisu.ReadWriteImage.java
/** * * @param img/*from w ww . ja va 2 s . c om*/ * @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: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. j a v a 2s . c om*/ * @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 ww w . j a va2 s . 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:common.utils.ImageUtils.java
public static void writeImage(BufferedImage image, float quality, OutputStream out) throws IOException { ImageWriter writer = getImageWriter(); if (writer == null) return;// w ww .j a v a 2s .c o m ImageOutputStream ios = ImageIO.createImageOutputStream(out); writer.setOutput(ios); ImageWriteParam param = writer.getDefaultWriteParam(); //JPEGImageWriteParam param = new JPEGImageWriteParam(Locale.getDefault()); //param.setOptimizeHuffmanTables(true); if (quality >= 0) { param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(quality); //param.setCompressionMode(ImageWriteParam.MODE_COPY_FROM_METADATA); //param.setProgressiveMode(ImageWriteParam.MODE_DISABLED); //param.setTilingMode(ImageWriteParam.MODE_DISABLED); } writer.write(null, new IIOImage(image, null, null), param); }
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 .ja va 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; }