Search in sources :

Example 1 with ImageWriter

use of javax.imageio.ImageWriter in project Openfire by igniterealtime.

the class Resizer method resizeAvatar.

public static void resizeAvatar(final Element vCardElement) {
    if (vCardElement == null) {
        return;
    }
    // XPath didn't work?
    if (vCardElement.element("PHOTO") == null) {
        return;
    }
    if (vCardElement.element("PHOTO").element("BINVAL") == null || vCardElement.element("PHOTO").element("TYPE") == null) {
        return;
    }
    final Element element = vCardElement.element("PHOTO").element("BINVAL");
    if (element.getTextTrim() == null || element.getTextTrim().isEmpty()) {
        return;
    }
    // Get a writer (check if we can generate a new image for the type of the original).
    final String type = vCardElement.element("PHOTO").element("TYPE").getTextTrim();
    final Iterator it = ImageIO.getImageWritersByMIMEType(type);
    if (!it.hasNext()) {
        Log.debug("Cannot resize avatar. No writers available for MIME type {}.", type);
        return;
    }
    final ImageWriter iw = (ImageWriter) it.next();
    // Extract the original avatar from the VCard.
    final byte[] original = Base64.decode(element.getTextTrim());
    // Crop and shrink, if needed.
    final int targetDimension = JiveGlobals.getIntProperty("avatar.resize.targetdimension", 96);
    final byte[] resized = cropAndShrink(original, targetDimension, iw);
    // If a resized image was created, replace to original avatar in the VCard.
    if (resized != null) {
        Log.debug("Replacing original avatar in vcard with a resized variant.");
        vCardElement.element("PHOTO").element("BINVAL").setText(Base64.encodeBytes(resized));
    }
}
Also used : Element(org.dom4j.Element) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter)

Example 2 with ImageWriter

use of javax.imageio.ImageWriter 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 3 with ImageWriter

use of javax.imageio.ImageWriter 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 4 with ImageWriter

use of javax.imageio.ImageWriter 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 5 with ImageWriter

use of javax.imageio.ImageWriter 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)

Aggregations

ImageWriter (javax.imageio.ImageWriter)171 ImageWriteParam (javax.imageio.ImageWriteParam)103 IIOImage (javax.imageio.IIOImage)92 ImageOutputStream (javax.imageio.stream.ImageOutputStream)85 BufferedImage (java.awt.image.BufferedImage)80 File (java.io.File)73 IOException (java.io.IOException)53 ByteArrayOutputStream (java.io.ByteArrayOutputStream)44 Test (org.junit.Test)37 IIOMetadata (javax.imageio.metadata.IIOMetadata)36 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)30 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)28 RenderedOp (javax.media.jai.RenderedOp)28 ImageReadParam (javax.imageio.ImageReadParam)26 Iterator (java.util.Iterator)25 ImageReader (javax.imageio.ImageReader)24 FileImageOutputStreamExtImpl (it.geosolutions.imageio.stream.output.FileImageOutputStreamExtImpl)20 ByteArrayInputStream (java.io.ByteArrayInputStream)20 Graphics2D (java.awt.Graphics2D)19 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)17