List of usage examples for javax.imageio ImageIO createImageOutputStream
public static ImageOutputStream createImageOutputStream(Object output) throws IOException
From source file:it.geosolutions.imageio.plugins.nitronitf.ImageIOUtils.java
/** * Returns an ImageWriter given the input file * /*from w ww . j ava2 s . c om*/ * @param file * @return * @throws IOException */ public static ImageWriter getImageWriter(File file) throws IOException { String ext = FilenameUtils.getExtension(file.getName().toLowerCase()); ImageWriter writer = null; Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersBySuffix(ext); if (imageWriters.hasNext()) { writer = imageWriters.next(); ImageOutputStream stream = ImageIO.createImageOutputStream(file); writer.setOutput(stream); } return writer; }
From source file:com.occamlab.te.parsers.ImageParser.java
private static String getBase64Data(BufferedImage buffImage, String formatName, Node node) throws Exception { int numBytes = buffImage.getWidth() * buffImage.getHeight() * 4; ByteArrayOutputStream baos = new ByteArrayOutputStream(numBytes); ImageOutputStream ios = ImageIO.createImageOutputStream(baos); boolean status = ImageIO.write(buffImage, formatName, ios); if (!status) { throw new Exception("No suitable ImageIO writer found by ImageParser.getBase64Data() for image format " + formatName);/*from www .ja v a 2 s . c o m*/ } byte[] imageData = baos.toByteArray(); // String base64String = Base64.encodeBase64String(imageData); this is // unchunked (no line breaks) which is unwieldy byte[] base64Data = Base64.encodeBase64Chunked(imageData); // 2011-11-15 PwD uncomment for Java 1.7 String base64String = new // String(base64Data, StandardCharsets.UTF_8); String base64String = new String(base64Data, "UTF-8"); // 2011-11-15 PwD // for Java 1.6; // remove for // Java 1.7 return base64String; }
From source file:nl.mvdr.umvc3replayanalyser.controller.ReplaySaver.java
/** * Constructs a replay for the given video file with the data from the given game, saves it to disk and returns it. * // ww w. j av a 2s . c om * @param file * original video file * @param game * game * @param previewImage * preview image * @param logger * logger used to log informational messages that may be of interest to the user * @return the new replay * @throws IOException * in case saving the replay fails */ Replay saveReplay(File file, Game game, BufferedImage previewImage, MessageLogger logger) throws IOException { Date creationTime = new Date(file.lastModified()); String baseFilename = game.getBaseFilename(creationTime); // Save the preview image. File previewImageFile; if (this.configuration.isSavePreviewImageToDataDirectory()) { // Create preview image file in the data directory. previewImageFile = new File( configuration.getDataDirectoryPath() + FileUtils.SEPARATOR + baseFilename + "." + IMAGE_FORMAT); if (previewImageFile.exists()) { throw new IOException("Preview image file already exists: " + previewImageFile); } } else { // Save the preview image as a temporary file. previewImageFile = File.createTempFile("preview-" + baseFilename + "-", "." + IMAGE_FORMAT); previewImageFile.deleteOnExit(); } try (ImageOutputStream stream = ImageIO.createImageOutputStream(previewImageFile)) { if (stream == null) { throw new IOException("Unable to save preview image. Image stream for path " + previewImageFile + " could not be created."); } ImageIO.write(previewImage, IMAGE_FORMAT, stream); logger.log("Saved preview image: " + previewImageFile); } return saveReplay(file, game, FILE_URL_PREFIX + previewImageFile.getAbsolutePath(), logger); }
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 o m 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:com.abelsky.idea.geekandpoke.entries.Entry.java
private void writeObject(@NotNull ObjectOutputStream out) throws IOException { out.defaultWriteObject();/*from w w w. j ava 2 s . c o m*/ if (imageRef != null) { @Nullable BufferedImage image = imageRef.get(); if (image == null) { image = fetchCachedImage(); imageRef = new SoftReference<BufferedImage>(image); } ImageIO.write(image, "png", ImageIO.createImageOutputStream(out)); } }
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 * */// ww w. ja v a 2 s .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:eu.europa.esig.dss.pades.signature.visible.ImageFactory.java
private static InputStream convertToInputStream(BufferedImage buffImage, int dpi) throws IOException { Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("jpeg"); if (!it.hasNext()) { throw new DSSException("No writer for JPEG found"); }//from w w w . ja va 2s . c o m ImageWriter writer = it.next(); JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) writer.getDefaultWriteParam(); jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT); jpegParams.setCompressionQuality(1); ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier .createFromBufferedImageType(BufferedImage.TYPE_INT_RGB); IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, jpegParams); initDpi(metadata, dpi); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageOutputStream imageOs = ImageIO.createImageOutputStream(os); writer.setOutput(imageOs); writer.write(metadata, new IIOImage(buffImage, null, metadata), jpegParams); InputStream is = new ByteArrayInputStream(os.toByteArray()); return is; }
From source file:compressor.Compressor.java
void compress_images(String src, String dest) throws IOException { File f = null;//from w ww. j ava 2 s . co m String[] paths; try { // create new file f = new File(src); // array of files and directory paths = f.list(); File file = new File(dest + "compressed"); if (!file.exists()) { if (file.mkdir()) { System.out.println("Directory is created!"); } else { System.out.println("Failed to create directory!"); } } dest = dest + "compressed/"; // for each name in the path array for (String path : paths) { // prints filename and directory name File input = new File(src + path); BufferedImage image = ImageIO.read(input); File compressedImageFile = new File(dest + path); OutputStream os = new FileOutputStream(compressedImageFile); Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); ImageWriter writer = (ImageWriter) writers.next(); ImageOutputStream ios = ImageIO.createImageOutputStream(os); writer.setOutput(ios); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(0.05f); writer.write(null, new IIOImage(image, null, null), param); os.close(); ios.close(); writer.dispose(); } } catch (Exception e) { } }
From source file:ImageUtils.java
/** * Compress and save an image to the disk. Currently this method only supports JPEG images. * //from w ww . j a va 2s . co m * @param image The image to save * @param toFileName The filename to use * @param type The image type. Use <code>ImageUtils.IMAGE_JPEG</code> to save as JPEG images, * or <code>ImageUtils.IMAGE_PNG</code> to save as PNG. */ public static void saveCompressedImage(BufferedImage image, String toFileName, int type) { try { if (type == IMAGE_PNG) { throw new UnsupportedOperationException("PNG compression not implemented"); } Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); ImageWriter writer; writer = (ImageWriter) iter.next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File(toFileName)); writer.setOutput(ios); ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault()); iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwparam.setCompressionQuality(0.7F); writer.write(null, new IIOImage(image, null, null), iwparam); ios.flush(); writer.dispose(); ios.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:tvbrowserdataservice.file.ProgramField.java
/** * This Function loads an image using imageio, * resizes it to the Max-Size of Images in TVBrowser and * stores it as an compressed jpg./*from w ww .ja v a 2 s . c o m*/ * <p/> * If the Image is smaller than the Max-Size, it isn't altered * * @param data Image-Data * @return resized Image-Data */ private static byte[] recreateImage(byte[] data) { byte[] newdata = null; BufferedImage image = null; try { // Read Image image = ImageIO.read(new ByteArrayInputStream(data)); int curx = image.getWidth(null); int cury = image.getHeight(null); // If the Size is < than max, use the original Data to reduce compression // artefacts if ((curx <= MAX_IMAGE_SIZE_X) && (cury <= MAX_IMAGE_SIZE_Y)) { return data; } int newx = MAX_IMAGE_SIZE_X; int newy = (int) ((MAX_IMAGE_SIZE_X / (float) curx) * cury); if (newy > MAX_IMAGE_SIZE_Y) { newy = MAX_IMAGE_SIZE_Y; newx = (int) ((MAX_IMAGE_SIZE_Y / (float) cury) * curx); } BufferedImage tempPic = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2d = tempPic.createGraphics(); g2d.drawImage(image, null, null); g2d.dispose(); BufferedImage newImage = UiUtilities.scaleIconToBufferedImage(tempPic, newx, newy); ByteArrayOutputStream out = new ByteArrayOutputStream(); // Find a jpeg writer ImageWriter writer = null; Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg"); if (iter.hasNext()) { writer = iter.next(); } if (writer != null) { // Prepare output file ImageOutputStream ios = ImageIO.createImageOutputStream(out); writer.setOutput(ios); JPEGImageWriteParam param = new JPEGImageWriteParam(null); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(0.85f); // Write the image writer.write(null, new IIOImage(newImage, null, null), param); // Cleanup ios.flush(); writer.dispose(); ios.close(); newdata = out.toByteArray(); } else { mLog.severe("No JPEG-Exporter found. Image is not stored in Data"); } } catch (IOException e) { e.printStackTrace(); newdata = null; } return newdata; }