List of usage examples for javax.imageio ImageWriter endWriteSequence
public void endWriteSequence() throws IOException
From source file:net.d53dev.dslfy.web.service.GifService.java
public void saveAnimatedGIF(OutputStream out, List<GifFrame> frames, int loopCount) throws Exception { ImageWriter iw = ImageIO.getImageWritersByFormatName("gif").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(out); iw.setOutput(ios);//from ww w. ja va 2 s.co m iw.prepareWriteSequence(null); int p = 0; for (GifFrame frame : frames) { ImageWriteParam iwp = iw.getDefaultWriteParam(); IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(frame.img), iwp); this.configureGIFFrame(metadata, String.valueOf(frame.delay / 10L), p++, frame.disposalMethod, loopCount); IIOImage ii = new IIOImage(frame.img, null, metadata); iw.writeToSequence(ii, null); } iw.endWriteSequence(); ios.close(); }
From source file:de.unigoettingen.sub.commons.contentlib.imagelib.JpegInterpreter.java
@Override public byte[] writeToStreamAndByteArray(OutputStream outStream) { byte[] data = null; if (this.renderedimage == null) { // no image available return data; }//from w ww. j av a 2 s . c om try { // create a buffered Image, which has no Alpha channel // as JPEG does not support Alpha Channels and the // ImageIO doesn't care - but will create a corrupt JPEG BufferedImage noAlphaBi = ImageManipulator.fromRenderedToBufferedNoAlpha(renderedimage); ImageOutputStream imageOutStream = ImageIO.createImageOutputStream(outStream); // Iterator<ImageWriter> writerIter = ImageIO // .getImageWritersByFormatName("jpg"); // ImageWriter writer = writerIter.next(); // get writer from ImageIO ImageWriter writer = new JPEGImageWriter(new JPEGImageWriterSpi()); // create metadata by creating an XML tree ImageWriteParam writerParam = writer.getDefaultWriteParam(); ImageTypeSpecifier its = new ImageTypeSpecifier(noAlphaBi); // ImageTypeSpecifier its = new // ImageTypeSpecifier(image.getColorModel(), // image.getSampleModel()); // IIOMetadata iomd = writer.getDefaultImageMetadata(new // ImageTypeSpecifier(image), writerParam); // Element tree = // (Element)iomd.getAsTree("javax_imageio_jpeg_image_1.0"); // Element tree = (Element)iomd.getAsTree("javax_imageio_1.0"); // IIOMetadata iomd = writer.getDefaultImageMetadata(its, writerParam); // create the XML tree and modify the appropriate DOM elements // to set the metadata setMetadata(iomd); // set compression writerParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); float comprvalue = ((float) writerCompressionValue) / 100; writerParam.setCompressionQuality(comprvalue); // set output writer.setOutput(imageOutStream); writer.prepareWriteSequence(null); // create new image parameters to set the compression // Locale locale = new Locale("en"); // JPEGImageWriteParam jpegWriteParam = new // JPEGImageWriteParam(locale); // IIOImage iioImage = new IIOImage(renderedimage, null, iomd); IIOImage iioImage = new IIOImage(noAlphaBi, null, iomd); writer.write(null, iioImage, writerParam); writer.endWriteSequence(); imageOutStream.flush(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageOutputStream imageToFile = ImageIO.createImageOutputStream(baos); writer.setOutput(imageToFile); writer.prepareWriteSequence(null); writer.write(null, iioImage, writerParam); writer.endWriteSequence(); imageToFile.flush(); imageToFile.close(); baos.flush(); data = baos.toByteArray(); baos.close(); writer.dispose(); imageOutStream.close(); } catch (IOException e) { LOGGER.error("IOException occured", e); } return data; }
From source file:de.unigoettingen.sub.commons.contentlib.imagelib.JpegInterpreter.java
/************************************************************************************ * Write the renderedimage to an {@link OutputStream} * /*w w w. j a v a 2 s.c o m*/ * @param outStream the {@link OutputStream} to write to ************************************************************************************/ @Override public void writeToStream(FileOutputStream fos, OutputStream outStream) { if (this.renderedimage == null) { // no image available return; } try { // create a buffered Image, which has no Alpha channel // as JPEG does not support Alpha Channels and the // ImageIO doesn't care - but will create a corrupt JPEG BufferedImage noAlphaBi = ImageManipulator.fromRenderedToBufferedNoAlpha(renderedimage); ImageOutputStream imageOutStream = ImageIO.createImageOutputStream(outStream); // Iterator<ImageWriter> writerIter = ImageIO // .getImageWritersByFormatName("jpg"); // ImageWriter writer = writerIter.next(); // get writer from ImageIO ImageWriter writer = new JPEGImageWriter(new JPEGImageWriterSpi()); // create metadata by creating an XML tree ImageWriteParam writerParam = writer.getDefaultWriteParam(); ImageTypeSpecifier its = new ImageTypeSpecifier(noAlphaBi); // ImageTypeSpecifier its = new // ImageTypeSpecifier(image.getColorModel(), // image.getSampleModel()); // IIOMetadata iomd = writer.getDefaultImageMetadata(new // ImageTypeSpecifier(image), writerParam); // Element tree = // (Element)iomd.getAsTree("javax_imageio_jpeg_image_1.0"); // Element tree = (Element)iomd.getAsTree("javax_imageio_1.0"); // IIOMetadata iomd = writer.getDefaultImageMetadata(its, writerParam); // create the XML tree and modify the appropriate DOM elements // to set the metadata setMetadata(iomd); // set compression writerParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); float comprvalue = ((float) writerCompressionValue) / 100; writerParam.setCompressionQuality(comprvalue); // set output writer.setOutput(imageOutStream); writer.prepareWriteSequence(null); // create new image parameters to set the compression // Locale locale = new Locale("en"); // JPEGImageWriteParam jpegWriteParam = new // JPEGImageWriteParam(locale); // IIOImage iioImage = new IIOImage(renderedimage, null, iomd); IIOImage iioImage = new IIOImage(noAlphaBi, null, iomd); writer.write(null, iioImage, writerParam); writer.endWriteSequence(); imageOutStream.flush(); if (fos != null) { ImageOutputStream imageToFile = ImageIO.createImageOutputStream(fos); writer.setOutput(imageToFile); writer.prepareWriteSequence(null); writer.write(null, iioImage, writerParam); writer.endWriteSequence(); imageToFile.flush(); imageToFile.close(); fos.flush(); fos.close(); } // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // byte [] data = wi.getImageAsByteArray(); writer.dispose(); imageOutStream.close(); } catch (IOException e) { LOGGER.error("IOException occured", e); } }
From source file:org.dcm4che2.tool.dcm2dcm.Dcm2Dcm.java
/** * Recodes the images from the source transfer syntax, as read from the src * file, to the specified destination syntax. *///from w w w . j a v a2 s.c o m public void recodeImages(File src, File dest) throws IOException { ImageReader reader = new DicomImageReaderSpi().createReaderInstance(); ImageWriter writer = new DicomImageWriterSpi().createWriterInstance(); DicomStreamMetaData writeMeta = (DicomStreamMetaData) writer.getDefaultStreamMetadata(null); DicomObject ds; int frames; FileImageOutputStream output = null; try { FileImageInputStream input = new FileImageInputStream(src); try { reader.setInput(input); if (dest.exists()) dest.delete(); output = new FileImageOutputStream(dest); writer.setOutput(output); DicomStreamMetaData streamMeta = (DicomStreamMetaData) reader.getStreamMetadata(); ds = streamMeta.getDicomObject(); DicomObject newDs = new BasicDicomObject(); ds.copyTo(newDs); writeMeta.setDicomObject(newDs); frames = ds.getInt(Tag.NumberOfFrames, 1); newDs.putString(Tag.TransferSyntaxUID, VR.UI, destinationSyntax.uid()); newDs.putString(Tag.ImplementationClassUID, VR.UI, Implementation.classUID()); newDs.putString(Tag.ImplementationVersionName, VR.SH, Implementation.versionName()); if (overwriteObject != null) { overwriteObject.copyTo(newDs); } } finally { reader.dispose(); } writer.prepareWriteSequence(writeMeta); for (int i = 0; i < frames; i++) { FileInputStream inputStream = new FileInputStream(src); try { WritableRaster r = (WritableRaster) readRaster(inputStream, i); ColorModel cm = ColorModelFactory.createColorModel(ds); BufferedImage bi = new BufferedImage(cm, r, false, null); IIOImage iioimage = new IIOImage(bi, null, null); writer.writeToSequence(iioimage, null); } catch (NoSuchFieldException ex) { System.err.println(ex.toString()); } catch (IllegalAccessException ex) { System.err.println(ex.toString()); } finally { inputStream.close(); } } writer.endWriteSequence(); } finally { if (output != null) { output.close(); } } }
From source file:org.psystems.dicomweb.Dcm2Dcm.java
/** * Recodes the images from the source transfer syntax, as read from the src * file, to the specified destination syntax. */// w ww .j av a2 s . c o m public void recodeImages(File src, File dest) throws IOException { ImageReader reader = new DicomImageReaderSpi().createReaderInstance(); ImageWriter writer = new DicomImageWriterSpi().createWriterInstance(); FileImageInputStream input = new FileImageInputStream(src); reader.setInput(input); if (dest.exists()) dest.delete(); FileImageOutputStream output = new FileImageOutputStream(dest); writer.setOutput(output); DicomStreamMetaData streamMeta = (DicomStreamMetaData) reader.getStreamMetadata(); DicomObject ds = streamMeta.getDicomObject(); DicomStreamMetaData writeMeta = (DicomStreamMetaData) writer.getDefaultStreamMetadata(null); DicomObject newDs = new BasicDicomObject(); ds.copyTo(newDs); writeMeta.setDicomObject(newDs); int frames = ds.getInt(Tag.NumberOfFrames, 1); LookupTable lut = prepareBitStrip(writeMeta, reader); newDs.putString(Tag.TransferSyntaxUID, VR.UI, destinationSyntax.uid()); if (overwriteObject != null) { overwriteObject.copyTo(newDs); } writer.prepareWriteSequence(writeMeta); for (int i = 0; i < frames; i++) { WritableRaster r = (WritableRaster) reader.readRaster(i, null); ColorModel cm = ColorModelFactory.createColorModel(ds); BufferedImage bi = new BufferedImage(cm, r, false, null); if (lut != null) { lut.lookup(bi.getRaster(), bi.getRaster()); } IIOImage iioimage = new IIOImage(bi, null, null); writer.writeToSequence(iioimage, null); } writer.endWriteSequence(); output.close(); input.close(); }
From source file:org.psystems.dicomweb.Dcm2DcmCopy.java
/** * Recodes the images from the source transfer syntax, as read from the src * file, to the specified destination syntax. *///from w w w . j a v a2 s .com public void recodeImages(File src, File dest) throws IOException { ImageReader reader = new DicomImageReaderSpi().createReaderInstance(); ImageWriter writer = new DicomImageWriterSpi().createWriterInstance(); FileImageInputStream input = new FileImageInputStream(src); reader.setInput(input); if (dest.exists()) dest.delete(); FileImageOutputStream output = new FileImageOutputStream(dest); writer.setOutput(output); DicomStreamMetaData streamMeta = (DicomStreamMetaData) reader.getStreamMetadata(); DicomObject ds = streamMeta.getDicomObject(); DicomStreamMetaData writeMeta = (DicomStreamMetaData) writer.getDefaultStreamMetadata(null); DicomObject newDs = new BasicDicomObject(); ds.copyTo(newDs); writeMeta.setDicomObject(newDs); int frames = ds.getInt(Tag.NumberOfFrames, 1); LookupTable lut = prepareBitStrip(writeMeta, reader); newDs.putString(Tag.TransferSyntaxUID, VR.UI, destinationSyntax.uid()); if (overwriteObject != null) { overwriteObject.copyTo(newDs); } writer.prepareWriteSequence(writeMeta); for (int i = 0; i < frames; i++) { WritableRaster r = (WritableRaster) reader.readRaster(i, null); ColorModel cm = ColorModelFactory.createColorModel(ds); BufferedImage bi = new BufferedImage(cm, r, false, null); if (lut != null) { lut.lookup(bi.getRaster(), bi.getRaster()); } IIOImage iioimage = new IIOImage(bi, null, null); writer.writeToSequence(iioimage, null); } writer.endWriteSequence(); output.close(); input.close(); }