public static void saveImage(final String fileName, final BufferedImage image, ImageFormat imageFormat) { try { final File file = new File(fileName); final String extension = FileUtilities.getExtension(fileName); Iterator<ImageWriter> iter = null; if (canWriteFormat(extension)) { iter = ImageIO.getImageWritersByFormatName(extension); } else { iter = ImageIO.getImageWritersByFormatName(imageFormat.toString()); } final ImageWriter writer = iter.next(); final ImageWriteParam iwp = writer.getDefaultWriteParam(); file.getParentFile().mkdirs(); try (final FileImageOutputStream output = new FileImageOutputStream(file.getAbsoluteFile())) { writer.setOutput(output); final IIOImage outimage = new IIOImage(image, null, null); writer.write(null, outimage, iwp); writer.dispose(); } } catch (final IOException e) { log.log(Level.SEVERE, e.getMessage(), e); } }
public static void writeJPG( BufferedImage rendered, float quality, File f ) { try { JPEGImageWriteParam jpegParams = new JPEGImageWriteParam( null ); jpegParams.setCompressionMode( ImageWriteParam.MODE_EXPLICIT ); jpegParams.setCompressionQuality( 1f ); ImageWriter writer = ImageIO.getImageWritersByFormatName( "jpeg" ).next(); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode( ImageWriteParam.MODE_EXPLICIT ); // Needed see javadoc param.setCompressionQuality( quality ); // Highest quality writer.setOutput( new FileImageOutputStream( f ) ); writer.write( null, new IIOImage( rendered, null, null ), jpegParams ); } catch ( Throwable th ) { th.printStackTrace(); } }
private void saveGraphicJpeg(BufferedImage chart, File outputFile, float quality) throws IOException { // Setup correct compression for jpeg. Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = (ImageWriter) iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(quality); // Output the images. FileImageOutputStream output = new FileImageOutputStream(outputFile); writer.setOutput(output); IIOImage image = new IIOImage(chart, null, null); writer.write(null, image, iwp); writer.dispose(); }
public static void main(String[] args) { try { File f = new File("ImageInputStreamFromRAF.tmp"); RandomAccessFile raf = new RandomAccessFile(f, "rw"); ImageInputStream istream = ImageIO.createImageInputStream(raf); ImageOutputStream ostream = ImageIO.createImageOutputStream(raf); f.delete(); if (istream == null) { throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) returned null!"); } if (ostream == null) { throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) returned null!"); } if (!(istream instanceof FileImageInputStream)) { throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) did not return a FileImageInputStream!"); } if (!(ostream instanceof FileImageOutputStream)) { throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) did not return a FileImageOutputStream!"); } } catch (IOException ioe) { throw new RuntimeException("Unexpected IOException: " + ioe); } }
@SideOnly(Side.CLIENT) public static void writeImgToFile(BufferedImage img, String name) { if (GalacticraftCore.enableJPEG) { File folder = new File(FMLClientHandler.instance().getClient().mcDataDir, "assets/temp"); try { ImageOutputStream outputStreamA = new FileImageOutputStream(new File(folder, name)); GalacticraftCore.jpgWriter.setOutput(outputStreamA); GalacticraftCore.jpgWriter.write(null, new IIOImage(img, null, null), GalacticraftCore.writeParam); outputStreamA.close(); } catch (Exception e) { } } }
public static void downloadImage(String imgURL, InputStream postInStr) throws IOException { BufferedImage image = ImageIO.read(postInStr); String[] URLparts = imgURL.split("/"); File outputFile = new File(saveFolderName + "/" + URLparts[URLparts.length - 1]); JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null); jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); jpegParams.setCompressionQuality(1f); final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next(); writer.setOutput(new FileImageOutputStream(outputFile)); writer.write(null, new IIOImage(image, null, null), jpegParams); System.out.println("Got " + outputFile + " from " + imgURL); }
public static void main(String[] args) { JFrame frame = new JFrame(); BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); frame.getContentPane().add(new JLabel(new ImageIcon(image))); frame.setSize(image.getHeight() + frame.getSize().height, image.getWidth() + frame.getSize().width); frame.pack(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true); try { ImageOutputStream output = new FileImageOutputStream(new File("anim.gif")); GifSequenceWriter writer = new GifSequenceWriter(output, image.getType(), (int)(1000/FPS), true); for (int i = 0; i < FPS*GIF_LENGTH; i++) { Scene scene = constructScene(i/(FPS*GIF_LENGTH)); scene.render(image, frame); writer.writeToSequence(image); } writer.close(); output.close(); } catch (IOException e) { System.out.println(e.getMessage()); } File outFile = new File("image.png"); }
private void writeCompressedJPG( BufferedImage image, File file, float quality ) throws IOException { Iterator iter = ImageIO.getImageWritersByFormatName( "jpeg" ); ImageWriter writer = (ImageWriter) iter.next(); // instantiate an ImageWriteParam object with default compression options ImageWriteParam iwp = writer.getDefaultWriteParam(); //Now, we can set the compression quality: iwp.setCompressionMode( ImageWriteParam.MODE_EXPLICIT ); iwp.setCompressionQuality( quality ); // an integer between 0 and 1 // 1 specifies minimum compression and maximum quality //Output the file: FileImageOutputStream output = new FileImageOutputStream( file ); writer.setOutput( output ); IIOImage a = new IIOImage( image, null, null ); writer.write( null, a, iwp ); }
/** * Saves the given <code>image</code>. */ private void savePhoto(BufferedImage image, File file) throws IOException { String fileFormat = this.controller.getFileFormat(); Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(fileFormat); ImageWriter writer = (ImageWriter) iter.next(); ImageWriteParam writeParam = writer.getDefaultWriteParam(); if ("JPEG".equals(fileFormat)) { writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writeParam.setCompressionQuality(this.controller.getFileCompressionQuality()); } FileImageOutputStream output = new FileImageOutputStream(file); writer.setOutput(output); writer.write(null, new IIOImage(image, null, null), writeParam); writer.dispose(); output.close(); }
private void saveGraphicJpeg(BufferedImage chart, File outputFile, float quality) throws IOException { // Setup correct compression for jpeg. Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(quality); // Output the image. FileImageOutputStream output = new FileImageOutputStream(outputFile); writer.setOutput(output); IIOImage image = new IIOImage(chart, null, null); writer.write(null, image, iwp); writer.dispose(); }
private void saveGraphicJpeg(BufferedImage chart, File outputFile, float quality) throws IOException { // Setup correct compression for jpeg. Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = (ImageWriter) iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(quality); // Output the image. FileImageOutputStream output = new FileImageOutputStream(outputFile); writer.setOutput(output); IIOImage image = new IIOImage(chart, null, null); writer.write(null, image, iwp); writer.dispose(); }
@Override public void run() { // make image wr.setPixels(0, 0, WIDTH, HEIGHT, pixList); bufImage.setData(wr); outputImage = new IIOImage(bufImage, null, null); // make filename with date SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss"); String dateString = sdf.format(new Date()); // file to write File imageFile = new File(filePath+dateString+".jpg"); //write jpeg image file try { jpgWriter.setOutput(new FileImageOutputStream( imageFile )); jpgWriter.write(null, outputImage, jpgWriteParam); } catch (IOException e) { e.printStackTrace(); } }
private static void convertImages( final File file ) throws Exception { if ( file.isDirectory() ) { for ( final File childFile : file.listFiles() ) convertImages( childFile ); } else { final BufferedImage bi = ImageIO.read( file ); // Default quality is not sufficient! // ImageIO.write( bi, "JPG", new File( file.getPath().replace( ".png", ".jpg" ) ) ); final ImageWriter iw = ImageIO.getImageWritersByFormatName( "jpg" ).next(); final ImageWriteParam iwp = iw.getDefaultWriteParam(); iwp.setCompressionMode( ImageWriteParam.MODE_EXPLICIT ); iwp.setCompressionQuality( 0.9f ); iw.setOutput( new FileImageOutputStream( new File( file.getPath().replace( ".png", ".jpg" ) ) ) ); iw.write( null, new IIOImage( bi , null, null ), iwp ); iw.dispose(); } }
@Override public void run() { // make image wr.setPixels(0, 0, WIDTH, HEIGHT, getFloydDitheredMonochrom()); //dithered image // wr.setPixels(0, 0, WIDTH, HEIGHT, pixList); // grayscale image // wr.setPixels(0, 0, WIDTH, HEIGHT, getJarvisDitheredMonochom()); //dithered image // wr.setPixels(0, 0, WIDTH, HEIGHT, pixContrasted); // grayscale image bufImage.setData(wr); outputImage = new IIOImage(bufImage, null, null); // make filename with date SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss"); String dateString = sdf.format(new Date()); // file to write File imageFile = new File(filePath+dateString+".jpg"); //write jpeg image file try { imageWriter.setOutput(new FileImageOutputStream( imageFile )); imageWriter.write(null, outputImage, imageWriteParam); } catch (IOException e) { e.printStackTrace(); } }
/** * Can change this to choose a better compression level as the default * * @param image * @param scaledImage * @return */ private static boolean writeTo(BufferedImage image, File scaledImage, Size scaledSize, String outputFormat) { try { if (!StringHelper.containsNonWhitespace(outputFormat)) { outputFormat = OUTPUT_FORMAT; } Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat); if (writers.hasNext()) { ImageWriter writer = writers.next(); ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize); IIOImage iiOImage = new IIOImage(image, null, null); ImageOutputStream iOut = new FileImageOutputStream(scaledImage); writer.setOutput(iOut); writer.write(null, iiOImage, iwp); writer.dispose(); iOut.flush(); iOut.close(); return true; } else { return ImageIO.write(image, outputFormat, scaledImage); } } catch (IOException e) { return false; } }
public void write(File f, BitmapImage img, int camg) throws IOException { IFFOutputStream out = null; try { out = new IFFOutputStream(new FileImageOutputStream(f)); out.pushCompositeChunk("FORM", "ILBM"); writeBMHD(out, img); writeCMAP(out, img); writeCAMG(out, camg); writeBODY(out, img); out.popChunk(); } finally { if (out != null) { out.close(); } } }
public void write(File file) throws IOException { ImageWriter writer = getWriter(); IIOImage[] images = getImageSequence(writer); file.delete(); ImageOutputStream out = new FileImageOutputStream(file); try { writer.setOutput(out); writer.prepareWriteSequence(null); for (IIOImage img : images) writer.writeToSequence(img, null); writer.endWriteSequence(); } finally { out.close(); } }
public void createWriter(File file, int time, boolean loop, Rectangle bounds, boolean lc) throws IOException, AWTException { lowcolor = lc; writer = new GifSequenceWriter(new FileImageOutputStream(file), BufferedImage.TYPE_INT_ARGB, time, loop); writer.imageWriteParam .setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writer.imageWriteParam.setCompressionType("LZW"); this.bounds = bounds; this.x = bounds.x; this.y = bounds.y; this.w = bounds.width; this.h = bounds.height; this.time = time; rt = new Robot(); WriterThread.setup(writer, lowcolor); }
public static void makeGIFOld(Iterator<BufferedImage> images, float frameRate, File file, EventListener evl) throws IOException { if (!images.hasNext()) { return; } try (ImageOutputStream output = new FileImageOutputStream(file)) { BufferedImage img0 = images.next(); GifSequenceWriter writer = new GifSequenceWriter(output, img0.getType(), (int) (1000.0 / frameRate), true); writer.writeToSequence(img0); while (images.hasNext()) { writer.writeToSequence(images.next()); } writer.close(); } }
/** * writeLeftAlignedValue() writes a given value of the given type into * a given stream as a left-justified four-byte record. * See Section 2 (page 15) of the TIFF spec for details. * * @param stream the stream representing the file being written. * @param type the type of the value. * @param val the value to write. * @throws IOException */ private static void writeLeftAlignedValue(FileImageOutputStream stream, TiffType type, int val) throws IOException { switch (type) { case SHORT: stream.writeShort(val); stream.writeShort(0); break; case LONG: stream.writeInt(val); break; default: // There are other possible types, but we're not using them. throw new IllegalArgumentException(); } }
/** * Write a BufferedImage to a temporary file. */ private String convert2TmpFile(BufferedImage pBufferedImage) throws IOException { String tmpFile = getTmpFile(); ImageTypeSpecifier spec = ImageTypeSpecifier.createFromRenderedImage(pBufferedImage); Iterator<ImageWriter> iter; iter = ImageIO.getImageWriters(spec,"TIFF"); if (!iter.hasNext()) { iter = ImageIO.getImageWriters(spec,"PNG"); } if (iter.hasNext()) { ImageWriter writer = (ImageWriter) iter.next(); FileImageOutputStream fios = new FileImageOutputStream(new File(tmpFile)); writer.setOutput(fios); writer.write(pBufferedImage); fios.close(); writer.dispose(); } return tmpFile; }
private boolean writeInImageOutput(FileImageOutputStream fileIOS, BufferedImage bi, boolean andCloseIt){ try{ //Appends new graphics to the output ImageIO.write(bi, "PNG", fileIOS); if(andCloseIt==false){ fileIOS.flush(); }else{ fileIOS.close(); } }catch (java.io.IOException ioe){ System.out.println("PNG Aborted"); return false; } return true; }
public static String convertImageTo(String filePath, String format) throws IOException, TaskIOException { BufferedImage image = ImageIO.read(new File(filePath)); File tmpFile = IOUtils.createTemporaryBuffer("." + format); ImageOutputStream outputStream = new FileImageOutputStream(tmpFile); try { ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next(); writer.setOutput(outputStream); ImageWriteParam param = writer.getDefaultWriteParam(); if (format.equals("jpeg")) { param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(1.0F); } writer.write(null, new IIOImage(image, null, null), param); } finally { org.apache.commons.io.IOUtils.closeQuietly(outputStream); } return tmpFile.getPath(); }
private void writeJPEG(BufferedImage input, ImageResizeRequest imageResizeRequest) throws IOException { Iterator iter = ImageIO.getImageWritersByFormatName("JPG"); if (iter.hasNext()) { ImageWriter writer = (ImageWriter) iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(imageResizeRequest.getCompressionQuality()); File outFile = new File(imageResizeRequest.getDestinationFilePath()); FileImageOutputStream output = new FileImageOutputStream(outFile); writer.setOutput(output); IIOImage image = new IIOImage(input, null, null); writer.write(null, image, iwp); output.close(); } }
public void writeImageToFile(BufferedImage input, String fileName) throws IOException { logger.debug("writeImageToFile " + fileName); Iterator iter = ImageIO.getImageWritersByFormatName("JPG"); if (iter.hasNext()) { ImageWriter writer = (ImageWriter) iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(0.95f); File outFile = new File(fileName); FileImageOutputStream output = new FileImageOutputStream(outFile); writer.setOutput(output); IIOImage image = new IIOImage(input, null, null); writer.write(null, image, iwp); output.close(); } }
private void writeImage(File outputFile, BufferedImage img) throws FileNotFoundException, IOException { Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter iw = it.next(); ImageWriteParam iwp = iw.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(1); FileImageOutputStream outs = new FileImageOutputStream(outputFile); iw.setOutput(outs); IIOImage ioimage = new IIOImage(img, null, null); iw.write(null, ioimage, iwp); iw.dispose(); }
public ImageOutputStream createOutputStreamInstance(Object output, boolean useCache, File cacheDir) { if (output instanceof File) { try { return new FileImageOutputStream((File)output); } catch (Exception e) { e.printStackTrace(); return null; } } else { throw new IllegalArgumentException(); } }
public ImageOutputStream createOutputStreamInstance(Object output, boolean useCache, File cacheDir) { if (output instanceof RandomAccessFile) { try { return new FileImageOutputStream((RandomAccessFile)output); } catch (Exception e) { e.printStackTrace(); return null; } } else { throw new IllegalArgumentException ("input not a RandomAccessFile!"); } }
private static boolean compressImageFile(ImageWriter jpgWriter, ImageWriteParam jpgWriteParam, File inputFile, File outputFile) throws IOException { BufferedImage image = null; try { /* Para evitar problemas na leitura de imagens JPEG com modelo de cor CMYK um leitor terceiro é utilizado como extensão do ImageIO, Por isso precisamos da dependência: com.twelvemonkeys.imageio:imageio-jpeg. A extensão é instalada automaticamente, basta estar no classpath. */ image = ImageIO.read(inputFile); } catch (Exception ex) { System.out.println("Falha ao ler arquivo com ImageIO."); ex.printStackTrace(); } IIOImage outputImage = new IIOImage(image, null, null); if (!outputFile.exists()) { if (!outputFile.getParentFile().exists() && !outputFile.getParentFile().mkdirs()) { System.out.println("Não foi possível criar os diretórios intermediários para salvar o arquivo no caminho: " + outputFile.getAbsolutePath()); return false; } if (!outputFile.createNewFile()) { System.out.println("Não foi possível criar o arquivo: " + outputFile.getAbsolutePath()); return false; } } ImageOutputStream outputStream = new FileImageOutputStream(outputFile); jpgWriter.setOutput(outputStream); jpgWriter.write(null, outputImage, jpgWriteParam); return true; }