List of usage examples for javax.imageio ImageWriter setOutput
public void setOutput(Object output)
From source file:org.dcm4che2.tool.dcm2jpg.Dcm2Jpg.java
private void encodeByImageIO(BufferedImage bi, File dest) throws IOException { ImageWriter writer = getImageWriter(imageWriterClassname); ImageOutputStream out = null; try {//from w ww .j a v a2 s . c om out = ImageIO.createImageOutputStream(dest); writer.setOutput(out); ImageWriteParam iwparam = writer.getDefaultWriteParam(); if (iwparam.canWriteCompressed()) { iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); String[] compressionTypes = iwparam.getCompressionTypes(); if (compressionTypes != null && compressionTypes.length > 0) { if (compressionType != null || iwparam.getCompressionType() == null) { for (int i = 0; i < compressionTypes.length; i++) { if (compressionType == null || compressionTypes[i].compareToIgnoreCase(compressionType) == 0) { iwparam.setCompressionType(compressionTypes[i]); break; } } } } if (imageQuality != null) iwparam.setCompressionQuality(imageQuality); } else if (imageQuality != null) { System.out.println("Selected Image Writer can not compress! imageQuality is ignored!"); } writer.write(null, new IIOImage(bi, null, null), iwparam); } finally { CloseUtils.safeClose(out); writer.dispose(); } }
From source file:jails.http.converter.BufferedImageHttpMessageConverter.java
public void write(BufferedImage image, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { if (contentType == null) { contentType = getDefaultContentType(); }//from www . j a va 2 s. c o m Assert.notNull(contentType, "Count not determine Content-Type, set one using the 'defaultContentType' property"); outputMessage.getHeaders().setContentType(contentType); ImageOutputStream imageOutputStream = null; ImageWriter imageWriter = null; try { imageOutputStream = createImageOutputStream(outputMessage.getBody()); Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByMIMEType(contentType.toString()); if (imageWriters.hasNext()) { imageWriter = imageWriters.next(); ImageWriteParam iwp = imageWriter.getDefaultWriteParam(); process(iwp); imageWriter.setOutput(imageOutputStream); imageWriter.write(null, new IIOImage(image, null, null), iwp); } else { throw new HttpMessageNotWritableException( "Could not find javax.imageio.ImageWriter for Content-Type [" + contentType + "]"); } } finally { if (imageWriter != null) { imageWriter.dispose(); } if (imageOutputStream != null) { try { imageOutputStream.close(); } catch (IOException ex) { // ignore } } } }
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 w w . j a v a2 s. co 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 www .ja va2 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:ImageIOTest.java
/** * Save the current image in a file/*from ww w . jav a 2 s. c om*/ * @param formatName the file format */ public void saveFile(final String formatName) { if (images == null) return; Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(formatName); ImageWriter writer = iter.next(); JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); String[] extensions = writer.getOriginatingProvider().getFileSuffixes(); chooser.setFileFilter(new FileNameExtensionFilter("Image files", extensions)); int r = chooser.showSaveDialog(this); if (r != JFileChooser.APPROVE_OPTION) return; File f = chooser.getSelectedFile(); try { ImageOutputStream imageOut = ImageIO.createImageOutputStream(f); writer.setOutput(imageOut); writer.write(new IIOImage(images[0], null, null)); for (int i = 1; i < images.length; i++) { IIOImage iioImage = new IIOImage(images[i], null, null); if (writer.canInsertImage(i)) writer.writeInsert(i, iioImage, null); } } catch (IOException e) { JOptionPane.showMessageDialog(this, e); } }
From source file:org.polymap.core.data.image.ImageEncodeProcessor.java
private void imageioEncodeJPEG(Image image, ChunkedResponseOutputStream out) throws IOException { // this code is from http://forums.sun.com/thread.jspa?threadID=5197061 ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam param = writer.getDefaultWriteParam(); param.setSourceBands(new int[] { 0, 1, 2 }); ColorModel cm = new DirectColorModel(24, 0x00ff0000, // Red 0x0000ff00, // Green 0x000000ff, // Blue 0x0); // Alpha param.setDestinationType(new ImageTypeSpecifier(cm, cm.createCompatibleSampleModel(1, 1))); ImageOutputStream imageOut = ImageIO.createImageOutputStream(out); writer.setOutput(imageOut); writer.write(null, new IIOImage((RenderedImage) image, null, null), param); writer.dispose();/*ww w . j av a 2 s. c o m*/ imageOut.close(); }
From source file:com.sire.web.CajFacturaEnviadaBean.java
private void savePicture() { if (file != null) { try {/*from w w w . jav a 2 s . com*/ BufferedImage originalImage = ImageIO.read(file.getInputstream()); BufferedImage outputImage = new BufferedImage((int) (originalImage.getWidth() * 0.25), (int) (originalImage.getHeight() * 0.25), originalImage.getType()); Graphics2D g2d = outputImage.createGraphics(); g2d.drawImage(originalImage, 0, 0, (int) (originalImage.getWidth() * 0.25), (int) (originalImage.getHeight() * 0.25), null); g2d.dispose(); String imagesFolder = System.getProperty("imagesFolder"); if (imagesFolder == null) { String currentUsersHomeDir = System.getProperty("user.home"); imagesFolder = currentUsersHomeDir + File.separator + "photos"; } Iterator iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = (ImageWriter) iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); float quality = 1.0f; // reduce quality by 0% iwp.setCompressionQuality(quality); File f = new File(imagesFolder + File.separator + fileName); try (FileImageOutputStream output = new FileImageOutputStream(f)) { writer.setOutput(output); IIOImage image = new IIOImage(outputImage, null, null); writer.write(null, image, iwp); writer.dispose(); } } catch (IOException ex) { LOGGER.severe(ex.getMessage()); } } }
From source file:com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpRenderListener.java
private byte[] getJPGBytes(BufferedImage image) { ByteArrayOutputStream outputStream = null; try {/*from w ww .j a v a2 s . c o m*/ ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next(); ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam(); jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); jpgWriteParam.setCompressionQuality(1.0f); outputStream = new ByteArrayOutputStream(); jpgWriter.setOutput(new MemoryCacheImageOutputStream((outputStream))); IIOImage outputImage = new IIOImage(image, null, null); jpgWriter.write(null, outputImage, jpgWriteParam); jpgWriter.dispose(); outputStream.flush(); return outputStream.toByteArray(); } catch (Exception e) { throw new RuntimeException(e); } finally { closeOutputStream(outputStream); } }
From source file:com.lingxiang2014.util.ImageUtils.java
public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { Assert.notNull(srcFile);//from ww w .j a v a 2 s. c om Assert.notNull(destFile); Assert.state(destWidth > 0); Assert.state(destHeight > 0); if (type == Type.jdk) { Graphics2D graphics2D = null; ImageOutputStream imageOutputStream = null; ImageWriter imageWriter = null; try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); imageOutputStream = ImageIO.createImageOutputStream(destFile); imageWriter = ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName())) .next(); imageWriter.setOutput(imageOutputStream); ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality((float) (DEST_QUALITY / 100.0)); imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam); imageOutputStream.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (graphics2D != null) { graphics2D.dispose(); } if (imageWriter != null) { imageWriter.dispose(); } if (imageOutputStream != null) { try { imageOutputStream.close(); } catch (IOException e) { } } } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }
From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java
public static void writeJpeg(BufferedImage img, float quality, File file) throws IOException { if (img == null) { return;/*from w w w .jav a2s. com*/ } float q = quality; if (q < 0) { q = 0; } if (q > 1) { q = 1; } Iterator iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = (ImageWriter) iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(q); // float between 0 and 1 // 1 specifies minimum compression and maximum quality FileImageOutputStream output = new FileImageOutputStream(file); writer.setOutput(output); IIOImage image = new IIOImage(img, null, null); writer.write(null, image, iwp); writer.dispose(); }