use of javax.imageio.ImageReader in project tika by apache.
the class ImageParser method parse.
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
String type = metadata.get(Metadata.CONTENT_TYPE);
if (type != null) {
// fix it up to the new one, so Java is happy
if (OLD_BMP_TYPE.toString().equals(type)) {
type = MAIN_BMP_TYPE.toString();
}
try {
Iterator<ImageReader> iterator = ImageIO.getImageReadersByMIMEType(type);
if (iterator.hasNext()) {
ImageReader reader = iterator.next();
try {
try (ImageInputStream imageStream = ImageIO.createImageInputStream(new CloseShieldInputStream(stream))) {
reader.setInput(imageStream);
metadata.set(Metadata.IMAGE_WIDTH, Integer.toString(reader.getWidth(0)));
metadata.set(Metadata.IMAGE_LENGTH, Integer.toString(reader.getHeight(0)));
metadata.set("height", Integer.toString(reader.getHeight(0)));
metadata.set("width", Integer.toString(reader.getWidth(0)));
loadMetadata(reader.getImageMetadata(0), metadata);
}
} finally {
reader.dispose();
}
}
// Translate certain Metadata tags from the ImageIO
// specific namespace into the general Tika one
setIfPresent(metadata, "CommentExtensions CommentExtension", TikaCoreProperties.COMMENTS);
setIfPresent(metadata, "markerSequence com", TikaCoreProperties.COMMENTS);
setIfPresent(metadata, "Data BitsPerSample", Metadata.BITS_PER_SAMPLE);
} catch (IIOException e) {
// which Tika will just ignore.
if (!(e.getMessage() != null && e.getMessage().equals("Unexpected block type 0!") && type.equals("image/gif"))) {
throw new TikaException(type + " parse error", e);
}
}
}
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
xhtml.startDocument();
xhtml.endDocument();
}
use of javax.imageio.ImageReader in project fess by codelibs.
the class WebDriverGenerator method convert.
protected void convert(final File inputFile, final File outputFile) {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
try (ImageInputStream input = ImageIO.createImageInputStream(inputFile)) {
final Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
if (readers.hasNext()) {
final ImageReader reader = readers.next();
try {
reader.setInput(input);
final ImageReadParam param = reader.getDefaultReadParam();
final int samplingWidth = reader.getWidth(0) / fessConfig.getThumbnailHtmlPhantomjsThumbnailWidthAsInteger();
final int samplingHeight = reader.getHeight(0) / fessConfig.getThumbnailHtmlPhantomjsThumbnailHeightAsInteger();
param.setSourceSubsampling(samplingWidth, samplingHeight, 0, 0);
param.setSourceRegion(new Rectangle(fessConfig.getThumbnailHtmlPhantomjsWindowWidthAsInteger(), fessConfig.getThumbnailHtmlPhantomjsThumbnailHeightAsInteger() * reader.getHeight(0) / fessConfig.getThumbnailHtmlPhantomjsThumbnailWidthAsInteger()));
final BufferedImage image = reader.read(0, param);
ImageIO.write(image, fessConfig.getThumbnailHtmlPhantomjsFormat(), outputFile);
image.flush();
} finally {
reader.dispose();
}
}
} catch (final Throwable t) {
logger.warn("Failed to convert " + inputFile.getAbsolutePath(), t);
inputFile.renameTo(outputFile);
}
}
use of javax.imageio.ImageReader in project fess by codelibs.
the class HtmlTagBasedGenerator method saveImage.
protected boolean saveImage(final ImageInputStream input, final File outputFile) throws IOException {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
if (readers.hasNext()) {
final ImageReader reader = readers.next();
try {
reader.setInput(input);
final ImageReadParam param = reader.getDefaultReadParam();
final int width = reader.getWidth(0);
final int height = reader.getHeight(0);
final int samplingWidth = width / fessConfig.getThumbnailHtmlImageThumbnailWidthAsInteger();
final int samplingHeight = height / fessConfig.getThumbnailHtmlImageThumbnailHeightAsInteger();
param.setSourceSubsampling(samplingWidth <= 0 ? 1 : samplingWidth, samplingHeight <= 0 ? 1 : samplingHeight, 0, 0);
param.setSourceRegion(new Rectangle(width, height > width ? width : height));
final BufferedImage image = reader.read(0, param);
final int thumbnailWidth = fessConfig.getThumbnailHtmlImageThumbnailWidthAsInteger();
final int thumbnailHeight = (int) (((float) (height > width ? width : height)) * fessConfig.getThumbnailHtmlImageThumbnailWidthAsInteger().floatValue() / (float) width);
BufferedImage thumbnail = new BufferedImage(thumbnailWidth, thumbnailHeight, image.getType());
thumbnail.getGraphics().drawImage(image.getScaledInstance(thumbnailWidth, thumbnailHeight, Image.SCALE_AREA_AVERAGING), 0, 0, thumbnailWidth, thumbnailHeight, null);
ImageIO.write(thumbnail, fessConfig.getThumbnailHtmlImageFormat(), outputFile);
image.flush();
return true;
} finally {
reader.dispose();
}
}
return false;
}
Aggregations