Java Code Examples for javax.imageio.ImageReader#readAll()
The following examples show how to use
javax.imageio.ImageReader#readAll() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ITXtTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 2
Source File: ITXtTest.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 3
Source File: ITXtTest.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 4
Source File: ITXtTest.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 5
Source File: ITXtTest.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 6
Source File: ITXtTest.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 7
Source File: ITXtTest.java From hottub with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 8
Source File: ITXtTest.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 9
Source File: ITXtTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(f)) { ImageReader r = ImageIO.getImageReaders(imageInputStream).next(); r.setInput(imageInputStream); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 10
Source File: ITXtTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 11
Source File: ITXtTest.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 12
Source File: ITXtTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 13
Source File: MainPanel.java From java-swing-tips with MIT License | 5 votes |
private static List<BufferedImage> loadFromStream(ImageInputStream imageStream) throws IOException { Iterable<ImageReader> it = () -> ImageIO.getImageReaders(imageStream); ImageReader reader = StreamSupport.stream(it.spliterator(), false) .filter(MainPanel::checkGifFormat) .findFirst() .orElseThrow(() -> new IOException("Can not read image format!")); reader.setInput(imageStream, false, false); List<BufferedImage> list = new ArrayList<>(); for (int i = 0; i < reader.getNumImages(true); i++) { IIOImage frame = reader.readAll(i, null); list.add((BufferedImage) frame.getRenderedImage()); } reader.dispose(); return list; }
Example 14
Source File: ImageIOHelper.java From tess4j with Apache License 2.0 | 5 votes |
/** * Gets a list of <code>IIOImage</code> objects for an image file. * * @param inputFile input image file. It can be any of the supported * formats, including TIFF, JPEG, GIF, PNG, BMP, JPEG, and PDF if GPL * Ghostscript or PDFBox is installed * @return a list of <code>IIOImage</code> objects * @throws IOException */ public static List<IIOImage> getIIOImageList(File inputFile) throws IOException { // convert to TIFF if PDF File imageFile = getImageFile(inputFile); List<IIOImage> iioImageList = new ArrayList<IIOImage>(); String imageFormat = getImageFileFormat(imageFile); Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFormat); if (!readers.hasNext()) { throw new RuntimeException(JAI_IMAGE_READER_MESSAGE); } ImageReader reader = readers.next(); try (ImageInputStream iis = ImageIO.createImageInputStream(imageFile)) { reader.setInput(iis); int imageTotal = reader.getNumImages(true); for (int i = 0; i < imageTotal; i++) { // IIOImage oimage = new IIOImage(reader.read(i), null, reader.getImageMetadata(i)); IIOImage oimage = reader.readAll(i, reader.getDefaultReadParam()); iioImageList.add(oimage); } return iioImageList; } finally { if (reader != null) { reader.dispose(); } // delete temporary TIFF image for PDF if (imageFile != null && imageFile.exists() && imageFile != inputFile && imageFile.getName().startsWith("multipage") && imageFile.getName().endsWith(TIFF_EXT)) { imageFile.delete(); } } }
Example 15
Source File: Tesseract.java From tess4j with Apache License 2.0 | 4 votes |
/** * Performs OCR operation. * * @param inputFile an image file * @param rect the bounding rectangle defines the region of the image to be * recognized. A rectangle of zero dimension or <code>null</code> indicates * the whole image. * @return the recognized text * @throws TesseractException */ @Override public String doOCR(File inputFile, Rectangle rect) throws TesseractException { try { File imageFile = ImageIOHelper.getImageFile(inputFile); String imageFileFormat = ImageIOHelper.getImageFileFormat(imageFile); Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFileFormat); if (!readers.hasNext()) { throw new RuntimeException(ImageIOHelper.JAI_IMAGE_READER_MESSAGE); } ImageReader reader = readers.next(); StringBuilder result = new StringBuilder(); try (ImageInputStream iis = ImageIO.createImageInputStream(imageFile);) { reader.setInput(iis); int imageTotal = reader.getNumImages(true); init(); setTessVariables(); for (int i = 0; i < imageTotal; i++) { IIOImage oimage = reader.readAll(i, reader.getDefaultReadParam()); result.append(doOCR(oimage, inputFile.getPath(), rect, i + 1)); } if (String.valueOf(TRUE).equals(prop.getProperty("tessedit_create_hocr"))) { result.insert(0, htmlBeginTag).append(htmlEndTag); } } finally { // delete temporary TIFF image for PDF if (imageFile != null && imageFile.exists() && imageFile != inputFile && imageFile.getName().startsWith("multipage") && imageFile.getName().endsWith(ImageIOHelper.TIFF_EXT)) { imageFile.delete(); } reader.dispose(); dispose(); } return result.toString(); } catch (Exception e) { logger.error(e.getMessage(), e); throw new TesseractException(e); } }
Example 16
Source File: Tesseract1.java From tess4j with Apache License 2.0 | 4 votes |
/** * Performs OCR operation. * * @param inputFile an image file * @param rect the bounding rectangle defines the region of the image to be * recognized. A rectangle of zero dimension or <code>null</code> indicates * the whole image. * @return the recognized text * @throws TesseractException */ @Override public String doOCR(File inputFile, Rectangle rect) throws TesseractException { try { File imageFile = ImageIOHelper.getImageFile(inputFile); String imageFileFormat = ImageIOHelper.getImageFileFormat(imageFile); Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFileFormat); if (!readers.hasNext()) { throw new RuntimeException(ImageIOHelper.JAI_IMAGE_READER_MESSAGE); } ImageReader reader = readers.next(); StringBuilder result = new StringBuilder(); try (ImageInputStream iis = ImageIO.createImageInputStream(imageFile);) { reader.setInput(iis); int imageTotal = reader.getNumImages(true); init(); setTessVariables(); for (int i = 0; i < imageTotal; i++) { IIOImage oimage = reader.readAll(i, reader.getDefaultReadParam()); result.append(doOCR(oimage, inputFile.getPath(), rect, i + 1)); } if (String.valueOf(TRUE).equals(prop.getProperty("tessedit_create_hocr"))) { result.insert(0, htmlBeginTag).append(htmlEndTag); } } finally { // delete temporary TIFF image for PDF if (imageFile != null && imageFile.exists() && imageFile != inputFile && imageFile.getName().startsWith("multipage") && imageFile.getName().endsWith(ImageIOHelper.TIFF_EXT)) { imageFile.delete(); } reader.dispose(); dispose(); } return result.toString(); } catch (Exception e) { logger.error(e.getMessage(), e); throw new TesseractException(e); } }
Example 17
Source File: ImageIOHelper.java From tess4j with Apache License 2.0 | 4 votes |
/** * Creates a list of TIFF image files from an image file. It basically * converts images of other formats to TIFF format, or a multi-page TIFF * image to multiple TIFF image files. * * @param imageFile input image file * @param index an index of the page; -1 means all pages, as in a multi-page * TIFF image * @param preserve preserve compression mode * @return a list of TIFF image files * @throws IOException */ public static List<File> createTiffFiles(File imageFile, int index, boolean preserve) throws IOException { List<File> tiffFiles = new ArrayList<File>(); String imageFormat = getImageFileFormat(imageFile); Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFormat); if (!readers.hasNext()) { throw new RuntimeException(JAI_IMAGE_READER_MESSAGE); } ImageReader reader = readers.next(); // Get tiff writer and set output to file Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT); if (!writers.hasNext()) { throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE); } ImageWriter writer = writers.next(); try (ImageInputStream iis = ImageIO.createImageInputStream(imageFile)) { reader.setInput(iis); // Read the stream metadata // IIOMetadata streamMetadata = reader.getStreamMetadata(); // Set up the writeParam TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US); if (!preserve) { // not preserve original sizes; decompress tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED); } // Read the stream metadata IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam); int imageTotal = reader.getNumImages(true); for (int i = 0; i < imageTotal; i++) { // all if index == -1; otherwise, only index-th if (index == -1 || i == index) { IIOImage oimage = reader.readAll(i, reader.getDefaultReadParam()); File tiffFile = File.createTempFile(OUTPUT_FILE_NAME, TIFF_EXT); try (ImageOutputStream ios = ImageIO.createImageOutputStream(tiffFile)) { writer.setOutput(ios); writer.write(streamMetadata, oimage, tiffWriteParam); tiffFiles.add(tiffFile); } } } return tiffFiles; } finally { if (reader != null) { reader.dispose(); } if (writer != null) { writer.dispose(); } } }
Example 18
Source File: ImageIOHelper.java From tess4j with Apache License 2.0 | 4 votes |
/** * Merges multiple images into one multi-page TIFF image. * * @param inputImages an array of image files * @param outputTiff the output multi-page TIFF file * @throws IOException */ public static void mergeTiff(File[] inputImages, File outputTiff) throws IOException { if (inputImages.length == 0) { // if no image return; } Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT); if (!writers.hasNext()) { throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE); } ImageWriter writer = writers.next(); //Set up the writeParam TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US); // tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED); // commented out to preserve original sizes //Get the stream metadata IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam); try (ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff)) { writer.setOutput(ios); boolean firstPage = true; int index = 1; for (File inputImage : inputImages) { String imageFileFormat = getImageFileFormat(inputImage); Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFileFormat); if (!readers.hasNext()) { throw new RuntimeException(JAI_IMAGE_READER_MESSAGE); } ImageReader reader = readers.next(); try (ImageInputStream iis = ImageIO.createImageInputStream(inputImage)) { reader.setInput(iis); int imageTotal = reader.getNumImages(true); for (int i = 0; i < imageTotal; i++) { IIOImage oimage = reader.readAll(i, reader.getDefaultReadParam()); if (firstPage) { writer.write(streamMetadata, oimage, tiffWriteParam); firstPage = false; } else { writer.writeInsert(index++, oimage, tiffWriteParam); } } } finally { if (reader != null) { reader.dispose(); } } } } finally { writer.dispose(); } }
Example 19
Source File: NoExtraBytesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 3 votes |
private void read(File f) throws IOException { ImageReader reader = ImageIO.getImageReadersByFormatName("BMP").next(); ImageInputStream iis = ImageIO.createImageInputStream(new FileInputStream(f)); reader.setInput(iis); iio_dst = reader.readAll(0, reader.getDefaultReadParam()); }
Example 20
Source File: NoExtraBytesTest.java From jdk8u_jdk with GNU General Public License v2.0 | 3 votes |
private void read(File f) throws IOException { ImageReader reader = ImageIO.getImageReadersByFormatName("BMP").next(); ImageInputStream iis = ImageIO.createImageInputStream(new FileInputStream(f)); reader.setInput(iis); iio_dst = reader.readAll(0, reader.getDefaultReadParam()); }