use of javax.imageio.stream.FileImageInputStream in project TranskribusCore by Transkribus.
the class ImgUtils method readImageDimensionsWithImageIO.
public static Dimension readImageDimensionsWithImageIO(File imgFile) throws FileNotFoundException, IOException {
Dimension dim = null;
logger.debug("Loading file with imageIO...");
ImageInputStream iis = new FileImageInputStream(imgFile);
final Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
while (readers.hasNext()) {
ImageReader reader = readers.next();
try {
logger.debug("reader format = " + reader.getFormatName());
reader.setInput(iis);
final int xDim = reader.getWidth(0);
final int yDim = reader.getHeight(0);
logger.debug("Success with reader impl: " + reader.getClass().getCanonicalName());
dim = new Dimension(xDim, yDim);
} catch (Exception e) {
logger.warn("Could not read image dimensions with reader: " + reader.getFormatName() + ": " + e.getMessage());
logger.debug("Cause: ", e);
logger.debug("Reader impl: " + reader.getClass().getCanonicalName());
} finally {
reader.dispose();
}
if (dim != null) {
break;
}
}
if (dim == null) {
throw new IOException("Could not read image dimensions with ImageIO.");
}
return dim;
}
use of javax.imageio.stream.FileImageInputStream in project TranskribusCore by Transkribus.
the class ImgUtils method readImage.
/**
* Reads image in the specified image file. For multiimage tiff files, the first image is read.
*/
public static BufferedImage readImage(File imgFile) throws FileNotFoundException, IOException {
ImageInputStream iis = new FileImageInputStream(imgFile);
BufferedImage img;
try {
img = readImage(iis);
} catch (IOException e) {
throw new IOException("Could not read image: " + imgFile.getAbsolutePath(), e);
}
return img;
}
use of javax.imageio.stream.FileImageInputStream in project WorldPainter by Captain-Chaos.
the class Dimension method getImageSize.
private java.awt.Dimension getImageSize(File image) throws IOException {
String filename = image.getName();
int p = filename.lastIndexOf('.');
if (p == -1) {
return null;
}
String suffix = filename.substring(p + 1).toLowerCase();
Iterator<ImageReader> readers = ImageIO.getImageReadersBySuffix(suffix);
if (readers.hasNext()) {
ImageReader reader = readers.next();
try {
try (ImageInputStream in = new FileImageInputStream(image)) {
reader.setInput(in);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
return new java.awt.Dimension(width, height);
}
} finally {
reader.dispose();
}
} else {
return null;
}
}
use of javax.imageio.stream.FileImageInputStream in project OpenOLAT by OpenOLAT.
the class ImageHelperImpl method scaleImage.
/**
* @param image the image to scale
* @param scaledImaged the new scaled image
* @param maxSize the maximum size (height or width) of the new scaled image
* @return
*/
@Override
public Size scaleImage(File image, String imageExt, VFSLeaf scaledImage, int maxWidth, int maxHeight) {
ImageInputStream imageIns = null;
OutputStream bos = new BufferedOutputStream(scaledImage.getOutputStream(false));
try {
imageIns = new FileImageInputStream(image);
SizeAndBufferedImage scaledSize = calcScaledSize(imageIns, imageExt, maxWidth, maxHeight, false);
if (scaledSize == null) {
return null;
}
if (!scaledSize.getScaledSize().isChanged() && isSameFormat(image, scaledImage)) {
InputStream cloneIns = new FileInputStream(image);
IOUtils.copy(cloneIns, bos);
IOUtils.closeQuietly(cloneIns);
return scaledSize.getScaledSize();
} else {
BufferedImage imageSrc = scaledSize.getImage();
if (imageSrc == null) {
// unsupported image format
return null;
}
BufferedImage scaledBufferedImage = scaleTo(imageSrc, scaledSize.getScaledSize());
if (writeTo(scaledBufferedImage, bos, scaledSize.getScaledSize(), getImageFormat(scaledImage))) {
return scaledSize.getScaledSize();
}
return null;
}
} catch (IOException e) {
return null;
} finally {
closeQuietly(imageIns);
FileUtils.closeSafely(bos);
}
}
use of javax.imageio.stream.FileImageInputStream in project openmrs-core by openmrs.
the class ImageHandler method getObs.
/**
* Currently supports all views and puts the Image file data into the ComplexData object
*
* @see org.openmrs.obs.ComplexObsHandler#getObs(org.openmrs.Obs, java.lang.String)
*/
@Override
public Obs getObs(Obs obs, String view) {
File file = getComplexDataFile(obs);
// Raw image
if (ComplexObsHandler.RAW_VIEW.equals(view)) {
BufferedImage img = null;
try {
img = ImageIO.read(file);
} catch (IOException e) {
log.error("Trying to read file: " + file.getAbsolutePath(), e);
}
ComplexData complexData = new ComplexData(file.getName(), img);
String mimeType = null;
// Image MIME type
try {
FileImageInputStream imgStream = new FileImageInputStream(file);
Iterator<ImageReader> imgReader = ImageIO.getImageReaders(imgStream);
imgStream.close();
if (imgReader.hasNext()) {
mimeType = "image/" + imgReader.next().getFormatName().toLowerCase();
} else {
log.warn("MIME type of " + file.getAbsolutePath() + " is not known");
}
} catch (FileNotFoundException e) {
log.error("Image " + file.getAbsolutePath() + " was not found", e);
} catch (IOException e) {
log.error("Trying to determine MIME type of " + file.getAbsolutePath(), e);
}
// If the mimetype is still null, determine it via getFileMimeType()
mimeType = mimeType != null ? mimeType : OpenmrsUtil.getFileMimeType(file);
complexData.setMimeType(mimeType);
obs.setComplexData(complexData);
} else {
// NOTE: if adding support for another view, don't forget to update supportedViews list above
return null;
}
return obs;
}
Aggregations