List of usage examples for javax.imageio ImageWriter write
public void write(RenderedImage image) throws IOException
From source file:Main.java
static public void main(String args[]) throws Exception { int width = 200, height = 200; BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D ig2 = bi.createGraphics(); ig2.fillRect(0, 0, width - 1, height - 1); Iterator imageWriters = ImageIO.getImageWritersByFormatName("GIF"); ImageWriter imageWriter = (ImageWriter) imageWriters.next(); File file = new File("filename.gif"); ImageOutputStream ios = ImageIO.createImageOutputStream(file); imageWriter.setOutput(ios);//from w w w .jav a 2s . c om imageWriter.write(bi); }
From source file:Main.java
static public void main(String args[]) throws Exception { FileInputStream fin = new FileInputStream("a.gif"); Iterator readers = ImageIO.getImageReadersBySuffix("GIF"); ImageReader imageReader = (ImageReader) readers.next(); ImageInputStream iis = ImageIO.createImageInputStream(fin); imageReader.setInput(iis, false);//from www . j a v a2 s . co m imageReader.addIIOReadProgressListener(new IIOReadProgressListener() { public void imageComplete(ImageReader source) { System.out.println("image complete " + source); } public void imageProgress(ImageReader source, float percentageDone) { System.out.println("image progress " + source + ": " + percentageDone + "%"); } public void imageStarted(ImageReader source, int imageIndex) { System.out.println("image #" + imageIndex + " started " + source); } public void readAborted(ImageReader source) { System.out.println("read aborted " + source); } public void sequenceComplete(ImageReader source) { System.out.println("sequence complete " + source); } public void sequenceStarted(ImageReader source, int minIndex) { System.out.println("sequence started " + source + ": " + minIndex); } public void thumbnailComplete(ImageReader source) { System.out.println("thumbnail complete " + source); } public void thumbnailProgress(ImageReader source, float percentageDone) { System.out.println("thumbnail started " + source + ": " + percentageDone + "%"); } public void thumbnailStarted(ImageReader source, int imageIndex, int thumbnailIndex) { System.out.println("thumbnail progress " + source + ", " + thumbnailIndex + " of " + imageIndex); } }); BufferedImage image = imageReader.read(0); Iterator imageWriters = ImageIO.getImageWritersBySuffix("JPG"); ImageWriter imageWriter = (ImageWriter) imageWriters.next(); File file = new File("b.JPG"); ImageOutputStream ios = ImageIO.createImageOutputStream(file); imageWriter.setOutput(ios); imageWriter.write(image); }
From source file:org.modelibra.util.ImageHandler.java
/** * Saves an image to a jpeg file./* w w w.ja va 2s . c o m*/ * * @param bi * buffered image * @param file * jpeg file * @param quality * image quality */ public static void saveImageToJPEGFile14(BufferedImage bi, File file, float quality) throws IOException { JPEGImageWriteParam param = new JPEGImageWriteParam(null); ImageTypeSpecifier type = new ImageTypeSpecifier(bi); param.setDestinationType(type); ImageOutputStream imgos = ImageIO.createImageOutputStream(file); Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = writers.next(); writer.setOutput(imgos); writer.write(bi); imgos.close(); }
From source file:com.tomtom.speedtools.json.ImageSerializer.java
@Nonnull private static byte[] writeAsBytes(@Nonnull final Image v) throws IOException { assert v != null; /**// w ww . j a v a2 s . c om * Create a PNG output stream. */ final String mimeType = "image/png"; try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) { final Iterator<ImageWriter> it = ImageIO.getImageWritersByMIMEType(mimeType); if (it.hasNext()) { final ImageWriter w = it.next(); final ImageOutputStream os = ImageIO.createImageOutputStream(stream); w.setOutput(os); w.write(convertToBufferedImage(v)); os.close(); w.dispose(); } else { LOG.info("writeAsBytes: No encoder for MIME type " + mimeType); throw new IOException("No encoder for MIME type " + mimeType); } return stream.toByteArray(); } }
From source file:FileUtils.java
/** * Utility method to write BufferedImage object to disk * @param image - BufferedImage object to save. * @param data - relative path to the image * @param format - file prefix of the image * @return BufferedImage representation of the image * *///from w ww . j av a 2s . c o m public static void imageToBitmap(BufferedImage image, String data, String format) throws IOException { final OutputStream inb = new FileOutputStream(data); final ImageWriter wrt = ImageIO.getImageWritersByFormatName(format).next(); final ImageInputStream imageInput = ImageIO.createImageOutputStream(inb); wrt.setOutput(imageInput); wrt.write(image); inb.close(); }
From source file:org.esxx.Response.java
public static void writeObject(Object object, ContentType ct, OutputStream out) throws IOException { if (object == null) { return;/*from w w w . j a v a 2s . com*/ } // Unwrap wrapped objects object = JS.toJavaObject(object); // Convert complex types to primitive types if (object instanceof Node) { ESXX esxx = ESXX.getInstance(); if (ct.match("message/rfc822")) { try { String xml = esxx.serializeNode((Node) object); org.esxx.xmtp.XMTPParser xmtpp = new org.esxx.xmtp.XMTPParser(); javax.mail.Message msg = xmtpp.convertMessage(new StringReader(xml)); object = new ByteArrayOutputStream(); msg.writeTo(new FilterOutputStream((OutputStream) object) { @Override public void write(int b) throws IOException { if (b == '\r') { return; } else if (b == '\n') { out.write('\r'); out.write('\n'); } else { out.write(b); } } }); } catch (javax.xml.stream.XMLStreamException ex) { throw new ESXXException("Failed to serialize Node as message/rfc822:" + ex.getMessage(), ex); } catch (javax.mail.MessagingException ex) { throw new ESXXException("Failed to serialize Node as message/rfc822:" + ex.getMessage(), ex); } } else { object = esxx.serializeNode((Node) object); } } else if (object instanceof Scriptable) { if (ct.match("application/x-www-form-urlencoded")) { String cs = Parsers.getParameter(ct, "charset", "UTF-8"); object = StringUtil.encodeFormVariables(cs, (Scriptable) object); } else if (ct.match("text/csv")) { object = jsToCSV(ct, (Scriptable) object); } else { object = jsToJSON(object).toString(); } } else if (object instanceof byte[]) { object = new ByteArrayInputStream((byte[]) object); } else if (object instanceof File) { object = new FileInputStream((File) object); } // Serialize primitive types if (object instanceof ByteArrayOutputStream) { ByteArrayOutputStream bos = (ByteArrayOutputStream) object; bos.writeTo(out); } else if (object instanceof ByteBuffer) { // Write result as-is to output stream WritableByteChannel wbc = Channels.newChannel(out); ByteBuffer bb = (ByteBuffer) object; bb.rewind(); while (bb.hasRemaining()) { wbc.write(bb); } wbc.close(); } else if (object instanceof InputStream) { IO.copyStream((InputStream) object, out); } else if (object instanceof Reader) { // Write stream as-is, using the specified charset (if present) String cs = Parsers.getParameter(ct, "charset", "UTF-8"); Writer ow = new OutputStreamWriter(out, cs); IO.copyReader((Reader) object, ow); } else if (object instanceof String) { // Write string as-is, using the specified charset (if present) String cs = Parsers.getParameter(ct, "charset", "UTF-8"); Writer ow = new OutputStreamWriter(out, cs); ow.write((String) object); ow.flush(); } else if (object instanceof RenderedImage) { Iterator<ImageWriter> i = ImageIO.getImageWritersByMIMEType(ct.getBaseType()); if (!i.hasNext()) { throw new ESXXException("No ImageWriter available for " + ct.getBaseType()); } ImageWriter writer = i.next(); writer.setOutput(ImageIO.createImageOutputStream(out)); writer.write((RenderedImage) object); } else { throw new UnsupportedOperationException("Unsupported object class type: " + object.getClass()); } }
From source file:org.apache.axis2.jaxws.marshaller.impl.alt.Attachment.java
private static byte[] createBytesFromImage(Image image, String mimeType) { try {//from www .j a va 2 s .c o m ImageWriter imageWriter = null; BufferedImage bufferedImage = (BufferedImage) image; ByteArrayOutputStream baos = new ByteArrayOutputStream(); Iterator iterator = javax.imageio.ImageIO.getImageWritersByMIMEType(mimeType); if (iterator.hasNext()) { imageWriter = (ImageWriter) iterator.next(); } ImageOutputStream ios = javax.imageio.ImageIO.createImageOutputStream(baos); imageWriter.setOutput(ios); imageWriter.write(new IIOImage(bufferedImage, null, null)); ios.flush(); imageWriter.dispose(); return baos.toByteArray(); } catch (IOException e) { throw ExceptionFactory.makeWebServiceException(e); } }
From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java
/** * ???????// w w w .j a v a2 s. c o m * * @param org_id * @param folderName * @param uid * @param fileBean * @param acceptExts * @param msgList * @return */ public static byte[] getBytesShrink(InputStream is, int width, int height, List<String> msgList) { byte[] result = null; try { BufferedImage orgImage = ImageIO.read(is); BufferedImage shrinkImage = FileuploadUtils.shrinkImage(orgImage, width, height); Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("png"); ImageWriter writer = writers.next(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(out); writer.setOutput(ios); writer.write(shrinkImage); result = out.toByteArray(); } catch (Exception e) { logger.error("fileupload", e); result = null; } finally { try { if (is != null) { is.close(); } } catch (Exception e) { logger.error("fileupload", e); result = null; } } return result; }
From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java
/** * ???????/*from ww w . j av a 2s .c o m*/ * * @param org_id * @param folderName * @param uid * @param fileBean * @param acceptExts * @param msgList * @return */ public static ShrinkImageSet getBytesShrinkFilebean(String org_id, String folderName, int uid, FileuploadLiteBean fileBean, String[] acceptExts, int width, int height, List<String> msgList, boolean isFixOrgImage) { byte[] result = null; byte[] fixResult = null; InputStream is = null; boolean fixed = false; try { String file_name = fileBean.getFileName(); String ext = ""; if (acceptExts != null && acceptExts.length > 0) { // ??? // ???? boolean isAccept = false; String tmpExt = null; int len = acceptExts.length; for (int i = 0; i < len; i++) { if (!acceptExts[i].startsWith(".")) { tmpExt = "." + acceptExts[i]; } if (file_name.toLowerCase().endsWith(tmpExt)) { isAccept = true; ext = tmpExt.replace(".", ""); ; break; } } if (!isAccept) { // ???????null ? return null; } } is = ALStorageService.getFile(FOLDER_TMP_FOR_ATTACHMENT_FILES, uid + ALStorageService.separator() + folderName, String.valueOf(fileBean.getFileId())); byte[] imageInBytes = IOUtils.toByteArray(is); ImageInformation readImageInformation = readImageInformation(new ByteArrayInputStream(imageInBytes)); BufferedImage bufferdImage = ImageIO.read(new ByteArrayInputStream(imageInBytes)); if (readImageInformation != null) { bufferdImage = transformImage(bufferdImage, getExifTransformation(readImageInformation), readImageInformation.orientation >= 5 ? bufferdImage.getHeight() : bufferdImage.getWidth(), readImageInformation.orientation >= 5 ? bufferdImage.getWidth() : bufferdImage.getHeight()); fixed = isFixOrgImage; } if (bufferdImage == null) { // ?bufferdImage???????????,null?. return null; } BufferedImage shrinkImage = FileuploadUtils.shrinkAndTrimImage(bufferdImage, width, height); Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("jpeg"); ImageWriter writer = writers.next(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(out); writer.setOutput(ios); writer.write(shrinkImage); result = out.toByteArray(); if (fixed) { Iterator<ImageWriter> writers2 = ImageIO.getImageWritersBySuffix(ext); ImageWriter writer2 = writers2.next(); ByteArrayOutputStream out2 = new ByteArrayOutputStream(); ImageOutputStream ios2 = ImageIO.createImageOutputStream(out2); writer2.setOutput(ios2); writer2.write(bufferdImage); fixResult = out2.toByteArray(); } } catch (Exception e) { logger.error("fileupload", e); result = null; } finally { try { if (is != null) { is.close(); } } catch (Exception e) { logger.error("fileupload", e); result = null; } } return new ShrinkImageSet(result, fixed ? fixResult : null); }
From source file:com.t3.net.LocalLocation.java
@Override public void putContent(ImageWriter writer, BufferedImage image) { try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(getFile()))) { writer.setOutput(out);/*from w ww . j a va 2 s .c o m*/ writer.write(image); } catch (IOException e) { e.printStackTrace(); } }