use of javax.imageio.ImageReader in project jersey by jersey.
the class RenderedImageProvider method readFrom.
@Override
public RenderedImage readFrom(Class<RenderedImage> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
if (IMAGE_MEDIA_TYPE.isCompatible(mediaType)) {
Iterator<ImageReader> readers = ImageIO.getImageReadersByMIMEType(mediaType.toString());
if (!readers.hasNext()) {
throw new IOException("The image-based media type " + mediaType + "is not supported for reading");
}
ImageReader reader = readers.next();
ImageInputStream in = ImageIO.createImageInputStream(entityStream);
reader.setInput(in, true, true);
BufferedImage bi = reader.read(0, reader.getDefaultReadParam());
in.close();
reader.dispose();
return bi;
} else {
return ImageIO.read(entityStream);
}
}
use of javax.imageio.ImageReader in project screenbird by adamhub.
the class Frame method getJPEGBounds.
/** @return the bounds of the image provided.
* @throws UnsupportedOperationException if the file cannot be read
* as an image by ImageIO classes.
* @throws IOException if an error occurred while reading the file
*/
private static Dimension getJPEGBounds(File file) throws IOException, IIOException {
FileInputStream in = null;
try {
//Hack for UNIX machines
if (MediaUtil.osIsUnix())
ImageIO.setUseCache(false);
in = new FileInputStream(file);
ImageIO.setCacheDirectory(new File(Settings.SCREEN_CAPTURE_DIR));
ImageInputStream stream = ImageIO.createImageInputStream(in);
Iterator iter = ImageIO.getImageReaders(stream);
ImageReader reader = (ImageReader) iter.next();
String formatName = reader.getFormatName().toLowerCase();
if (formatName.indexOf("jpeg") == -1 && formatName.indexOf("jpg") == -1) {
throw new IllegalArgumentException("This image file is not a JPEG image. This encoder only supports JPEG images.");
}
if (reader == null) {
throw new UnsupportedOperationException("The file \"" + file.getName() + "\" is not a supported image.");
}
reader.setInput(stream, false);
int w = reader.getWidth(0);
int h = reader.getHeight(0);
reader.dispose();
stream.close();
return new Dimension(w, h);
} catch (IllegalArgumentException e) {
log(e);
} catch (UnsupportedOperationException e) {
log(e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
log(e);
}
}
return null;
}
use of javax.imageio.ImageReader 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;
}
use of javax.imageio.ImageReader 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
}
}
}
}
use of javax.imageio.ImageReader in project d54 by mitrisdev.
the class GNU30Plugin method loadGifObject.
public ArrayList<BufferedImage> loadGifObject(InputStream is) throws IOException {
ImageInputStream image = ImageIO.createImageInputStream(is);
ImageReader ir = (ImageReader) ImageIO.getImageReadersBySuffix("gif").next();
ir.setInput(image, false);
int frames = ir.getNumImages(true);
ArrayList<BufferedImage> br = new ArrayList<BufferedImage>();
for (int i = 0; i < frames; ++i) {
BufferedImage frame = ir.read(i);
br.add(frame);
}
return br;
}
Aggregations