List of usage examples for javax.imageio.stream ImageInputStream close
void close() throws IOException;
From source file:org.artifactory.common.wicket.panel.upload.LogoFileUploadForm.java
/** * check if the image has fake format , its not a real image * this check done to eliminate security issue * * @return/* w w w .j a va 2s.c om*/ * @throws IOException */ private boolean isImageFake() throws Exception { boolean isFakeImage = false; ImageInputStream imageInputStream = null; try { Path path = Paths.get(uploadedFile.getCanonicalPath()); byte[] data = Files.readAllBytes(path); imageInputStream = ImageIO.createImageInputStream(new ByteArrayInputStream(data)); Iterator<ImageReader> iter = ImageIO.getImageReaders(imageInputStream); if (!iter.hasNext()) { isFakeImage = true; } } catch (Exception e) { throw new Exception(e); } finally { if (imageInputStream != null) { try { imageInputStream.close(); } catch (IOException e) { throw new IOException(e); } } } return isFakeImage; }
From source file:net.filterlogic.util.imaging.ToTIFF.java
/** * Convert multipage TIFF to single page TIFF. * @param srcFiles Array of source files to convert. * @param destPath Folder to store single page TIFFs in. * @param archivePath Path to move source TIFF files to after single page TIFFs created. * @param pattern Pattern of single page TIFF file names. Java NumberFormatter used with page number to create file name. * @param multipage Set to true if source TIFFs should be coverted to multi-page TIFF. * @param dpi DPI to set TIFFs to.//w w w . ja v a 2 s.co m * @return Returns a list of files in destination path. * @throws net.filterlogic.util.imaging.OpenCaptureImagingException */ public static List toTIFF(String[] srcFiles, String destPath, String archivePath, String pattern, boolean multipage, int dpi) throws OpenCaptureImagingException { String pathSep = System.getProperty("file.separator"); boolean jaiSupport = true; int pageCount = 0; int fileNameCount = 0; byte[] imageData = null; // make sure destpath has trailing slash. if (destPath.lastIndexOf(pathSep) != destPath.length() - 1) destPath += pathSep; // create path if doesn't exist if (!Path.ValidatePath(destPath)) if (!Path.createPath(destPath)) throw new OpenCaptureImagingException( "Unable to create destination path for imported images [" + destPath + "]"); // make sure archivePath has trailing slash if (archivePath.lastIndexOf(pathSep) != archivePath.length() - 1) archivePath += pathSep; if (!Path.ValidatePath(archivePath)) if (!Path.createPath(archivePath)) throw new OpenCaptureImagingException( "Unable to create archive path for imported images [" + archivePath + "]"); // set a default pattern if one not passed. if (pattern.trim().length() < 1) pattern = "#"; NumberFormat formatter = new DecimalFormat(pattern); ArrayList<String> list = new ArrayList<String>(); for (int i = 0; i < srcFiles.length; i++) { try { File f = new File(srcFiles[i]); imageData = null; ImageIO.setUseCache(false); ImageInputStream imageInputStream = ImageIO.createImageInputStream(f); java.util.Iterator readers = ImageIO.getImageReaders(imageInputStream); ImageReader reader1 = null; if (readers.hasNext()) { reader1 = (ImageReader) readers.next(); jaiSupport = true; } else jaiSupport = false; if (jaiSupport) { //ImageInputStream iis = ImageIO.createImageInputStream(new FileInputStream(f)); reader1.setInput(imageInputStream); pageCount = reader1.getNumImages(true); } else { String newFileName = bigEndian2LittleEndian(f.getAbsolutePath()); if (imageInputStream != null) { imageInputStream.flush(); imageInputStream.close(); reader1.setInput(imageInputStream); pageCount = reader1.getNumImages(true); } imageInputStream = ImageIO.createImageInputStream(new File(newFileName)); readers = ImageIO.getImageReaders(imageInputStream); } // Iterator writers = ImageIO.getImageWritersByFormatName("tiff"); // ImageWriter writer = (ImageWriter)writers.next(); //ImageWriteParam param = writer.getDefaultWriteParam(); //param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); //String[] legalTypes = param.getCompressionTypes(); //param.setCompressionType("PackBits"); //ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ImageOutputStream ios = null; BufferedImage img = null; // break out each page to single file for (int t = 0; t < pageCount; t++) { // format filenumber String tifName = destPath + formatter.format(fileNameCount) + ".tif"; while (new File(tifName).exists()) { tifName = destPath + formatter.format(++fileNameCount) + ".tif"; } FileOutputStream file = new FileOutputStream(new File(tifName)); if (jaiSupport) { img = reader1.read(t); IIOImage iioimg = reader1.readAll(t, null); //ios = ImageIO.createImageOutputStream(file); //IIOMetadata iiom = getMetadata(writer, img, null, 200); } else { img = loadTIFF(imageData, t); } TIFFEncodeParam tep = setEncoder(TIFFEncodeParam.COMPRESSION_PACKBITS, 200); ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", file, tep); encoder.encode(img); //boolean ok = ImageIO.write(img, "tiff", ios); //writer.setOutput(ios); //writer.write(iiom, iioimg, null); img.flush(); //ios.flush(); //ios.close(); // ios = null; //iioimg = null; //iiom = null; img = null; //writer.dispose(); //byteOut.flush(); file.close(); file = null; //System.out.println("Add file!"); list.add(tifName); } if (jaiSupport) { reader1.dispose(); } readers = null; // writer.dispose(); // writers = null; imageInputStream.flush(); imageInputStream.close(); imageInputStream = null; f = null; // move file with overwrite if (!net.filterlogic.io.FileAccess.Move(srcFiles[i], archivePath, true)) throw new Exception("Unable to move input file to archive path [" + srcFiles[i] + "] to [" + archivePath + "]"); } catch (Exception e) { throw new OpenCaptureImagingException(e.toString()); } } return list; }
From source file:net.algart.simagis.imageio.IIOMetadataToJsonConverter.java
private JSONObject extendedReadExif(InputStream inputStream) throws IOException, JSONException { assert inputStream != null; ImageInputStream iis = ImageIO.createImageInputStream(inputStream); Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); try {// w w w . j a v a 2 s . c o m if (!readers.hasNext()) { return null; // no readers to read such "Exif" } ImageReader reader = readers.next(); try { reader.setInput(iis); return toJson(reader.getImageMetadata(0)); } finally { reader.dispose(); } } finally { iis.close(); } }
From source file:org.exoplatform.wcm.ext.component.activity.FileUIActivity.java
protected int getImageWidth(Node node) { int imageWidth = 0; try {//from ww w . j a v a2s . com if (node.hasNode(NodetypeConstant.JCR_CONTENT)) node = node.getNode(NodetypeConstant.JCR_CONTENT); ImageReader reader = ImageIO.getImageReadersByMIMEType(mimeType).next(); ImageInputStream iis = ImageIO.createImageInputStream(node.getProperty("jcr:data").getStream()); reader.setInput(iis, true); imageWidth = reader.getWidth(0); iis.close(); reader.dispose(); } catch (Exception e) { LOG.info("Cannot get node"); } return imageWidth; }
From source file:org.exoplatform.wcm.ext.component.activity.FileUIActivity.java
protected int getImageHeight(Node node) { int imageHeight = 0; try {//from w w w.j a v a 2s. c o m if (node.hasNode(NodetypeConstant.JCR_CONTENT)) node = node.getNode(NodetypeConstant.JCR_CONTENT); ImageReader reader = ImageIO.getImageReadersByMIMEType(mimeType).next(); ImageInputStream iis = ImageIO.createImageInputStream(node.getProperty("jcr:data").getStream()); reader.setInput(iis, true); imageHeight = reader.getHeight(0); iis.close(); reader.dispose(); } catch (Exception e) { LOG.info("Cannot get node"); } return imageHeight; }
From source file:org.dcm4che.tool.dcm2jpg.Dcm2Jpg.java
public void convert(File src, File dest) throws IOException { ImageInputStream iis = ImageIO.createImageInputStream(src); try {//from w w w . j av a 2 s . com BufferedImage bi = readImage(iis); bi = convert(bi); dest.delete(); ImageOutputStream ios = ImageIO.createImageOutputStream(dest); try { writeImage(ios, bi); } finally { try { ios.close(); } catch (IOException ignore) { } } } finally { try { iis.close(); } catch (IOException ignore) { } } }
From source file:com.gargoylesoftware.htmlunit.html.HtmlImage.java
private void readImageIfNeeded() throws IOException { downloadImageIfNeeded();/*from www . j a va 2 s. c o m*/ if (imageData_ == null) { if (null == imageWebResponse_) { throw new IOException("No image response available (src=" + getSrcAttribute() + ")"); } final ImageInputStream iis = ImageIO.createImageInputStream(imageWebResponse_.getContentAsStream()); final Iterator<ImageReader> iter = ImageIO.getImageReaders(iis); if (!iter.hasNext()) { iis.close(); throw new IOException("No image detected in response"); } final ImageReader imageReader = iter.next(); imageReader.setInput(iis); imageData_ = new ImageData(imageReader); // dispose all others while (iter.hasNext()) { iter.next().dispose(); } } }
From source file:tilt.image.Picture.java
/** * Get the format of the picture// w ww.jav a2s . co m * @return a mime type * @throws ImageException */ final String getFormatName() throws ImageException { try { ImageInputStream iis = ImageIO.createImageInputStream(orig); Iterator<ImageReader> iter = ImageIO.getImageReaders(iis); if (!iter.hasNext()) { throw new RuntimeException("No readers found for " + id); } ImageReader reader = iter.next(); iis.close(); return reader.getFormatName(); } catch (Exception e) { throw new ImageException(e); } }
From source file:org.apache.pdfbox.tools.imageio.TestImageIOUtils.java
/** * checks whether the compression of a TIFF file is as expected. * * @param filename Filename// ww w . j a v a2 s . c o m * @param expectedCompression expected TIFF compression * * @throws IOException if something goes wrong */ void checkTiffCompression(String filename, String expectedCompression) throws IOException { Iterator readers = ImageIO.getImageReadersBySuffix("tiff"); ImageReader reader = (ImageReader) readers.next(); ImageInputStream iis = ImageIO.createImageInputStream(new File(filename)); reader.setInput(iis); IIOMetadata imageMetadata = reader.getImageMetadata(0); Element root = (Element) imageMetadata.getAsTree(STANDARD_METADATA_FORMAT); Element comprElement = (Element) root.getElementsByTagName("Compression").item(0); Node comprTypeNode = comprElement.getElementsByTagName("CompressionTypeName").item(0); String actualCompression = comprTypeNode.getAttributes().getNamedItem("value").getNodeValue(); assertEquals("Incorrect TIFF compression in file " + filename, expectedCompression, actualCompression); iis.close(); reader.dispose(); }
From source file:jails.http.converter.BufferedImageHttpMessageConverter.java
public BufferedImage read(Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { ImageInputStream imageInputStream = null; ImageReader imageReader = null; try {/* www .ja v a 2 s . c o m*/ imageInputStream = createImageInputStream(inputMessage.getBody()); MediaType contentType = inputMessage.getHeaders().getContentType(); Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString()); if (imageReaders.hasNext()) { imageReader = imageReaders.next(); ImageReadParam irp = imageReader.getDefaultReadParam(); process(irp); imageReader.setInput(imageInputStream, true); return imageReader.read(0, irp); } else { throw new HttpMessageNotReadableException( "Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]"); } } finally { if (imageReader != null) { imageReader.dispose(); } if (imageInputStream != null) { try { imageInputStream.close(); } catch (IOException ex) { // ignore } } } }