use of javax.imageio.ImageReader 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");
}
use of javax.imageio.ImageReader in project kotlin by JetBrains.
the class IconDetector method checkExtension.
private static void checkExtension(Context context, File file) {
try {
ImageInputStream input = ImageIO.createImageInputStream(file);
if (input != null) {
try {
Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
while (readers.hasNext()) {
ImageReader reader = readers.next();
try {
reader.setInput(input);
// Check file extension
String formatName = reader.getFormatName();
if (formatName != null && !formatName.isEmpty()) {
String path = file.getPath();
int index = path.lastIndexOf('.');
String extension = path.substring(index + 1).toLowerCase(Locale.US);
if (!formatName.equalsIgnoreCase(extension)) {
if (endsWith(path, DOT_JPG) && formatName.equals("JPEG")) {
//$NON-NLS-1$
return;
}
String message = String.format("Misleading file extension; named `.%1$s` but the " + "file format is `%2$s`", extension, formatName);
Location location = Location.create(file);
context.report(ICON_EXTENSION, location, message);
}
break;
}
} finally {
reader.dispose();
}
}
} finally {
input.close();
}
}
} catch (IOException e) {
// Pass -- we can't handle all image types, warn about those we can
}
}
use of javax.imageio.ImageReader in project kotlin by JetBrains.
the class IconDetector method getSize.
private static Dimension getSize(File file) {
try {
ImageInputStream input = ImageIO.createImageInputStream(file);
if (input != null) {
try {
Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
if (readers.hasNext()) {
ImageReader reader = readers.next();
try {
reader.setInput(input);
return new Dimension(reader.getWidth(0), reader.getHeight(0));
} finally {
reader.dispose();
}
}
} finally {
input.close();
}
}
// Fallback: read the image using the normal means
BufferedImage image = ImageIO.read(file);
if (image != null) {
return new Dimension(image.getWidth(), image.getHeight());
} else {
return null;
}
} catch (IOException e) {
// Pass -- we can't handle all image types, warn about those we can
return null;
}
}
use of javax.imageio.ImageReader in project camel by apache.
the class BarcodeTestBase method checkType.
private void checkType(File file, String type) throws IOException {
ImageInputStream iis = ImageIO.createImageInputStream(file);
ImageReader reader = ImageIO.getImageReaders(iis).next();
IOHelper.close(iis);
String format = reader.getFormatName();
assertEquals(type, format.toUpperCase());
}
use of javax.imageio.ImageReader in project zm-mailbox by Zimbra.
the class ImageUtil method getImageReader.
/**
* Returns the {@code ImageReader}, or {@code null} if none is available.
* @param contentType the MIME content type, or {@code null}
* @param filename the filename, or {@code null}
*/
public static ImageReader getImageReader(String contentType, String filename) {
log.debug("Looking up ImageReader for %s, %s", contentType, filename);
ImageReader reader = null;
if (!StringUtil.isNullOrEmpty(contentType)) {
reader = Iterators.getNext(ImageIO.getImageReadersByMIMEType(contentType), null);
}
if (reader == null && !StringUtil.isNullOrEmpty(filename)) {
String ext = FileUtil.getExtension(filename);
if (!StringUtil.isNullOrEmpty(ext)) {
reader = Iterators.getNext(ImageIO.getImageReadersBySuffix(ext), null);
}
}
log.debug("Returning %s", reader);
return reader;
}
Aggregations