Search in sources :

Example 6 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 7 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 8 with ImageWriter

use of javax.imageio.ImageWriter in project nutz by nutzam.

the class Images method writeJpeg.

/**
     * 写入一个 JPG 图像
     * 
     * @param im
     *            图像对象
     * @param targetJpg
     *            目标输出 JPG 图像文件
     * @param quality
     *            质量 0.1f ~ 1.0f
     */
public static void writeJpeg(RenderedImage im, Object targetJpg, float quality) {
    try {
        ImageWriter writer = ImageIO.getImageWritersBySuffix("jpg").next();
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality);
        ImageOutputStream os = ImageIO.createImageOutputStream(targetJpg);
        writer.setOutput(os);
        writer.write((IIOMetadata) null, new IIOImage(im, null, null), param);
        os.flush();
        os.close();
    } catch (IOException e) {
        throw Lang.wrapThrow(e);
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 9 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)

Example 10 with ImageWriter

use of javax.imageio.ImageWriter in project photon-core by 1000Memories.

the class PhotoResource method getPhoto.

@GET
@Timed
@CacheControl(immutable = true)
public Response getPhoto(@PathParam("name") String name, @MatrixParam("w") Integer width, @MatrixParam("r") RotationParam rotateAngle, @MatrixParam("c") RectangleParam crop) throws Exception {
    InputStream resultStream;
    InputStream imageStream;
    try {
        imageStream = new BufferedInputStream(photoProvider.getPhotoInputStream(name));
    } catch (FileNotFoundException fnfe) {
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    String mimeType = URLConnection.guessContentTypeFromStream(imageStream);
    if (mimeType == null) {
        // Not implemented
        throw new WebApplicationException(501);
    }
    if (width != null || rotateAngle != null || crop != null) {
        BufferedImage image;
        TimerContext readContext = readTimer.time();
        try {
            image = ImageIO.read(imageStream);
        } finally {
            imageStream.close();
            readContext.stop();
        }
        if (crop != null) {
            image = com.thousandmemories.photon.core.Processor.crop(image, crop.get());
        }
        if (rotateAngle != null) {
            image = com.thousandmemories.photon.core.Processor.rotate(image, rotateAngle.get());
        }
        if (width != null) {
            image = com.thousandmemories.photon.core.Processor.fitToWidth(image, width);
        }
        Iterator<ImageWriter> i = ImageIO.getImageWritersByMIMEType(mimeType);
        if (!i.hasNext()) {
            mimeType = "image/jpeg";
            i = ImageIO.getImageWritersByMIMEType(mimeType);
        }
        ImageWriter writer = i.next();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        writer.setOutput(new MemoryCacheImageOutputStream(os));
        writer.write(image);
        image.flush();
        image = null;
        resultStream = new ByteArrayInputStream(os.toByteArray());
    } else {
        resultStream = imageStream;
    }
    return Response.ok(resultStream, mimeType).build();
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) TimerContext(com.yammer.metrics.core.TimerContext) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) Timed(com.yammer.metrics.annotation.Timed) CacheControl(com.yammer.dropwizard.jersey.caching.CacheControl)

Aggregations

ImageWriter (javax.imageio.ImageWriter)71 BufferedImage (java.awt.image.BufferedImage)45 ImageOutputStream (javax.imageio.stream.ImageOutputStream)41 IIOImage (javax.imageio.IIOImage)36 ImageWriteParam (javax.imageio.ImageWriteParam)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 IOException (java.io.IOException)27 Iterator (java.util.Iterator)21 File (java.io.File)20 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)19 IIOMetadata (javax.imageio.metadata.IIOMetadata)19 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ImageReader (javax.imageio.ImageReader)13 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)8 Graphics2D (java.awt.Graphics2D)6 ImageInputStream (javax.imageio.stream.ImageInputStream)6 Rectangle (java.awt.Rectangle)5 InputStream (java.io.InputStream)5 ImageWriterSpi (javax.imageio.spi.ImageWriterSpi)5 Color (java.awt.Color)4