public static void main(String[] args) throws IOException { OutputStream ostream = new ByteArrayOutputStream(); File f = null; FileCacheImageOutputStream fcios = new FileCacheImageOutputStream(ostream, f); fcios.writeBit(1); fcios.write(96); fcios.seek(0); int r1 = fcios.read(); if (r1 != 128 ) { throw new RuntimeException("Failed, first byte is " + r1); } int r2 = fcios.read(); if (r2 != 96) { throw new RuntimeException("Failed, second byte is " + r2); } }
/** * Writes the image to an output stream * * @param img * @param ext * @param os */ public static void writeImageWithCompression(BufferedImage img, String ext, OutputStream os) { img = fixImage(img, ext); try { Iterator<ImageWriter> i = ImageIO.getImageWritersBySuffix(ext); ImageWriter jpegWriter = i.next(); // Set the compression quality to 0.8 ImageWriteParam param = jpegWriter.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(getDefaultImageCompression()); // Write the image to a file FileCacheImageOutputStream out = new FileCacheImageOutputStream(os, getTempImageCacheDir()); jpegWriter.setOutput(out); jpegWriter.write(null, new IIOImage(img, null, null), param); jpegWriter.dispose(); out.flush(); out.close(); } catch (IOException e) { log.error("Failed to save image to stream", e); } }
public void writeImage( final BufferedImage sourceImage, final OutputStream outputStream, final File cacheDir) throws IOException { try ( final CloseableImageWriter writer = new CloseableImageWriter("jpg"); // TODO: should this be in-memory? final ImageOutputStream imageOutputStream = new FileCacheImageOutputStream(outputStream, cacheDir) ) { writer.setOutput(imageOutputStream); final BufferedImage resizedImage = this.resizeImage(sourceImage); final IIOImage outputImage = new IIOImage(resizedImage, null, null); final ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(this.quality); writer.write(outputImage, param); } }
private ImageOutputStream createImageOutputStream(OutputStream os) throws IOException { if (this.cacheDir != null) { return new FileCacheImageOutputStream(os, this.cacheDir); } else { return new MemoryCacheImageOutputStream(os); } }
public ImageOutputStream createOutputStreamInstance(Object output, boolean useCache, File cacheDir) throws IOException { if (output instanceof OutputStream) { OutputStream os = (OutputStream)output; if (useCache) { return new FileCacheImageOutputStream(os, cacheDir); } else { return new MemoryCacheImageOutputStream(os); } } else { throw new IllegalArgumentException(); } }
public static void main(String[] args) throws IOException { OutputStream ostream = new ByteArrayOutputStream(); FileCacheImageOutputStream fcios = new FileCacheImageOutputStream(ostream, null); test(fcios); MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(ostream); test(mcios); }
@Override public ImageOutputStream createOutputStreamInstance(Object output, boolean useCache, File cacheDir) throws IOException { if (output instanceof OutputStream) { if (useCache) { return new FileCacheImageOutputStream((OutputStream) output, cacheDir); } else { return new MemoryCacheImageOutputStream((OutputStream) output); } } throw new IllegalArgumentException(Messages.getString("imageio.85")); }
@Override public ImageOutputStream createOutputStreamInstance(Object output, boolean useCache, File cacheDir) throws IOException { if (output instanceof OutputStream) { if (useCache) { return new FileCacheImageOutputStream((OutputStream) output, cacheDir); } else { return new MemoryCacheImageOutputStream((OutputStream) output); } } throw new IllegalArgumentException("Output is not an instance of OutputStream"); }