use of javax.imageio.ImageReader in project poi by apache.
the class ImageUtils method getImageDimension.
/**
* Return the dimension of this image
*
* @param is the stream containing the image data
* @param type type of the picture: {@link org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_JPEG},
* {@link org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_PNG} or {@link org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_DIB}
*
* @return image dimension in pixels
*/
public static Dimension getImageDimension(InputStream is, int type) {
Dimension size = new Dimension();
switch(type) {
//other formats like WMF, EMF and PICT are not supported in Java
case Workbook.PICTURE_TYPE_JPEG:
case Workbook.PICTURE_TYPE_PNG:
case Workbook.PICTURE_TYPE_DIB:
try {
//read the image using javax.imageio.*
ImageInputStream iis = ImageIO.createImageInputStream(is);
try {
Iterator<ImageReader> i = ImageIO.getImageReaders(iis);
ImageReader r = i.next();
try {
r.setInput(iis);
BufferedImage img = r.read(0);
int[] dpi = getResolution(r);
//since cannot divide by zero
if (dpi[0] == 0)
dpi[0] = PIXEL_DPI;
if (dpi[1] == 0)
dpi[1] = PIXEL_DPI;
size.width = img.getWidth() * PIXEL_DPI / dpi[0];
size.height = img.getHeight() * PIXEL_DPI / dpi[1];
} finally {
r.dispose();
}
} finally {
iis.close();
}
} catch (IOException e) {
//silently return if ImageIO failed to read the image
logger.log(POILogger.WARN, e);
}
break;
default:
logger.log(POILogger.WARN, "Only JPEG, PNG and DIB pictures can be automatically sized");
}
return size;
}
use of javax.imageio.ImageReader in project ChatGameFontificator by GlitchCog.
the class AnimatedGifUtil method loadDittoAnimatedGif.
/**
* Fix (ditto) for Java's animated GIF interpretation
*
* Adapted from http://stackoverflow.com/questions/26801433/fix-frame-rate-of-animated-gif-in-java#answer-26829534
*
* @param url
* The URL for the animated GIF to be loaded
* @param dim
* The dimension object to be filled by the width and height of the loaded animated GIF
* @return The loaded animated GIF
* @throws Exception
*/
public static Image loadDittoAnimatedGif(final URL url, Dimension dim) {
final Image dimImage = new ImageIcon(url).getImage();
Image image = null;
try {
ImageReader gifReader = ImageIO.getImageReadersByFormatName(GIF_EXTENSION).next();
InputStream imageStream = url.openStream();
gifReader.setInput(ImageIO.createImageInputStream(imageStream));
IIOMetadata imageMetaData = gifReader.getImageMetadata(0);
String metaFormatName = imageMetaData.getNativeMetadataFormatName();
final int frameCount = gifReader.getNumImages(true);
ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baoStream);
ImageWriter writer = ImageIO.getImageWriter(gifReader);
writer.setOutput(ios);
writer.prepareWriteSequence(null);
final int imgWidth = dimImage.getWidth(null);
final int imgHeight = dimImage.getHeight(null);
dim.setSize(imgWidth, imgHeight);
for (int i = 0; i < frameCount; i++) {
// This read method takes into account the frame's top and left coordinates
BufferedImage frame = gifReader.read(i);
IIOMetadataNode nodes = (IIOMetadataNode) gifReader.getImageMetadata(i).getAsTree(metaFormatName);
for (int j = 0; j < nodes.getLength(); j++) {
IIOMetadataNode node = (IIOMetadataNode) nodes.item(j);
if (GRAPHIC_CTRL_EXT.equalsIgnoreCase(node.getNodeName())) {
int delay = Integer.parseInt(node.getAttribute(ATTRIBUTE_DELAY_TIME));
// Minimum delay for browsers, which delay animated GIFs much more than would be to spec
if (delay < MIN_ANI_GIF_DELAY) {
node.setAttribute(ATTRIBUTE_DELAY_TIME, Integer.toString(MIN_ANI_GIF_DELAY));
}
// at least not for BTTV's (ditto)
if (node.getAttribute(ATTRIBUTE_DISPOSAL_METHOD).equals(DISPOSE_RESTORE_TO_PREVIOUS)) {
node.setAttribute(ATTRIBUTE_DISPOSAL_METHOD, DISPOSE_RESTORE_TO_BGCOLOR);
}
}
}
IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(frame), null);
metadata.setFromTree(metadata.getNativeMetadataFormatName(), nodes);
// This modified frame is necessary to get the correct image placement, width, and height in the final GIF
BufferedImage frameMod = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
Graphics big = frameMod.getGraphics();
big.drawImage(frame, 0, 0, null);
IIOImage fixedFrame = new IIOImage(frameMod, null, metadata);
writer.writeToSequence(fixedFrame, writer.getDefaultWriteParam());
}
writer.endWriteSequence();
if (ios != null) {
ios.close();
}
image = Toolkit.getDefaultToolkit().createImage(baoStream.toByteArray());
} catch (Exception e) {
// If anything goes wrong, just load it normally
logger.error("Error loading animated GIF (ditto) from " + url, e);
image = new ImageIcon(url).getImage();
dim.setSize(image.getWidth(null), image.getHeight(null));
}
return image;
}
use of javax.imageio.ImageReader in project android by JetBrains.
the class ConvertToWebpAction method isEligibleForConversion.
public static boolean isEligibleForConversion(@Nullable VirtualFile file, @Nullable WebpConversionSettings settings) {
if (file != null && !file.isDirectory()) {
String name = file.getName();
if (name.endsWith(DOT_PNG)) {
if (settings != null && settings.skipNinePatches && endsWithIgnoreCase(name, DOT_9PNG)) {
return false;
}
if (settings != null && settings.skipTransparentImages) {
try {
BufferedImage image = ImageIO.read(file.getInputStream());
if (image != null && ImageUtils.isNonOpaque(image)) {
return false;
}
} catch (IOException ignore) {
}
}
return true;
}
if (endsWithIgnoreCase(name, DOT_JPG) || SdkUtils.endsWith(name, DOT_JPEG)) {
return true;
}
if (endsWithIgnoreCase(name, DOT_BMP)) {
// Really?
return true;
}
if (endsWithIgnoreCase(name, DOT_GIF)) {
// Can convert only if not an animated gif (TODO: Support animated webp!)
ImageReader is = ImageIO.getImageReadersBySuffix("GIF").next();
ImageInputStream iis;
try {
iis = ImageIO.createImageInputStream(file.getInputStream());
is.setInput(iis);
return is.getNumImages(true) == 1;
} catch (IOException ignore) {
return false;
}
}
}
return false;
}
use of javax.imageio.ImageReader in project dropbox-sdk-java by dropbox.
the class DbxClientV1IT method testThumbnail.
@Test
public void testThumbnail() throws Exception {
String orig = p("test-imag" + E_ACCENT + ".jpeg");
// Upload an image.
InputStream in = this.getClass().getResourceAsStream("/test-image.jpeg");
if (in == null) {
throw new AssertionError("couldn't load test image \"test-image.jpeg\"");
}
DbxEntry.File origMD;
try {
origMD = client.uploadFile(orig, DbxWriteMode.add(), -1, in);
} finally {
IOUtil.closeInput(in);
}
BufferedImage origImage = ImageIO.read(getClass().getResource("/test-image.jpeg"));
int origW = origImage.getWidth();
int origH = origImage.getHeight();
DbxThumbnailFormat[] formats = { DbxThumbnailFormat.JPEG, DbxThumbnailFormat.PNG };
DbxThumbnailSize[] sizes = { DbxThumbnailSize.w32h32, DbxThumbnailSize.w64h64, DbxThumbnailSize.w64h64, DbxThumbnailSize.w640h480, DbxThumbnailSize.w1024h768 };
for (DbxThumbnailFormat format : formats) {
long prevSize = 0;
ImageReader reader = getImageReaderForFormat(format);
for (DbxThumbnailSize size : sizes) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
DbxEntry.File md = client.getThumbnail(size, format, orig, null, out);
byte[] data = out.toByteArray();
assertEquals(removeMediaInfo(origMD), removeMediaInfo(md));
// We're getting bigger and bigger thumbnails, so they should have more bytes
// than the previous ones.
assertTrue(data.length > prevSize);
reader.setInput(new MemoryCacheImageInputStream(new ByteArrayInputStream(data)));
int w = reader.getWidth(0);
int h = reader.getHeight(0);
int expectedW = Math.min(size.width, origW);
int expectedH = Math.min(size.width, origH);
assertTrue((w == expectedW && h <= expectedH) || (h == expectedH && w <= expectedW), "expected = " + expectedW + "x" + expectedH + ", got = " + w + "x" + h);
}
}
}
use of javax.imageio.ImageReader in project dropbox-sdk-java by dropbox.
the class DbxClientV1IT method getImageReaderForFormat.
private static ImageReader getImageReaderForFormat(DbxThumbnailFormat format) {
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(format.ident);
if (!readers.hasNext()) {
throw new AssertionError("no ImageReader for " + jq(format.ident));
}
ImageReader reader = readers.next();
if (readers.hasNext()) {
throw new AssertionError("multiple ImageReaders for " + jq(format.ident));
}
return reader;
}
Aggregations