Search in sources :

Example 6 with ImageReader

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

the class Images method readJpeg.

/**
     * 尝试读取JPEG文件的高级方法,可读取32位的jpeg文件
     * <p/>
     * 来自:
     * http://stackoverflow.com/questions/2408613/problem-reading-jpeg-image-
     * using-imageio-readfile-file
     * 
     */
private static BufferedImage readJpeg(InputStream in) throws IOException {
    Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JPEG");
    ImageReader reader = null;
    while (readers.hasNext()) {
        reader = readers.next();
        if (reader.canReadRaster()) {
            break;
        }
    }
    if (reader == null)
        return null;
    ImageInputStream input = ImageIO.createImageInputStream(in);
    reader.setInput(input);
    // Read the image raster
    Raster raster = reader.readRaster(0, null);
    BufferedImage image = createJPEG4(raster);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writeJpeg(image, out, 1);
    out.flush();
    return read(new ByteArrayInputStream(out.toByteArray()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage)

Example 7 with ImageReader

use of javax.imageio.ImageReader in project Botnak by Gocnak.

the class FaceManager method sanityCheck.

/**
     * Tests to see if an image is within reasonable downloading bounds (5000x5000)
     *
     * @param url The URL to the image to check.
     * @return True if within downloadable bounds else false.
     */
private static boolean sanityCheck(URL url) {
    try (ImageInputStream in = ImageIO.createImageInputStream(url.openStream())) {
        final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
        if (readers.hasNext()) {
            ImageReader reader = readers.next();
            try {
                reader.setInput(in);
                Dimension d = new Dimension(reader.getWidth(0), reader.getHeight(0));
                return d.getHeight() < 5000 && d.getWidth() < 5000;
            } finally {
                reader.dispose();
            }
        }
    } catch (Exception e) {
        return false;
    }
    return false;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ImageReader(javax.imageio.ImageReader)

Example 8 with ImageReader

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

Example 9 with ImageReader

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

the class ImageReaderReadAll method main.

public static void main(String[] argv) {
    ImageReader ireader;
    ImageReadParam irp;
    IIOImage image;
    BufferedImage bi;
    BufferedImage bi_1;
    BufferedImage bi_2;
    ireader = new DummyImageReaderImpl(null);
    MemoryCacheImageInputStream mciis = new MemoryCacheImageInputStream(new ByteArrayInputStream(ba));
    ireader.setInput(mciis);
    irp = new ImageReadParam();
    irp.setDestination(new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR));
    try {
        image = ireader.readAll(0, irp);
        bi_1 = ireader.read(0, irp);
        bi_2 = ireader.read(0);
    } catch (java.io.IOException ee) {
        throw new RuntimeException("Unexpected exception: " + ee);
    }
    bi = (BufferedImage) image.getRenderedImage();
    if (bi.getType() != bi_1.getType()) {
        throw new RuntimeException("Images have different type!");
    }
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) ByteArrayInputStream(java.io.ByteArrayInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 10 with ImageReader

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

the class BMPWriteParamTest method getWriteResult.

private static BufferedImage getWriteResult(BufferedImage img, String format) throws IOException {
    ImageWriter iw = null;
    Iterator writers = ImageIO.getImageWritersByFormatName(format);
    while (writers.hasNext()) {
        iw = (ImageWriter) writers.next();
        System.out.println(format + " -> " + iw.toString());
    }
    if (iw == null) {
        throw new RuntimeException("No available Image writer for " + format);
    }
    ImageWriteParam param = iw.getDefaultWriteParam();
    param.setSourceRegion(new Rectangle(10, 10, 31, 31));
    param.setSourceSubsampling(3, 3, 0, 0);
    IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOImage iio_img = new IIOImage(img, null, meta);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    iw.setOutput(ios);
    iw.write(meta, iio_img, param);
    ios.flush();
    byte[] ba_image = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);
    ImageReader ir = null;
    Iterator readers = ImageIO.getImageReadersByFormatName(format);
    while (readers.hasNext()) {
        ir = (ImageReader) readers.next();
        System.out.println(format + " -> " + ir.toString());
    }
    if (ir == null) {
        throw new RuntimeException("No available Image reader for " + format);
    }
    ir.setInput(ImageIO.createImageInputStream(bais));
    BufferedImage res = ir.read(0);
    return res;
}
Also used : ImageWriter(javax.imageio.ImageWriter) Rectangle(java.awt.Rectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) Iterator(java.util.Iterator) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Aggregations

ImageReader (javax.imageio.ImageReader)59 BufferedImage (java.awt.image.BufferedImage)32 ImageInputStream (javax.imageio.stream.ImageInputStream)32 IOException (java.io.IOException)22 ByteArrayInputStream (java.io.ByteArrayInputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 ImageReadParam (javax.imageio.ImageReadParam)14 ImageWriter (javax.imageio.ImageWriter)13 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)12 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 InputStream (java.io.InputStream)10 Iterator (java.util.Iterator)10 IIOMetadata (javax.imageio.metadata.IIOMetadata)10 IIOImage (javax.imageio.IIOImage)9 ImageOutputStream (javax.imageio.stream.ImageOutputStream)7 File (java.io.File)6 ImageWriteParam (javax.imageio.ImageWriteParam)5 ImageReaderSpi (javax.imageio.spi.ImageReaderSpi)4 Graphics (java.awt.Graphics)3 Graphics2D (java.awt.Graphics2D)3