Search in sources :

Example 41 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project adempiere by adempiere.

the class ScreenShot method createJPEG.

/**
	 * 	Create JPEG file from window
	 * 	@param window window
	 * 	@param fileName optional file name
	 * 	@return true if created
	 */
public static boolean createJPEG(Window window, String fileName) {
    BufferedImage bi = null;
    if (window == null || fileName == null)
        new IllegalArgumentException("ScreenShot.createJPEG Window os NULL");
    //	Get File
    File file = getJPGFile(window);
    if (file == null)
        return false;
    log.config("File=" + file);
    if (file.exists())
        file.delete();
    //	Get Writer
    Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
    ImageWriter writer = (ImageWriter) writers.next();
    if (writer == null) {
        log.log(Level.SEVERE, "no ImageWriter");
        return false;
    }
    //	Get Image
    try {
        Thread.sleep(1000);
        bi = getImage(window);
    } catch (InterruptedException ex) {
        log.log(Level.SEVERE, "ex", ex);
    }
    //	Write Image
    try {
        ImageOutputStream ios = ImageIO.createImageOutputStream(file);
        writer.setOutput(ios);
        writer.write(bi);
        ios.flush();
        ios.close();
    } catch (IOException ex) {
        log.log(Level.SEVERE, "ex", ex);
        return false;
    }
    return true;
}
Also used : Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 42 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class LUTCompareTest method createTestImage.

/* Create test image with two frames:
     *  1) with {red, red} palette
     *  2) with {blue, red } palette
     */
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");
}
Also used : ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 43 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class CachePermissionsTest method main.

public static void main(String[] args) {
    boolean isFileCacheExpected = Boolean.valueOf(args[0]).booleanValue();
    System.out.println("Is file cache expected: " + isFileCacheExpected);
    ImageIO.setUseCache(true);
    System.out.println("java.io.tmpdir is " + System.getProperty("java.io.tmpdir"));
    if (args.length > 1) {
        String testsrc = System.getProperty("test.src", ".");
        String policy = testsrc + File.separator + args[1];
        System.out.println("Policy file: " + policy);
        System.setProperty("java.security.policy", policy);
        System.out.println("Install security manager...");
        System.setSecurityManager(new SecurityManager());
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
        boolean isFileCache = ios.isCachedFile();
        System.out.println("Is file cache used: " + isFileCache);
        if (isFileCache != isFileCacheExpected) {
            System.out.println("WARNING: file chace usage is not as expected!");
        }
        System.out.println("Verify data writing...");
        for (int i = 0; i < 8192; i++) {
            ios.writeInt(i);
        }
        System.out.println("Verify data reading...");
        ios.seek(0L);
        for (int i = 0; i < 8192; i++) {
            int j = ios.readInt();
            if (i != j) {
                throw new RuntimeException("Wrong data in the stream " + j + " instead of " + i);
            }
        }
        System.out.println("Verify stream closing...");
        ios.close();
    } catch (IOException e) {
        /*
             * Something went wrong?
             */
        throw new RuntimeException("Test FAILED.", e);
    } catch (SecurityException e) {
        /*
             * We do not expect security execptions here:
             * we there are any security restrition, ImageIO
             * should swith to memory-cached streams, instead
             * of using file cache.
             */
        throw new RuntimeException("Test FAILED.", e);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 44 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class CompressionModeTest method doTest.

private static void doTest(int mode) {
    String fileFormat = "bmp";
    try {
        ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix(fileFormat).next();
        if (iw == null) {
            throw new RuntimeException("No available image writer for " + fileFormat + " Test failed.");
        }
        File file = new File("image." + fileFormat);
        ImageOutputStream ios = ImageIO.createImageOutputStream(file);
        iw.setOutput(ios);
        BufferedImage bimg = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        Graphics g = bimg.getGraphics();
        g.setColor(Color.green);
        g.fillRect(0, 0, 100, 100);
        ImageWriteParam param = iw.getDefaultWriteParam();
        param.setCompressionMode(mode);
        IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bimg), param);
        IIOImage iioImg = new IIOImage(bimg, null, meta);
        iw.write(null, iioImg, param);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Test failed.");
    }
}
Also used : Graphics(java.awt.Graphics) IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage)

Example 45 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class TopDownTest method writeWithCompression.

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);
}
Also used : ImageWriter(javax.imageio.ImageWriter) BMPImageWriteParam(javax.imageio.plugins.bmp.BMPImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Aggregations

ImageOutputStream (javax.imageio.stream.ImageOutputStream)64 ImageWriter (javax.imageio.ImageWriter)41 BufferedImage (java.awt.image.BufferedImage)37 ByteArrayOutputStream (java.io.ByteArrayOutputStream)36 IIOImage (javax.imageio.IIOImage)33 IOException (java.io.IOException)26 ImageWriteParam (javax.imageio.ImageWriteParam)26 File (java.io.File)24 IIOMetadata (javax.imageio.metadata.IIOMetadata)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)12 Iterator (java.util.Iterator)9 ImageInputStream (javax.imageio.stream.ImageInputStream)9 ImageReader (javax.imageio.ImageReader)7 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)7 Graphics2D (java.awt.Graphics2D)5 FileOutputStream (java.io.FileOutputStream)5 Color (java.awt.Color)4 Graphics (java.awt.Graphics)4 OutputStream (java.io.OutputStream)4