use of javax.imageio.stream.MemoryCacheImageInputStream 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.stream.MemoryCacheImageInputStream in project limelight by slagyr.
the class ImagePanel method setData.
public void setData(byte[] bytes) throws Exception {
ImageInputStream imageInput = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes));
setImage(ImageIO.read(imageInput));
filename = "[DATA]";
markAsNeedingLayout();
getParent().markAsNeedingLayout();
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project poi by apache.
the class BitmapImageRenderer method readImage.
/**
* Read the image data via ImageIO and optionally try to workaround metadata errors.
* The resulting image is of image type {@link BufferedImage#TYPE_INT_ARGB}
*
* @param data the data stream
* @param contentType the content type
* @return the bufferedImage or null, if there was no image reader for this content type
* @throws IOException thrown if there was an error while processing the image
*/
private static BufferedImage readImage(InputStream data, String contentType) throws IOException {
IOException lastException = null;
BufferedImage img = null;
if (data.markSupported()) {
data.mark(data.available());
}
// currently don't use FileCacheImageInputStream,
// because of the risk of filling the file handles (see #59166)
ImageInputStream iis = new MemoryCacheImageInputStream(data);
try {
iis = new MemoryCacheImageInputStream(data);
iis.mark();
Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
while (img == null && iter.hasNext()) {
ImageReader reader = iter.next();
ImageReadParam param = reader.getDefaultReadParam();
// 0:default mode, 1:fallback mode
for (int mode = 0; img == null && mode < 3; mode++) {
lastException = null;
try {
iis.reset();
} catch (IOException e) {
if (data.markSupported()) {
data.reset();
data.mark(data.available());
iis.close();
iis = new MemoryCacheImageInputStream(data);
} else {
// can't restore the input stream, so we need to stop processing here
lastException = e;
break;
}
}
iis.mark();
try {
switch(mode) {
case 0:
reader.setInput(iis, false, true);
img = reader.read(0, param);
break;
case 1:
{
// try to load picture in gray scale mode
// fallback mode for invalid image band metadata
// see http://stackoverflow.com/questions/10416378
Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
while (imageTypes.hasNext()) {
ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
param.setDestinationType(imageTypeSpecifier);
break;
}
}
reader.setInput(iis, false, true);
img = reader.read(0, param);
break;
}
case 2:
{
// try to load truncated pictures by supplying a BufferedImage
// and use the processed data up till the point of error
reader.setInput(iis, false, true);
int height = reader.getHeight(0);
int width = reader.getWidth(0);
Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
if (imageTypes.hasNext()) {
ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
img = imageTypeSpecifier.createBufferedImage(width, height);
param.setDestination(img);
} else {
lastException = new IOException("unable to load even a truncated version of the image.");
break;
}
try {
reader.read(0, param);
} finally {
if (img.getType() != BufferedImage.TYPE_INT_ARGB) {
int y = findTruncatedBlackBox(img, width, height);
if (y < height) {
BufferedImage argbImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = argbImg.createGraphics();
g.clipRect(0, 0, width, y);
g.drawImage(img, 0, 0, null);
g.dispose();
img.flush();
img = argbImg;
}
}
}
break;
}
}
} catch (IOException e) {
if (mode < 2) {
lastException = e;
}
} catch (RuntimeException e) {
if (mode < 2) {
lastException = new IOException("ImageIO runtime exception - " + (mode == 0 ? "normal" : "fallback"), e);
}
}
}
reader.dispose();
}
} finally {
iis.close();
}
// If you don't have an image at the end of all readers
if (img == null) {
if (lastException != null) {
// multiple locations above ...
throw lastException;
}
LOG.log(POILogger.WARN, "Content-type: " + contentType + " is not support. Image ignored.");
return null;
}
// add alpha channel
if (img.getType() != BufferedImage.TYPE_INT_ARGB) {
BufferedImage argbImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = argbImg.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return argbImg;
}
return img;
}
Aggregations