Search in sources :

Example 1 with IIOImage

use of javax.imageio.IIOImage in project screenbird by adamhub.

the class PreviewPlayerTest method generateTestImageFile.

/**
     * Generates screenshot for JUnit testing
     * @return 
     *      File pointer to screen shot
     */
private File generateTestImageFile(Rectangle captureArea) {
    Robot awtRobot;
    String currentCaptureDir = Settings.SCREEN_CAPTURE_DIR;
    if (captureArea == null) {
        //Get full screen if no defined area of screen capture is defined
        captureArea = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    }
    try {
        BufferedImage bufferedImage = generateTestImage(captureArea);
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(1.0F);
        File imageFile = new File(currentCaptureDir, "test.jpeg");
        FileImageOutputStream stream = new FileImageOutputStream(imageFile);
        //Set up file access
        writer.setOutput(stream);
        //Create image
        IIOImage image = new IIOImage(bufferedImage, null, null);
        //write image
        writer.write(null, image, iwp);
        //Close image stream
        stream.close();
        return imageFile;
    } catch (IOException e) {
        System.err.println(e);
    }
    return null;
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) Rectangle(java.awt.Rectangle) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) Robot(java.awt.Robot) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 2 with IIOImage

use of javax.imageio.IIOImage in project screenbird by adamhub.

the class VideoCacheTest method generateTestImage.

/**
     * Generates screenshot for JUnit testing
     * @return 
     *      File pointer to screen shot
     */
private File generateTestImage() {
    Robot awtRobot;
    String currentCaptureDir = Settings.SCREEN_CAPTURE_DIR;
    try {
        awtRobot = new Robot();
        BufferedImage bufferedImage = awtRobot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(1.0F);
        File imageFile = new File(currentCaptureDir, "test.jpeg");
        FileImageOutputStream stream = new FileImageOutputStream(imageFile);
        //Set up file access
        writer.setOutput(stream);
        //Create image
        IIOImage image = new IIOImage(bufferedImage, null, null);
        //write image
        writer.write(null, image, iwp);
        //Close image stream
        stream.close();
        return imageFile;
    } catch (AWTException e) {
        System.err.println(e);
    } catch (IOException e) {
        System.err.println(e);
    }
    return null;
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) Rectangle(java.awt.Rectangle) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) Robot(java.awt.Robot) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage) AWTException(java.awt.AWTException)

Example 3 with IIOImage

use of javax.imageio.IIOImage in project screenbird by adamhub.

the class ImageUtilTest method generateTestImageFile.

/**
     * Generates screenshot for JUnit testing
     * @return 
     *      File pointer to screen shot
     */
private File generateTestImageFile(Rectangle captureArea) {
    if (captureArea == null) {
        //Get full screen if no defined area of screen capture is defined
        captureArea = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    }
    try {
        BufferedImage bufferedImage = generateTestImage(captureArea);
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(1.0F);
        File imageFile = generateTempFile("jpeg");
        FileImageOutputStream stream = new FileImageOutputStream(imageFile);
        //Set up file access
        writer.setOutput(stream);
        //Create image
        IIOImage image = new IIOImage(bufferedImage, null, null);
        //write image
        writer.write(null, image, iwp);
        //Close image stream
        stream.close();
        return imageFile;
    } catch (IOException e) {
        System.err.println(e);
    }
    return null;
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) Rectangle(java.awt.Rectangle) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 4 with IIOImage

use of javax.imageio.IIOImage in project screenbird by adamhub.

the class ImageUtil method markAndCompress.

/**
     * Adds a watermark to the image and compresses it.
     * @param imageSource
     * @param compressionQuality
     * @param mark
     * @param markImageSource 
     */
public static void markAndCompress(String imageSource, float compressionQuality, boolean mark, String markImageSource) {
    try {
        File fileImageSource = new File(imageSource);
        if (!fileImageSource.exists()) {
            throw new ImageDoesNotExistException("Mark Image doesn't exists: " + fileImageSource.getAbsolutePath());
        }
        BufferedImage bufferedImage = ImageIO.read(fileImageSource);
        if (mark) {
            addMark(bufferedImage, markImageSource, 1.0f, ImageUtil.MARK_LEFT_BOTTOM);
        }
        // Get a jpeg writer
        ImageWriter writer = null;
        Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
        if (iter.hasNext()) {
            writer = (ImageWriter) iter.next();
        }
        if (writer == null) {
            throw new IOException("Could not get JPEG writer");
        }
        // Prepare output file
        ImageOutputStream ios = ImageIO.createImageOutputStream(fileImageSource);
        if (ios == null) {
            throw new IOException("Could not open image stream to write image watermark");
        }
        writer.setOutput(ios);
        // Set the compression quality
        ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault());
        iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        if (compressionQuality < 0.0F || compressionQuality > 1.0F) {
            compressionQuality = 1.0F;
        }
        iwparam.setCompressionQuality(compressionQuality);
        // Write the image
        writer.write(null, new IIOImage(bufferedImage, null, null), iwparam);
        // Cleanup
        ios.flush();
        writer.dispose();
        ios.close();
    } catch (IllegalArgumentException e) {
        log(e);
    } catch (IOException e) {
        log(e);
    } catch (ImageDoesNotExistException e) {
        log(e);
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) Iterator(java.util.Iterator) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 5 with IIOImage

use of javax.imageio.IIOImage in project jdk8u_jdk by JetBrains.

the class BooleanAttributes method test.

public static void test(String mimeType, boolean useStreamMeta, String metaXml, String... boolXpaths) throws Exception {
    BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    iw.setOutput(ios);
    ImageWriteParam param = null;
    IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
    IIOMetadata imageMeta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
    Source src = new StreamSource(new StringReader(metaXml));
    DOMResult dst = new DOMResult();
    transform(src, dst);
    Document doc = (Document) dst.getNode();
    Element node = doc.getDocumentElement();
    String metaFormat = node.getNodeName();
    // Verify that the default metadata gets formatted correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, false);
    meta.mergeTree(metaFormat, node);
    // Verify that the merged metadata gets formatte correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
    iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
    iw.dispose();
    ios.close();
    ImageReader ir = ImageIO.getImageReader(iw);
    byte[] bytes = os.toByteArray();
    if (bytes.length == 0)
        throw new AssertionError("Zero length image file");
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    ir.setInput(iis);
    if (useStreamMeta)
        meta = ir.getStreamMetadata();
    else
        meta = ir.getImageMetadata(0);
    // Verify again after writing and re-reading the image
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) StreamSource(javax.xml.transform.stream.StreamSource) Element(org.w3c.dom.Element) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageWriter(javax.imageio.ImageWriter) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) Document(org.w3c.dom.Document) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream)

Aggregations

IIOImage (javax.imageio.IIOImage)122 ImageWriter (javax.imageio.ImageWriter)92 ImageWriteParam (javax.imageio.ImageWriteParam)78 ImageOutputStream (javax.imageio.stream.ImageOutputStream)78 BufferedImage (java.awt.image.BufferedImage)66 File (java.io.File)45 IOException (java.io.IOException)41 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 IIOMetadata (javax.imageio.metadata.IIOMetadata)31 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)24 ByteArrayInputStream (java.io.ByteArrayInputStream)22 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)17 ImageReader (javax.imageio.ImageReader)16 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)16 Graphics2D (java.awt.Graphics2D)14 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)14 Test (org.junit.Test)14 FileOutputStream (java.io.FileOutputStream)13 RenderedImage (java.awt.image.RenderedImage)10 Iterator (java.util.Iterator)10