Search in sources :

Example 61 with IIOImage

use of javax.imageio.IIOImage in project vcell by virtualcell.

the class FormatSpecificSpecs method encodeJPEG.

public static VideoMediaSampleJPEG encodeJPEG(BufferedImage bufferedImage, float compressionQuality, int width, int height, int sampleDuration, int bitsPerPixel, boolean isGrayscale) throws Exception {
    ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpeg").next();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
    imageWriter.setOutput(imageOutputStream);
    ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
    imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    // quality 0(very compressed, lossy) -> 1.0(less compressed,loss-less)
    imageWriteParam.setCompressionQuality(compressionQuality);
    IIOImage iioImage = new IIOImage(bufferedImage, null, null);
    imageWriter.write(null, iioImage, imageWriteParam);
    imageOutputStream.close();
    imageWriter.dispose();
    return new VideoMediaSampleJPEG(width, height, sampleDuration, byteArrayOutputStream.toByteArray(), bitsPerPixel, isGrayscale);
}
Also used : VideoMediaSampleJPEG(cbit.vcell.export.gloworm.quicktime.VideoMediaSampleJPEG) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 62 with IIOImage

use of javax.imageio.IIOImage in project chuidiang-ejemplos by chuidiang.

the class VideoStream method getnextframe.

// -----------------------------------
// getnextframe
// returns the next frame as an array of byte and the size of the frame
// -----------------------------------
public int getnextframe(byte[] frame) throws Exception {
    BufferedImage image = getDesktopScreenshot();
    ByteArrayOutputStream bas = new ByteArrayOutputStream();
    ImageOutputStream outputStream = ImageIO.createImageOutputStream(bas);
    // NOTE: The rest of the code is just a cleaned up version of your code
    // Obtain writer for JPEG format
    ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
    // Configure JPEG compression: 70% quality
    ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
    jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    jpgWriteParam.setCompressionQuality(0.7f);
    // Set your in-memory stream as the output
    jpgWriter.setOutput(outputStream);
    // Write image as JPEG w/configured settings to the in-memory stream
    // (the IIOImage is just an aggregator object, allowing you to associate
    // thumbnails and metadata to the image, it "does" nothing)
    jpgWriter.write(null, new IIOImage(image, null, null), jpgWriteParam);
    // Dispose the writer to free resources
    jpgWriter.dispose();
    byte[] imageBytes = bas.toByteArray();
    System.out.println(imageBytes.length);
    for (int i = 0; i < imageBytes.length; i++) {
        frame[i] = imageBytes[i];
    }
    return imageBytes.length;
// int length = 0;
// String length_string;
// byte[] frame_length = new byte[5];
// 
// //read current frame length
// fis.read(frame_length,0,5);
// 
// //transform frame_length to integer
// length_string = new String(frame_length);
// length = Integer.parseInt(length_string);
// 
// return(fis.read(frame,0,length));
}
Also used : ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 63 with IIOImage

use of javax.imageio.IIOImage in project SIMVA-SoS by SESoS.

the class SunJPEGEncoderAdapter method encode.

/**
 * Encodes an image in JPEG format and writes it to an output stream.
 *
 * @param bufferedImage  the image to be encoded (<code>null</code> not
 *     permitted).
 * @param outputStream  the OutputStream to write the encoded image to
 *     (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O problem.
 * @throws NullPointerException if <code>bufferedImage</code> is
 *     <code>null</code>.
 */
@Override
public void encode(BufferedImage bufferedImage, OutputStream outputStream) throws IOException {
    ParamChecks.nullNotPermitted(bufferedImage, "bufferedImage");
    ParamChecks.nullNotPermitted(outputStream, "outputStream");
    Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg");
    ImageWriter writer = (ImageWriter) iterator.next();
    ImageWriteParam p = writer.getDefaultWriteParam();
    p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    p.setCompressionQuality(this.quality);
    ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream);
    writer.setOutput(ios);
    writer.write(null, new IIOImage(bufferedImage, null, null), p);
    ios.flush();
    writer.dispose();
    ios.close();
}
Also used : Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 64 with IIOImage

use of javax.imageio.IIOImage in project hutool by looly.

the class ImgUtil method write.

/**
 * 通过{@link ImageWriter}写出图片到输出流
 *
 * @param image   图片
 * @param writer  {@link ImageWriter}
 * @param output  输出的Image流{@link ImageOutputStream}
 * @param quality 质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩
 * @return 是否成功写出
 * @since 4.3.2
 */
public static boolean write(Image image, ImageWriter writer, ImageOutputStream output, float quality) {
    if (writer == null) {
        return false;
    }
    writer.setOutput(output);
    final RenderedImage renderedImage = toRenderedImage(image);
    // 设置质量
    ImageWriteParam imgWriteParams = null;
    if (quality > 0 && quality < 1) {
        imgWriteParams = writer.getDefaultWriteParam();
        if (imgWriteParams.canWriteCompressed()) {
            imgWriteParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            imgWriteParams.setCompressionQuality(quality);
            // ColorModel.getRGBdefault();
            final ColorModel colorModel = renderedImage.getColorModel();
            imgWriteParams.setDestinationType(new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16)));
        }
    }
    try {
        if (null != imgWriteParams) {
            writer.write(null, new IIOImage(renderedImage, null, null), imgWriteParams);
        } else {
            writer.write(renderedImage);
        }
        output.flush();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        writer.dispose();
    }
    return true;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) ColorModel(java.awt.image.ColorModel) IOException(java.io.IOException) RenderedImage(java.awt.image.RenderedImage) ImageWriteParam(javax.imageio.ImageWriteParam) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage)

Example 65 with IIOImage

use of javax.imageio.IIOImage in project propane by ruby-processing.

the class PImageAWT method saveImageIO.

/**
 * Use ImageIO functions from Java 1.4 and later to handle image save.Various formats are supported, typically jpeg, png, bmp, and wbmp.To get a list of the supported formats for writing, use: <BR>
 *    <code>println(javax.imageio.ImageIO.getReaderFormatNames())</code>
 *
 * @param path
 * @return
 * @throws java.io.IOException
 * @path The path to which the file should be written.
 */
protected boolean saveImageIO(String path) throws IOException {
    try {
        int outputFormat = (format == ARGB) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
        String extension = path.substring(path.lastIndexOf('.') + 1).toLowerCase();
        // http://code.google.com/p/processing/issues/detail?id=415
        if (extension.equals("bmp") || extension.equals("jpg") || extension.equals("jpeg")) {
            outputFormat = BufferedImage.TYPE_INT_RGB;
        }
        BufferedImage bimage = new BufferedImage(pixelWidth, pixelHeight, outputFormat);
        bimage.setRGB(0, 0, pixelWidth, pixelHeight, pixels, 0, pixelWidth);
        File file = new File(path);
        ImageWriter writer = null;
        ImageWriteParam param = null;
        IIOMetadata metadata = null;
        if (extension.equals("jpg") || extension.equals("jpeg")) {
            if ((writer = imageioWriter("jpeg")) != null) {
                // Set JPEG quality to 90% with baseline optimization. Setting this
                // to 1 was a huge jump (about triple the size), so this seems good.
                // Oddly, a smaller file size than Photoshop at 90%, but I suppose
                // it's a completely different algorithm.
                param = writer.getDefaultWriteParam();
                param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                param.setCompressionQuality(0.9f);
            }
        }
        if (extension.equals("png")) {
            if ((writer = imageioWriter("png")) != null) {
                param = writer.getDefaultWriteParam();
                if (false) {
                    metadata = imageioDPI(writer, param, 100);
                }
            }
        }
        if (writer != null) {
            try (BufferedOutputStream output = new BufferedOutputStream(PApplet.createOutput(file))) {
                writer.setOutput(ImageIO.createImageOutputStream(output));
                // writer.write(null, new IIOImage(bimage, null, null), param);
                writer.write(metadata, new IIOImage(bimage, null, metadata), param);
                writer.dispose();
                output.flush();
            }
            return true;
        }
        // If iter.hasNext() somehow fails up top, it falls through to here
        return javax.imageio.ImageIO.write(bimage, extension, file);
    } catch (IOException e) {
        throw new IOException("image save failed.");
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Aggregations

IIOImage (javax.imageio.IIOImage)123 ImageWriter (javax.imageio.ImageWriter)92 ImageWriteParam (javax.imageio.ImageWriteParam)79 ImageOutputStream (javax.imageio.stream.ImageOutputStream)79 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)32 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)25 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 Iterator (java.util.Iterator)11 RenderedImage (java.awt.image.RenderedImage)10