Refine search
/** * 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(); }
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); }
@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); } }
JPEGWriter.getDefaultStreamMetadata(JPEGParam); new ByteArrayOutputStream(); MemoryCacheImageOutputStream tableStream = new MemoryCacheImageOutputStream(tableByteStream); JPEGWriter.setOutput(tableStream); try { JPEGWriter.prepareWriteSequence(JPEGStreamMetadata); tableStream.flush(); JPEGWriter.endWriteSequence(); byte[] tables = tableByteStream.toByteArray(); if(DEBUG) System.out.println("tables.length = "+ tables.length);
if ( avatar.getWidth() <= targetDimension && avatar.getHeight() <= targetDimension ) Log.debug( "Original image dimension ({}x{}) is within acceptable bounds ({}x{}). No need to resize.", new Object[] { avatar.getWidth(), avatar.getHeight(), targetDimension, targetDimension }); return null; try ( final ByteArrayOutputStream bostream = new ByteArrayOutputStream(); final ImageOutputStream iostream = new MemoryCacheImageOutputStream( bostream ) ) iw.setOutput( iostream ); iw.write( resizedAvatar ); final byte[] data = bostream.toByteArray();
public static byte[] toJpg(BufferedImage image) throws IOException { // Start with a white layer to have images with an alpha layer handled correctly. BufferedImage newBufferedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); newBufferedImage.createGraphics().drawImage(image, 0, 0, Color.WHITE, null); // Manually get the ImageWriter to be able to adjust quality ImageWriter writer = ImageIO.getImageWritersBySuffix("jpg").next(); ImageWriteParam imageWriterParam = writer.getDefaultWriteParam(); imageWriterParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriterParam.setCompressionQuality(1f); ByteArrayOutputStream output = new ByteArrayOutputStream(); writer.setOutput(new MemoryCacheImageOutputStream(output)); writer.write(null, new IIOImage(newBufferedImage, null, null), imageWriterParam); writer.dispose(); return output.toByteArray(); }
height-=verticalBorderWidth; SlashValue content=(SlashValue)cell.getValue(); BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics2D g=(Graphics2D)image.getGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); ByteArrayOutputStream byteOutput=new ByteArrayOutputStream(); MemoryCacheImageOutputStream memoryImage=new MemoryCacheImageOutputStream(byteOutput); try{ ImageIO.write(image, "png", memoryImage); imageBytes=byteOutput.toByteArray(); String base64Data=Base64Utils.encodeToString(imageBytes); content.setBase64Data(base64Data); try { if(memoryImage!=null){ memoryImage.close(); byteOutput.close();
private static PDImageXObject createAlphaFromARGBImage2(PDDocument document, BufferedImage bi) throws IOException ByteArrayOutputStream bos = new ByteArrayOutputStream(); int bpc; if (bi.getTransparency() == Transparency.BITMASK) try (MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos)) for (int y = 0, h = bi.getHeight(); y < h; ++y) for (int x = 0, w = bi.getWidth(); x < w; ++x) int alpha = bi.getRGB(x, y) >>> 24; mcios.writeBit(alpha); while (mcios.getBitOffset() != 0) mcios.writeBit(0); mcios.flush(); int alpha = bi.getRGB(x, y) >>> 24; bos.write(alpha); PDImageXObject pdImage = prepareImageXObject(document, bos.toByteArray(), bi.getWidth(), bi.getHeight(), bpc, PDDeviceGray.INSTANCE);
if (!image.getColorModel().hasAlpha()) WritableRaster alphaRaster = image.getAlphaRaster(); if (alphaRaster == null) try (MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos)) for (int pixel : pixels) mcios.writeBit(pixel); ++p; if (p % width == 0) while (mcios.getBitOffset() != 0) mcios.writeBit(0); mcios.flush(); for (int pixel : pixels) bos.write(pixel); PDImageXObject pdImage = prepareImageXObject(document, bos.toByteArray(), image.getWidth(), image.getHeight(), bpc, PDDeviceGray.INSTANCE);
public void testWriteToStream() throws IOException { final TiffLong tiffLong = new TiffLong(_TIFFLONG_MAX); final MemoryCacheImageOutputStream stream = new MemoryCacheImageOutputStream(new ByteArrayOutputStream()); tiffLong.write(stream); assertEquals(4, stream.length()); stream.seek(0); assertEquals(0xffffffff, stream.readInt()); }
/** * This method gets the encoded image from the given object as a byte array * of data. */ protected byte[] extractImageEncoding(IIOImage iioimage, ImageWriteParam param) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); MemoryCacheImageOutputStream imageOutput = new MemoryCacheImageOutputStream( baos); writer.setOutput(imageOutput); writer.write(null, iioimage, writeParam); imageOutput.close(); baos.close(); byte[] data = baos.toByteArray(); // Write little endian raw data if( (!encapsulated) && bytes==2 && ! dos.getTransferSyntax().bigEndian() ) { for(int i=0; i<data.length; i+=2) { byte swap = data[i]; data[i] = data[i+1]; data[i+1] = swap; } } return data; }
BufferedImage bufferedImage = new BufferedImage(scaledImage.getWidth(null), scaledImage. getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D graphics = bufferedImage.createGraphics(); graphics.drawImage(scaledImage, 0, 0, null); scaledImage.flush(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); MemoryCacheImageOutputStream mciis = new MemoryCacheImageOutputStream(baos); ImageIO.write(bufferedImage, IMAGE_JPEG_FORMAT, mciis); mciis.flush(); bufferedImage.flush(); byte[] jpeg = baos.toByteArray(); mciis.close();
private byte[] convertToJPG(BufferedImage img) throws IOException { ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(JPG_QUALITY); ByteArrayOutputStream bout = new ByteArrayOutputStream(); writer.setOutput(new MemoryCacheImageOutputStream(bout)); writer.write(null, new IIOImage(img, null, null), iwp); writer.dispose(); bout.flush(); return bout.toByteArray(); }
@Override protected OutputStream makeObject() { return new IIOOutputStreamAdapter(new MemoryCacheImageOutputStream(new ByteArrayOutputStream())); }
ByteArrayOutputStream os = new ByteArrayOutputStream(); writer.setOutput(new MemoryCacheImageOutputStream(os)); writer.write(image); image.flush(); image = null; resultStream = new ByteArrayInputStream(os.toByteArray()); } else { resultStream = imageStream;
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()); }
private byte[] createIFDBytes(final TiffIFD ifd, final int startOffset) throws IOException { final ByteArrayOutputStream bout = new ByteArrayOutputStream(); final MemoryCacheImageOutputStream ios = new MemoryCacheImageOutputStream(bout); ios.seek(startOffset); final int nextIFDOffset = 0; TiffTag.BEAM_METADATA }; ios.writeShort(entryTags.length); ios.seek(nextEntryPos); ifd.getEntry(entryTags[i]).write(ios); nextEntryPos += TiffDirectoryEntry.BYTES_PER_ENTRY; ios.writeInt(nextIFDOffset); ios.flush(); byte[] bytes = bout.toByteArray(); return Arrays.copyOfRange(bytes, startOffset, bytes.length);
public void testWrite() throws Exception { final GeoTiffAscii tiffAscii = new GeoTiffAscii("Alois und Sepp"); final ByteArrayOutputStream os = new ByteArrayOutputStream(); final MemoryCacheImageOutputStream ios = new MemoryCacheImageOutputStream(os); tiffAscii.write(ios); ios.flush(); assertEquals("Alois und Sepp|\u0000", os.toString()); }
@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()); } }
@Override protected void setUp() throws Exception { final ByteArrayOutputStream os = new ByteArrayOutputStream(24); _ios = new MemoryCacheImageOutputStream(os); _prefix = "Ancillery3RecordTest_prefix"; _ios.writeBytes(_prefix); writeRecordData(_ios); _ios.writeBytes("Ancillery3RecordTest_suffix"); // suffix _reader = new CeosFileReader(_ios); }