private ImageOutputStream createImageOutputStream(OutputStream os) throws IOException { if (this.cacheDir != null) { return new FileCacheImageOutputStream(os, this.cacheDir); } else { return new MemoryCacheImageOutputStream(os); } }
private ImageOutputStream createImageOutputStream(OutputStream os) throws IOException { if (this.cacheDir != null) { return new FileCacheImageOutputStream(os, this.cacheDir); } else { return new MemoryCacheImageOutputStream(os); } }
/** * create a gif animation producer * @param loops - number of loops for the animated images. -1 = no loops; 0 = indefinitely loops; else: number of loops */ public AnimationGIF(int loops) { this.counter = 0; this.loops = loops; this.ios = null; this.writer = null; this.baos = new ByteArrayOutputStream(); Iterator<ImageWriter> writerIterator = ImageIO.getImageWritersByFormatName("GIF"); this.writer = writerIterator.next(); // com.sun.media.imageioimpl.plugins.gif.GIFImageWriter, com.sun.imageio.plugins.gif.GIFImageWriter this.ios = new MemoryCacheImageOutputStream(baos); this.writer.setOutput(ios); this.iwp = writer.getDefaultWriteParam(); }
/** * Compress {@link BufferedImage} with a given quality into byte array. * * @param bi the {@link BufferedImage} to compres into byte array * @param quality the compressed image quality (1 = 100%, 0.5 = 50%, 0.1 = 10%, etc) * @return The size of compressed data (number of bytes) */ private int compress(BufferedImage bi, float quality) { baos.reset(); final JPEGImageWriteParam params = new JPEGImageWriteParam(null); params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); params.setCompressionQuality(quality); try (MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos)) { final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next(); writer.setOutput(mcios); writer.write(null, new IIOImage(bi, null, null), params); } catch (IOException e) { throw new IllegalStateException(e); } return baos.size(); }
protected InputStream compress(BufferedImage image, String formatName) throws Exception { if (formatName.toLowerCase().equals("jpeg") || formatName.toLowerCase().equals("jpg")) { image = stripAlpha(image); } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(byteArrayOutputStream); if (formatName.toLowerCase().equals("gif")) { formatName = "png"; } Iterator<ImageWriter> writerIter = ImageIO.getImageWritersByFormatName(formatName); ImageWriter writer = writerIter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); if (formatName.toLowerCase().equals("jpeg") || formatName.toLowerCase().equals("jpg")) { iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(0.85F); iwp.setProgressiveMode(ImageWriteParam.MODE_DEFAULT); } MemoryCacheImageOutputStream output = new MemoryCacheImageOutputStream(bos); writer.setOutput(output); IIOImage iomage = new IIOImage(image, null,null); writer.write(null, iomage, iwp); bos.flush(); return new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); }
final ImageOutputStream iostream = new MemoryCacheImageOutputStream( bostream ) )
@Override public void writeImageFile(OutputStream outStream, String format, ByteBuffer imageData, int width, int height) throws IOException { BufferedImage awtImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Screenshots.convertScreenShot2(imageData.asIntBuffer(), awtImage); ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next(); ImageWriteParam writeParam = writer.getDefaultWriteParam(); if (format.equals("jpg")) { JPEGImageWriteParam jpegParam = (JPEGImageWriteParam) writeParam; jpegParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); jpegParam.setCompressionQuality(0.95f); } awtImage = verticalFlip(awtImage); ImageOutputStream imgOut = new MemoryCacheImageOutputStream(outStream); writer.setOutput(imgOut); IIOImage outputImage = new IIOImage(awtImage, null, null); try { writer.write(null, outputImage, writeParam); } finally { imgOut.close(); writer.dispose(); } }
@Override protected OutputStream makeObject() { return new IIOOutputStreamAdapter(new MemoryCacheImageOutputStream(new ByteArrayOutputStream())); }
private static PDImageXObject createFromGrayImage(BufferedImage image, PDDocument document) throws IOException { int height = image.getHeight(); int width = image.getWidth(); int[] rgbLineBuffer = new int[width]; int bpc = image.getColorModel().getPixelSize(); ByteArrayOutputStream baos = new ByteArrayOutputStream(((width*bpc/8)+(width*bpc%8 != 0 ? 1:0))*height); try (MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos)) { for (int y = 0; y < height; ++y) { for (int pixel : image.getRGB(0, y, width, 1, rgbLineBuffer, 0, width)) { mcios.writeBits(pixel & 0xFF, bpc); } int bitOffset = mcios.getBitOffset(); if (bitOffset != 0) { mcios.writeBits(0, 8 - bitOffset); } } mcios.flush(); } return prepareImageXObject(document, baos.toByteArray(), image.getWidth(), image.getHeight(), bpc, PDDeviceGray.INSTANCE); }
/** * Builds a {@link ImageOutputStream} writing to <code>destination</code>, based on logic that * involves the image size * * @param image the image to be written on the destination (can be null) * @param destination the destination * @return * @throws IOException */ public static ImageOutputStream createImageOutputStream(RenderedImage image, Object destination) throws IOException { // already what we need? if (destination instanceof ImageOutputStream) { return (ImageOutputStream) destination; } // generate the ImageOutputStream if (destination instanceof OutputStream && filesystemThreshold != null && image != null) { OutputStream stream = (OutputStream) destination; // if going to wrap a output stream and we have a threshold set long imageSize = computeImageSize(image); if (imageSize > filesystemThreshold) { File cacheDirectory = getCacheDirectory(); return new FileCacheImageOutputStream(stream, cacheDirectory); } else { return new MemoryCacheImageOutputStream(stream); } } else { return ImageIO.createImageOutputStream(destination); } }
try (MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos))
@Test(expected = IllegalArgumentException.class) public void testWriteNullDirectory() throws IOException { createWriter().write(null, new MemoryCacheImageOutputStream(new ByteArrayOutputStream())); }
try (MemoryCacheImageOutputStream out = new MemoryCacheImageOutputStream(encoded))
@Test public void testWrite() throws IOException { List<IPTCEntry> entries = new ArrayList<>(); entries.add(new IPTCEntry(IPTC.TAG_KEYWORDS, new String[] {"Uno", "Due", "Tre"})); Directory iptc = new IPTCDirectory(entries); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); MemoryCacheImageOutputStream stream = new MemoryCacheImageOutputStream(bytes); createWriter().write(iptc, stream); stream.close(); Directory written = createReader().read(new ByteArrayImageInputStream(bytes.toByteArray())); assertEquals(iptc, written); } }
MemoryCacheImageOutputStream memoryImage=new MemoryCacheImageOutputStream(byteOutput); try{ ImageIO.write(image, "png", memoryImage);
private ByteArrayOutputStream transcode(final ImageReader reader, final URL resource, final ImageWriter writer, int outCSType) throws IOException { try (ImageInputStream input = ImageIO.createImageInputStream(resource)) { reader.setInput(input); ImageTypeSpecifier specifier = null; Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0); while (types.hasNext()) { ImageTypeSpecifier type = types.next(); if (type.getColorModel().getColorSpace().getType() == outCSType) { specifier = type; break; } } // Read image with requested color space ImageReadParam readParam = reader.getDefaultReadParam(); readParam.setSourceRegion(new Rectangle(Math.min(100, reader.getWidth(0)), Math.min(100, reader.getHeight(0)))); readParam.setDestinationType(specifier); IIOImage image = reader.readAll(0, readParam); // Write it back ByteArrayOutputStream bytes = new ByteArrayOutputStream(1024); try (ImageOutputStream output = new MemoryCacheImageOutputStream(bytes)) { writer.setOutput(output); ImageWriteParam writeParam = writer.getDefaultWriteParam(); writeParam.setDestinationType(specifier); writer.write(null, image, writeParam); return bytes; } } }
@Test public void testRewriteExisting() throws IOException { IPTCReader reader = createReader(); Directory iptc = reader.read(getDataAsIIS()); assumeNotNull(iptc); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); MemoryCacheImageOutputStream stream = new MemoryCacheImageOutputStream(bytes); createWriter().write(iptc, stream); stream.close(); Directory written = reader.read(new ByteArrayImageInputStream(bytes.toByteArray())); assertEquals(iptc, written); }
@Test public void testFlushOnAdapterDoesNotMoveFlushedPositionInBacking() throws IOException { MemoryCacheImageOutputStream backing = new MemoryCacheImageOutputStream(new ByteArrayOutputStream()); IIOOutputStreamAdapter adapter = new IIOOutputStreamAdapter(backing); // Sanity check assertEquals(0, backing.getFlushedPosition()); // Write & flush adapter.write(0xCA); adapter.write(new byte[8]); adapter.write(0xFE); adapter.flush(); // Assertions assertEquals(10, backing.length()); assertEquals(10, backing.getStreamPosition()); assertEquals(0, backing.getFlushedPosition()); // Just make sure we can safely seek back to start and read data back backing.seek(0); assertEquals(0, backing.getStreamPosition()); // If this can be read, I think the contract of flush is also fulfilled (kind of) assertEquals(0xCA, backing.read()); assertEquals(8, backing.skipBytes(8)); assertEquals(0xFE, backing.read()); } }
private Resource getAsResource(BufferedImage image, ImageMime mime) throws IOException { ImageWriter writer = mime.getImageWriter(image); ByteArrayOutputStream os = new ByteArrayOutputStream(); final MemoryCacheImageOutputStream ios = new MemoryCacheImageOutputStream(os); writer.setOutput(ios); writer.write(image); ios.flush(); return new ByteArrayResource(os.toByteArray()); }
MemoryCacheImageOutputStream mcos = new MemoryCacheImageOutputStream(os))