javax.imageio.IIOImage Java Examples

The following examples show how to use javax.imageio.IIOImage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GIFImageWriter.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public void writeToSequence(IIOImage image, ImageWriteParam param)
  throws IOException {
    if (stream == null) {
        throw new IllegalStateException("output == null!");
    }
    if (image == null) {
        throw new IllegalArgumentException("image == null!");
    }
    if (image.hasRaster()) {
        throw new UnsupportedOperationException("canWriteRasters() == false!");
    }
    if (!isWritingSequence) {
        throw new IllegalStateException("prepareWriteSequence() was not invoked!");
    }

    write(!wroteSequenceHeader, false, theStreamMetadata,
          image, param);

    if (!wroteSequenceHeader) {
        wroteSequenceHeader = true;
    }

    this.imageIndex++;
}
 
Example #2
Source File: GIFImageWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void write(IIOMetadata sm,
                  IIOImage iioimage,
                  ImageWriteParam p) throws IOException {
    if (stream == null) {
        throw new IllegalStateException("output == null!");
    }
    if (iioimage == null) {
        throw new IllegalArgumentException("iioimage == null!");
    }
    if (iioimage.hasRaster()) {
        throw new UnsupportedOperationException("canWriteRasters() == false!");
    }

    resetLocal();

    GIFWritableStreamMetadata streamMetadata;
    if (sm == null) {
        streamMetadata =
            (GIFWritableStreamMetadata)getDefaultStreamMetadata(p);
    } else {
        streamMetadata =
            (GIFWritableStreamMetadata)convertStreamMetadata(sm, p);
    }

    write(true, true, streamMetadata, iioimage, p);
}
 
Example #3
Source File: LUTCompareTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static Image createTestImage() throws IOException  {
    BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
    BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });

    ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
    ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
    ImageWriteParam param = writer.getDefaultWriteParam();
    writer.setOutput(ios);
    writer.prepareWriteSequence(null);
    writer.writeToSequence(new IIOImage(frame1, null, null), param);
    writer.writeToSequence(new IIOImage(frame2, null, null), param);
    writer.endWriteSequence();
    writer.reset();
    writer.dispose();

    ios.flush();
    ios.close();

    return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
}
 
Example #4
Source File: CommUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static byte[] imageToBytes(BufferedImage img) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    MemoryCacheImageOutputStream outputStream = new MemoryCacheImageOutputStream(byteArrayOutputStream);
    try {
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(IMAGE_QUALITY);
        writer.setOutput(outputStream);
        IIOImage image = new IIOImage(img, null, null);
        writer.write(null, image, iwp);
        writer.dispose();
        byte[] result = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        outputStream.close();
        return result;
    } catch (IOException e) {
        StaticLog.error(e);
        return new byte[0];
    }
}
 
Example #5
Source File: GIFImageWriter.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
public void write(IIOMetadata sm,
                  IIOImage iioimage,
                  ImageWriteParam p) throws IOException {
    if (stream == null) {
        throw new IllegalStateException("output == null!");
    }
    if (iioimage == null) {
        throw new IllegalArgumentException("iioimage == null!");
    }
    if (iioimage.hasRaster()) {
        throw new UnsupportedOperationException("canWriteRasters() == false!");
    }

    resetLocal();

    GIFWritableStreamMetadata streamMetadata;
    if (sm == null) {
        streamMetadata =
            (GIFWritableStreamMetadata)getDefaultStreamMetadata(p);
    } else {
        streamMetadata =
            (GIFWritableStreamMetadata)convertStreamMetadata(sm, p);
    }

    write(true, true, streamMetadata, iioimage, p);
}
 
Example #6
Source File: GIFImageWriter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public void write(IIOMetadata sm,
                  IIOImage iioimage,
                  ImageWriteParam p) throws IOException {
    if (stream == null) {
        throw new IllegalStateException("output == null!");
    }
    if (iioimage == null) {
        throw new IllegalArgumentException("iioimage == null!");
    }
    if (iioimage.hasRaster()) {
        throw new UnsupportedOperationException("canWriteRasters() == false!");
    }

    resetLocal();

    GIFWritableStreamMetadata streamMetadata;
    if (sm == null) {
        streamMetadata =
            (GIFWritableStreamMetadata)getDefaultStreamMetadata(p);
    } else {
        streamMetadata =
            (GIFWritableStreamMetadata)convertStreamMetadata(sm, p);
    }

    write(true, true, streamMetadata, iioimage, p);
}
 
Example #7
Source File: LUTCompareTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static Image createTestImage() throws IOException  {
    BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
    BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });

    ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
    ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
    ImageWriteParam param = writer.getDefaultWriteParam();
    writer.setOutput(ios);
    writer.prepareWriteSequence(null);
    writer.writeToSequence(new IIOImage(frame1, null, null), param);
    writer.writeToSequence(new IIOImage(frame2, null, null), param);
    writer.endWriteSequence();
    writer.reset();
    writer.dispose();

    ios.flush();
    ios.close();

    return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
}
 
Example #8
Source File: ImageHelperService.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
public byte[] createPNGThumbnailFromByteArray(byte[] img) throws Exception {
	ByteArrayInputStream byteIn = new ByteArrayInputStream(img);
	ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
	try {
		BufferedImage buffImg = ImageIO.read(byteIn);
		BufferedImage thumbImg = createThumbnail(buffImg);
		try (ImageOutputStream ios = ImageIO.createImageOutputStream(byteOut)) {
			ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next();
			writer.setOutput(ios);
			writer.write(null, new IIOImage(thumbImg, null, null), writer.getDefaultWriteParam());
			ios.flush();
			writer.dispose();
		}
	} catch (IOException e) {
		logger.error("Error reading image from byte array", e);
	}
	return byteOut.toByteArray();
}
 
Example #9
Source File: ImageGifFile.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static void writeGifImageFile(BufferedImage image,
        ImageAttributes attributes, String outFile) {
    try {
        ImageWriter writer = getWriter();
        ImageWriteParam param = getPara(attributes, writer);
        IIOMetadata metaData = getWriterMeta(attributes, image, writer, param);

        try ( ImageOutputStream out = ImageIO.createImageOutputStream(new File(outFile))) {
            writer.setOutput(out);
            writer.write(metaData, new IIOImage(image, null, metaData), param);
            out.flush();
        }
        writer.dispose();

    } catch (Exception e) {
        logger.error(e.toString());
    }
}
 
Example #10
Source File: GIFImageWriter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void writeToSequence(IIOImage image, ImageWriteParam param)
  throws IOException {
    if (stream == null) {
        throw new IllegalStateException("output == null!");
    }
    if (image == null) {
        throw new IllegalArgumentException("image == null!");
    }
    if (image.hasRaster()) {
        throw new UnsupportedOperationException("canWriteRasters() == false!");
    }
    if (!isWritingSequence) {
        throw new IllegalStateException("prepareWriteSequence() was not invoked!");
    }

    write(!wroteSequenceHeader, false, theStreamMetadata,
          image, param);

    if (!wroteSequenceHeader) {
        wroteSequenceHeader = true;
    }

    this.imageIndex++;
}
 
Example #11
Source File: LUTCompareTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static Image createTestImage() throws IOException  {
    BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
    BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });

    ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
    ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
    ImageWriteParam param = writer.getDefaultWriteParam();
    writer.setOutput(ios);
    writer.prepareWriteSequence(null);
    writer.writeToSequence(new IIOImage(frame1, null, null), param);
    writer.writeToSequence(new IIOImage(frame2, null, null), param);
    writer.endWriteSequence();
    writer.reset();
    writer.dispose();

    ios.flush();
    ios.close();

    return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
}
 
Example #12
Source File: JmeDesktopSystem.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void writeImageFile(OutputStream outStream, String format, ByteBuffer imageData, int width, int height) throws IOException {
    BufferedImage awtImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
    Screenshots.convertScreenShot2(imageData.asIntBuffer(), awtImage);

    ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
    ImageWriteParam writeParam = writer.getDefaultWriteParam();

    if (format.equals("jpg")) {
        JPEGImageWriteParam jpegParam = (JPEGImageWriteParam) writeParam;
        jpegParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        jpegParam.setCompressionQuality(0.95f);
    }

    awtImage = verticalFlip(awtImage);
    
    ImageOutputStream imgOut = new MemoryCacheImageOutputStream(outStream);
    writer.setOutput(imgOut);
    IIOImage outputImage = new IIOImage(awtImage, null, null);
    try {
        writer.write(null, outputImage, writeParam);
    } finally {
        imgOut.close();
        writer.dispose();
    }
}
 
Example #13
Source File: ImageHelper.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example #14
Source File: HeatChart.java    From JuiceboxLegacy with MIT License 6 votes vote down vote up
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();

}
 
Example #15
Source File: ResizePictureUtil.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
    * Reads the original image, creates a resized copy of it and returns its input stream. largestDimension is the
    * largest dimension of the resized image, the other dimension is scaled accordingly.
    *
    * @param image
    *            original image
    * @param largestDimension
    *            the largest dimension of the resized image, the other dimension is scaled accordingly
    * @return
    * @throws IOException
    * @throws UploadImageGalleryFileException
    */
   private static InputStream resize(BufferedImage image, int largestDimension) throws IOException {

//resize to 150 pixels max
BufferedImage outImage = Scalr.resize(image, Method.QUALITY, largestDimension);

//set jpeg compression quality explicitly
ImageWriter imageWriter = ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam jpgWriteParam = imageWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWriteParam.setCompressionQuality(.95f);

// buffer all data in a circular buffer of infinite sizes
CircularByteBuffer outputBuffer = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
ImageOutputStream ios = ImageIO.createImageOutputStream(outputBuffer.getOutputStream());
imageWriter.setOutput(ios);
IIOImage outputImage = new IIOImage(outImage, null, null);
imageWriter.write(null, outputImage, jpgWriteParam);
imageWriter.dispose();
outputBuffer.getOutputStream().close();

return outputBuffer.getInputStream();

   }
 
Example #16
Source File: ImageConverter.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
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();
	}
}
 
Example #17
Source File: LUTCompareTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static Image createTestImage() throws IOException  {
    BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
    BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });

    ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
    ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
    ImageWriteParam param = writer.getDefaultWriteParam();
    writer.setOutput(ios);
    writer.prepareWriteSequence(null);
    writer.writeToSequence(new IIOImage(frame1, null, null), param);
    writer.writeToSequence(new IIOImage(frame2, null, null), param);
    writer.endWriteSequence();
    writer.reset();
    writer.dispose();

    ios.flush();
    ios.close();

    return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
}
 
Example #18
Source File: GIFImageWriter.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void writeToSequence(IIOImage image, ImageWriteParam param)
  throws IOException {
    if (stream == null) {
        throw new IllegalStateException("output == null!");
    }
    if (image == null) {
        throw new IllegalArgumentException("image == null!");
    }
    if (image.hasRaster()) {
        throw new UnsupportedOperationException("canWriteRasters() == false!");
    }
    if (!isWritingSequence) {
        throw new IllegalStateException("prepareWriteSequence() was not invoked!");
    }

    write(!wroteSequenceHeader, false, theStreamMetadata,
          image, param);

    if (!wroteSequenceHeader) {
        wroteSequenceHeader = true;
    }

    this.imageIndex++;
}
 
Example #19
Source File: GifWriter.java    From scrimage with Apache License 2.0 6 votes vote down vote up
@Override
    public void write(AwtImage image, ImageMetadata metadata, OutputStream out) throws IOException {

        javax.imageio.ImageWriter writer = ImageIO.getImageWritersByFormatName("gif").next();
        ImageWriteParam params = writer.getDefaultWriteParam();

        if (progressive) {
            params.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);
        } else {
            params.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
        }

        try (MemoryCacheImageOutputStream output = new MemoryCacheImageOutputStream(out)) {
            writer.setOutput(output);
            writer.write(null, new IIOImage(image.awt(), null, null), params);
            writer.dispose();
        }

//        IOUtils.closeQuietly(out);
    }
 
Example #20
Source File: ITXtTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static void writeTo(File f, ITXtTest t) {
    BufferedImage src = createBufferedImage();
    try {
        ImageOutputStream imageOutputStream =
            ImageIO.createImageOutputStream(f);

        ImageTypeSpecifier imageTypeSpecifier =
            new ImageTypeSpecifier(src);
        ImageWriter imageWriter =
            ImageIO.getImageWritersByFormatName("PNG").next();

        imageWriter.setOutput(imageOutputStream);

        IIOMetadata m =
            imageWriter.getDefaultImageMetadata(imageTypeSpecifier, null);

        String format = m.getNativeMetadataFormatName();
        Node root = m.getAsTree(format);

        IIOMetadataNode iTXt = t.getNode();
        root.appendChild(iTXt);
        m.setFromTree(format, root);

        imageWriter.write(new IIOImage(src, null, m));
        imageOutputStream.close();
        System.out.println("Writing done.");
    } catch (Throwable e) {
        throw new RuntimeException("Writing test failed.", e);
    }
}
 
Example #21
Source File: JPEGImageWriter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void writeToSequence(IIOImage image, ImageWriteParam param)
    throws IOException {
    setThreadLock();
    try {
        cbLock.check();

        if (sequencePrepared == false) {
            throw new IllegalStateException("sequencePrepared not called!");
        }
        // In the case of JPEG this does nothing different from write
        write(null, image, param);
    } finally {
        clearThreadLock();
    }
}
 
Example #22
Source File: JPEGImageWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void write(IIOMetadata streamMetadata,
                  IIOImage image,
                  ImageWriteParam param) throws IOException {
    setThreadLock();
    try {
        cbLock.check();

        writeOnThread(streamMetadata, image, param);
    } finally {
        clearThreadLock();
    }
}
 
Example #23
Source File: JPEGImageWriter.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void writeToSequence(IIOImage image, ImageWriteParam param)
    throws IOException {
    setThreadLock();
    try {
        cbLock.check();

        if (sequencePrepared == false) {
            throw new IllegalStateException("sequencePrepared not called!");
        }
        // In the case of JPEG this does nothing different from write
        write(null, image, param);
    } finally {
        clearThreadLock();
    }
}
 
Example #24
Source File: TopDownTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void writeWithCompression(BufferedImage src,
                                         String compression) throws IOException
{
    System.out.println("Compression: " + compression);
    ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
    if (writer == null) {
        throw new RuntimeException("Test failed: no bmp writer available");
    }
    File fout = File.createTempFile(compression + "_", ".bmp",
                                    new File("."));

    ImageOutputStream ios = ImageIO.createImageOutputStream(fout);
    writer.setOutput(ios);

    BMPImageWriteParam param = (BMPImageWriteParam)
            writer.getDefaultWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionType(compression);
    param.setTopDown(true);
    writer.write(null, new IIOImage(src, null, null), param);
    writer.dispose();
    ios.flush();
    ios.close();

    BufferedImage dst = ImageIO.read(fout);

    verify(dst);
}
 
Example #25
Source File: EncodeSubImageTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void write(File f, boolean subsample) throws IOException {
    ImageOutputStream ios = ImageIO.createImageOutputStream(f);

    writer.setOutput(ios);
    ImageWriteParam p = writer.getDefaultWriteParam();
    if (subsample) {
        p.setSourceSubsampling(subSampleX, subSampleY, 0, 0);
    }
    writer.write(null, new IIOImage(img, null, null), p);
    ios.close();
    writer.reset();
}
 
Example #26
Source File: ImageIOHelper.java    From tess4j with Apache License 2.0 5 votes vote down vote up
/**
     * Gets a list of <code>IIOImage</code> objects for an image file.
     *
     * @param inputFile input image file. It can be any of the supported
     * formats, including TIFF, JPEG, GIF, PNG, BMP, JPEG, and PDF if GPL
     * Ghostscript or PDFBox is installed
     * @return a list of <code>IIOImage</code> objects
     * @throws IOException
     */
    public static List<IIOImage> getIIOImageList(File inputFile) throws IOException {
        // convert to TIFF if PDF
        File imageFile = getImageFile(inputFile);

        List<IIOImage> iioImageList = new ArrayList<IIOImage>();
        String imageFormat = getImageFileFormat(imageFile);

        Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFormat);
        if (!readers.hasNext()) {
            throw new RuntimeException(JAI_IMAGE_READER_MESSAGE);
        }
        ImageReader reader = readers.next();

        try (ImageInputStream iis = ImageIO.createImageInputStream(imageFile)) {
            reader.setInput(iis);

            int imageTotal = reader.getNumImages(true);

            for (int i = 0; i < imageTotal; i++) {
//                IIOImage oimage = new IIOImage(reader.read(i), null, reader.getImageMetadata(i));
                IIOImage oimage = reader.readAll(i, reader.getDefaultReadParam());
                iioImageList.add(oimage);
            }

            return iioImageList;
        } finally {
            if (reader != null) {
                reader.dispose();
            }

            // delete temporary TIFF image for PDF
            if (imageFile != null && imageFile.exists() && imageFile != inputFile && imageFile.getName().startsWith("multipage") && imageFile.getName().endsWith(TIFF_EXT)) {
                imageFile.delete();
            }
        }
    }
 
Example #27
Source File: JPEGImageWriter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void writeToSequence(IIOImage image, ImageWriteParam param)
    throws IOException {
    setThreadLock();
    try {
        cbLock.check();

        if (sequencePrepared == false) {
            throw new IllegalStateException("sequencePrepared not called!");
        }
        // In the case of JPEG this does nothing different from write
        write(null, image, param);
    } finally {
        clearThreadLock();
    }
}
 
Example #28
Source File: PNGCanvas.java    From SwissQRBill with MIT License 5 votes vote down vote up
/**
 * Saves image as PDF and stores meta data to indicate the resolution.
 */
private static void createPNG(BufferedImage image, OutputStream os, int resolution) throws IOException {

    ImageWriter writer = null;
    ImageWriteParam writeParam = null;
    IIOMetadata metadata = null;

    for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName("png"); iw.hasNext(); ) {
        writer = iw.next();
        writeParam = writer.getDefaultWriteParam();
        ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(image.getType());
        metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
        if (!metadata.isReadOnly() && metadata.isStandardMetadataFormatSupported())
            break;
    }

    if (writer == null || writeParam == null)
        throw new QRBillGenerationException("No valid PNG writer found");

    addDpiMetadata(metadata, resolution);
    addTextMetadata(metadata);

    try (ImageOutputStream stream = ImageIO.createImageOutputStream(os)) {
        writer.setOutput(stream);
        writer.write(metadata, new IIOImage(image, null, metadata), writeParam);
    }
}
 
Example #29
Source File: ITXtTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void writeTo(File f, ITXtTest t) {
    BufferedImage src = createBufferedImage();
    try {
        ImageOutputStream imageOutputStream =
            ImageIO.createImageOutputStream(f);

        ImageTypeSpecifier imageTypeSpecifier =
            new ImageTypeSpecifier(src);
        ImageWriter imageWriter =
            ImageIO.getImageWritersByFormatName("PNG").next();

        imageWriter.setOutput(imageOutputStream);

        IIOMetadata m =
            imageWriter.getDefaultImageMetadata(imageTypeSpecifier, null);

        String format = m.getNativeMetadataFormatName();
        Node root = m.getAsTree(format);

        IIOMetadataNode iTXt = t.getNode();
        root.appendChild(iTXt);
        m.setFromTree(format, root);

        imageWriter.write(new IIOImage(src, null, m));
        imageOutputStream.close();
        System.out.println("Writing done.");
    } catch (Throwable e) {
        throw new RuntimeException("Writing test failed.", e);
    }
}
 
Example #30
Source File: JPEGImageWriter.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public void writeToSequence(IIOImage image, ImageWriteParam param)
    throws IOException {
    setThreadLock();
    try {
        cbLock.check();

        if (sequencePrepared == false) {
            throw new IllegalStateException("sequencePrepared not called!");
        }
        // In the case of JPEG this does nothing different from write
        write(null, image, param);
    } finally {
        clearThreadLock();
    }
}