Search in sources :

Example 6 with ImageReadParam

use of javax.imageio.ImageReadParam in project intellij-community by JetBrains.

the class IfsUtil method refresh.

/**
   * Load image data for file and put user data attributes into file.
   *
   * @param file File
   * @return true if file image is loaded.
   * @throws java.io.IOException if image can not be loaded
   */
private static boolean refresh(@NotNull VirtualFile file) throws IOException {
    Long loadedTimeStamp = file.getUserData(TIMESTAMP_KEY);
    SoftReference<BufferedImage> imageRef = file.getUserData(BUFFERED_IMAGE_REF_KEY);
    if (loadedTimeStamp == null || loadedTimeStamp.longValue() != file.getTimeStamp() || SoftReference.dereference(imageRef) == null) {
        try {
            final byte[] content = file.contentsToByteArray();
            if (ICO_FORMAT.equalsIgnoreCase(file.getExtension())) {
                try {
                    final BufferedImage image = ICO_IMAGE_PARSER.getBufferedImage(new ByteSourceArray(content), null);
                    file.putUserData(FORMAT_KEY, ICO_FORMAT);
                    file.putUserData(BUFFERED_IMAGE_REF_KEY, new SoftReference<>(image));
                    return true;
                } catch (ImageReadException ignore) {
                }
            }
            InputStream inputStream = new ByteArrayInputStream(content, 0, content.length);
            ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);
            try {
                Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
                if (imageReaders.hasNext()) {
                    ImageReader imageReader = imageReaders.next();
                    try {
                        file.putUserData(FORMAT_KEY, imageReader.getFormatName());
                        ImageReadParam param = imageReader.getDefaultReadParam();
                        imageReader.setInput(imageInputStream, true, true);
                        int minIndex = imageReader.getMinIndex();
                        BufferedImage image = imageReader.read(minIndex, param);
                        file.putUserData(BUFFERED_IMAGE_REF_KEY, new SoftReference<>(image));
                        return true;
                    } finally {
                        imageReader.dispose();
                    }
                }
            } finally {
                imageInputStream.close();
            }
        } finally {
            // We perform loading no more needed
            file.putUserData(TIMESTAMP_KEY, file.getTimeStamp());
        }
    }
    return false;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteSourceArray(org.apache.sanselan.common.byteSources.ByteSourceArray) ImageInputStream(javax.imageio.stream.ImageInputStream) BufferedImage(java.awt.image.BufferedImage) ImageReadParam(javax.imageio.ImageReadParam) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageReadException(org.apache.sanselan.ImageReadException) ImageReader(javax.imageio.ImageReader)

Example 7 with ImageReadParam

use of javax.imageio.ImageReadParam in project spring-framework by spring-projects.

the class BufferedImageHttpMessageConverter method read.

@Override
public BufferedImage read(Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    ImageInputStream imageInputStream = null;
    ImageReader imageReader = null;
    try {
        imageInputStream = createImageInputStream(inputMessage.getBody());
        MediaType contentType = inputMessage.getHeaders().getContentType();
        Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
        if (imageReaders.hasNext()) {
            imageReader = imageReaders.next();
            ImageReadParam irp = imageReader.getDefaultReadParam();
            process(irp);
            imageReader.setInput(imageInputStream, true);
            return imageReader.read(0, irp);
        } else {
            throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
        }
    } finally {
        if (imageReader != null) {
            imageReader.dispose();
        }
        if (imageInputStream != null) {
            try {
                imageInputStream.close();
            } catch (IOException ex) {
            // ignore
            }
        }
    }
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) FileCacheImageInputStream(javax.imageio.stream.FileCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MediaType(org.springframework.http.MediaType) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader)

Example 8 with ImageReadParam

use of javax.imageio.ImageReadParam in project intellij-community by JetBrains.

the class ImagePreviewComponent method readImageFromBytes.

@NotNull
public static BufferedImage readImageFromBytes(@NotNull byte[] content) throws IOException {
    InputStream inputStream = new ByteArrayInputStream(content, 0, content.length);
    ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);
    try {
        Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
        if (imageReaders.hasNext()) {
            ImageReader imageReader = imageReaders.next();
            try {
                ImageReadParam param = imageReader.getDefaultReadParam();
                imageReader.setInput(imageInputStream, true, true);
                int minIndex = imageReader.getMinIndex();
                return imageReader.read(minIndex, param);
            } finally {
                imageReader.dispose();
            }
        }
    } finally {
        imageInputStream.close();
    }
    throw new IOException("Can't read image from given content");
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with ImageReadParam

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

the class ImageReadParamPasses method test.

private static void test(int minPass, int numPasses) {
    ImageReadParam param = new ImageReadParam();
    param.setSourceProgressivePasses(minPass, numPasses);
    expect(param.getSourceMinProgressivePass(), minPass);
    expect(param.getSourceNumProgressivePasses(), numPasses);
    int maxPass = numPasses == maxint ? maxint : minPass + numPasses - 1;
    expect(param.getSourceMaxProgressivePass(), maxPass);
}
Also used : ImageReadParam(javax.imageio.ImageReadParam)

Example 10 with ImageReadParam

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

the class BmpBigDestinationTest method main.

public static void main(String[] args) {
    try {
        BufferedImage src = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = src.createGraphics();
        g.setColor(Color.red);
        g.fillRect(0, 0, 100, 100);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByFormatName(format).next();
        if (iw == null) {
            throw new RuntimeException("No writer available. Test failed.");
        }
        iw.setOutput(ImageIO.createImageOutputStream(baos));
        iw.write(src);
        byte[] data = baos.toByteArray();
        ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName(format).next();
        ir.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(data)));
        Iterator specifiers = ir.getImageTypes(0);
        ImageTypeSpecifier typeSpecifier = null;
        if (specifiers.hasNext()) {
            typeSpecifier = (ImageTypeSpecifier) specifiers.next();
        }
        ImageReadParam param = new ImageReadParam();
        BufferedImage dst = typeSpecifier.createBufferedImage(200, 200);
        param.setDestination(dst);
        ir.read(0, param);
        checkResults(src, dst);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Unexpected exception. Test failed.");
    }
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageWriter(javax.imageio.ImageWriter) Iterator(java.util.Iterator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Graphics2D(java.awt.Graphics2D)

Aggregations

ImageReadParam (javax.imageio.ImageReadParam)20 BufferedImage (java.awt.image.BufferedImage)16 ImageReader (javax.imageio.ImageReader)14 IOException (java.io.IOException)9 ImageInputStream (javax.imageio.stream.ImageInputStream)6 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)5 MediaType (org.springframework.http.MediaType)4 Graphics2D (java.awt.Graphics2D)3 Iterator (java.util.Iterator)3 UnavailableException (javax.servlet.UnavailableException)3 HttpHeaders (org.springframework.http.HttpHeaders)3 ResponseEntity (org.springframework.http.ResponseEntity)3 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 Rectangle (java.awt.Rectangle)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ImageWriter (javax.imageio.ImageWriter)2