Package javax.imageio

Examples of javax.imageio.ImageReader


        if (type != null) {
            try {
                Iterator<ImageReader> iterator =
                    ImageIO.getImageReadersByMIMEType(type);
                if (iterator.hasNext()) {
                    ImageReader reader = iterator.next();
                    reader.setInput(ImageIO.createImageInputStream(
                            new CloseShieldInputStream(stream)));
                    metadata.set("height", Integer.toString(reader.getHeight(0)));
                    metadata.set("width", Integer.toString(reader.getWidth(0)));
                    reader.dispose();
                }
            } catch (IIOException e) {
                throw new TikaException(type + " parse error", e);
            }
        }
View Full Code Here


       */
      MemoryCacheImageInputStream input = new MemoryCacheImageInputStream(
          imageUrl.openStream());
      Iterator<ImageReader> it = ImageIO.getImageReaders(input);
      readers : while (it.hasNext()) {
        ImageReader reader = it.next();
        reader.setInput(input);

        int numImages = reader.getNumImages(true);
        if (numImages == 0) {
          // Fall through

        } else if (numImages == 1) {
          try {
            image = reader.read(0);
            IIOMetadata metadata = reader.getImageMetadata(0);
            if (metadata != null
                && metadata.isStandardMetadataFormatSupported()) {
              // http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/metadata/doc-files/standard_metadata.html
              Node data = metadata.getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);
              metadata : for (int i = 0, j = data.getChildNodes().getLength(); i < j; i++) {
                Node child = data.getChildNodes().item(i);
                if (child.getLocalName().equalsIgnoreCase("compression")) {
                  for (int k = 0, l = child.getChildNodes().getLength(); k < l; k++) {
                    Node child2 = child.getChildNodes().item(k);
                    if (child2.getLocalName().equalsIgnoreCase("lossless")) {
                      Node value = child2.getAttributes().getNamedItem("value");
                      if (value == null) {
                        // The default is true, according to the DTD
                        lossy = false;
                      } else {
                        lossy = !Boolean.parseBoolean(value.getNodeValue());
                      }
                      break metadata;
                    }
                  }
                }
              }
            }
          } catch (Exception e) {
            // Hope we have another reader that can handle the image
            continue readers;
          }

        } else {
          // Read all contained images
          BufferedImage[] images = new BufferedImage[numImages];

          try {
            for (int i = 0; i < numImages; i++) {
              images[i] = reader.read(i);
            }
          } catch (Exception e) {
            // Hope we have another reader that can handle the image
            continue readers;
          }
View Full Code Here

    //--------------------  VALIDATE THE IMAGE -----------------------------------------
      // check width and heigh of image
      logger.debug(importFileName + " ext = " + getFileExtensionForImageReader(importFileName));
      Iterator readers = ImageIO.getImageReadersBySuffix(getFileExtensionForImageReader(importFileName));
      ImageReader reader = (ImageReader) readers.next();
  
       try {
          ImageInputStream iis = ImageIO.createImageInputStream(importFile.getInputStream());
          reader.setInput(iis, true);
          int width = reader.getWidth(0);
          int height = reader.getHeight(0);
          logger.debug(importFile.getFileName() + ": width=" + width + ", height=" + height);
          if (width > maxWidth || height >  maxHeight){
              errors.add(BaseConstants.WARN_KEY,new ActionMessage("error.dimensions", width, height, maxWidth, maxHeight ));
              saveMessages(request, errors);
              return mapping.getInputForward();           
View Full Code Here

        msgs.add(BaseConstants.WARN_KEY, new ActionMessage("error.imageext", SystemConfigConstants.CONTENT_TYPE));
      }
      else{           
              logger.debug(fileName + " ext = " + getFileExtensionForImageReader(fileName));
              Iterator readers = ImageIO.getImageReadersBySuffix(getFileExtensionForImageReader(fileName));
              ImageReader reader = (ImageReader) readers.next();
          
               try {
                  ImageInputStream iis = ImageIO.createImageInputStream(uploadedFile.getInputStream());
                  reader.setInput(iis, true);
                  int width = reader.getWidth(0);
                  int height = reader.getHeight(0);
                  logger.debug(uploadedFile.getFileName() + ": width=" + width + ", height=" + height);
                 
              if (validateHeight){
                      if (height >  maxHeight){
                        msgs.add(BaseConstants.WARN_KEY,new ActionMessage("error.heightdimensions", height, maxHeight ));           
View Full Code Here

    public Object getContent(DataSource ds) throws IOException {
        Iterator i = ImageIO.getImageReadersByMIMEType(ds.getContentType());
        if (!i.hasNext()) {
            throw new UnsupportedDataTypeException();
        }
        ImageReader reader = (ImageReader) i.next();
        return reader.read(0);
    }
View Full Code Here

    @Override
    public final DecodeResult decode(InputStream encoded, OutputStream decoded,
                                         COSDictionary parameters, int index) throws IOException
    {
        ImageReader reader = findImageReader("JBIG2", "jbig2-imageio is not installed");
        DecodeResult result = new DecodeResult(new COSDictionary());
        result.getParameters().addAll(parameters);

        COSInteger bits = (COSInteger) parameters.getDictionaryObject(COSName.BITS_PER_COMPONENT);
        COSDictionary params = (COSDictionary) parameters.getDictionaryObject(COSName.DECODE_PARMS);

        COSStream globals = null;
        if (params != null)
        {
            globals = (COSStream) params.getDictionaryObject(COSName.JBIG2_GLOBALS);
        }

        ImageInputStream iis = null;
        try
        {
            if (globals != null)
            {
                iis = ImageIO.createImageInputStream(
                        new SequenceInputStream(globals.getUnfilteredStream(), encoded));
                reader.setInput(iis);
            }
            else
            {
                iis = ImageIO.createImageInputStream(encoded);
                reader.setInput(iis);
            }

            BufferedImage image;
            try
            {
                image = reader.read(0);
            }
            catch (Exception e)
            {
                // wrap and rethrow any exceptions
                throw new IOException("Could not read JBIG2 image", e);
            }

            // I am assuming since JBIG2 is always black and white
            // depending on your renderer this might or might be needed
            if (image.getColorModel().getPixelSize() != bits.intValue())
            {
                if (bits.intValue() != 1)
                {
                    LOG.warn("Attempting to handle a JBIG2 with more than 1-bit depth");
                }
                BufferedImage packedImage = new BufferedImage(image.getWidth(), image.getHeight(),
                        BufferedImage.TYPE_BYTE_BINARY);
                Graphics graphics = packedImage.getGraphics();
                graphics.drawImage(image, 0, 0, null);
                graphics.dispose();
                image = packedImage;
            }

            DataBuffer dBuf = image.getData().getDataBuffer();
            if (dBuf.getDataType() == DataBuffer.TYPE_BYTE)
            {
                decoded.write(((DataBufferByte) dBuf).getData());
            }
            else
            {
                throw new IOException("Unexpected image buffer type");
            }
        }
        finally
        {
            if (iis != null)
            {
                iis.close();
            }
            reader.dispose();
        }

        // repair missing color space
        if (!parameters.containsKey(COSName.COLORSPACE))
        {
View Full Code Here

    }

    // try to read using JAI Image I/O
    private BufferedImage readJPX(InputStream input, DecodeResult result) throws IOException
    {
        ImageReader reader = findImageReader("JPEG2000", "Java Advanced Imaging (JAI) Image I/O Tools are not installed");
        ImageInputStream iis = null;
        try
        {
            iis = ImageIO.createImageInputStream(input);
            reader.setInput(iis, true, true);

            BufferedImage image;
            try
            {
                image = reader.read(0);
            }
            catch (Exception e)
            {
                // wrap and rethrow any exceptions
                throw new IOException("Could not read JPEG 2000 (JPX) image", e);
            }

            COSDictionary parameters = result.getParameters();

            // "If the image stream uses the JPXDecode filter, this entry is optional
            // and shall be ignored if present"
            parameters.setInt(COSName.BITS_PER_COMPONENT, image.getColorModel().getComponentSize(0));

            // "Decode shall be ignored, except in the case where the image is treated as a mask"
            if (!parameters.getBoolean(COSName.IMAGE_MASK, false))
            {
                parameters.setItem(COSName.DECODE, null);
            }

            // override dimensions, see PDFBOX-1735
            parameters.setInt(COSName.WIDTH, image.getWidth());
            parameters.setInt(COSName.HEIGHT, image.getHeight());

            // extract embedded color space
            if (!parameters.containsKey(COSName.COLORSPACE))
            {
                result.setColorSpace(new PDJPXColorSpace(image.getColorModel().getColorSpace()));
            }

            return image;
        }
        finally
        {
            if (iis != null)
            {
                iis.close();
            }
            reader.dispose();
        }
    }
View Full Code Here

     * @throws MissingImageReaderException if no image reader is found.
     */
    protected ImageReader findImageReader(String formatName, String errorCause) throws MissingImageReaderException
    {
        Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(formatName);
        ImageReader reader = null;
        while (readers.hasNext())
        {
            reader = readers.next();
            if (reader.canReadRaster())
            {
                break;
            }
        }
        if (reader == null)
View Full Code Here

            checkBmpResolution(filename, expectedResolution);
            return;
        }
        Iterator readers = ImageIO.getImageReadersBySuffix(suffix);
        assertTrue("No image reader found for suffix " + suffix, readers.hasNext());
        ImageReader reader = (ImageReader) readers.next();
        ImageInputStream iis = ImageIO.createImageInputStream(new File(filename));
        assertNotNull("No ImageInputStream created for file " + filename, iis);
        reader.setInput(iis);
        IIOMetadata imageMetadata = reader.getImageMetadata(0);
        Element root = (Element) imageMetadata.getAsTree(STANDARD_METADATA_FORMAT);
        NodeList dimensionNodes = root.getElementsByTagName("Dimension");
        assertTrue("No resolution found in image file " + filename, dimensionNodes.getLength() > 0);
        Element dimensionElement = (Element) dimensionNodes.item(0);

        NodeList pixelSizeNodes = dimensionElement.getElementsByTagName("HorizontalPixelSize");
        assertTrue("No X resolution found in image file " + filename, pixelSizeNodes.getLength() > 0);
        Node pixelSizeNode = pixelSizeNodes.item(0);
        String val = pixelSizeNode.getAttributes().getNamedItem("value").getNodeValue();
        int actualResolution = (int) Math.round(25.4 / Double.parseDouble(val));
        assertEquals("X resolution doesn't match in image file " + filename, expectedResolution, actualResolution);

        pixelSizeNodes = dimensionElement.getElementsByTagName("VerticalPixelSize");
        assertTrue("No Y resolution found in image file " + filename, pixelSizeNodes.getLength() > 0);
        pixelSizeNode = pixelSizeNodes.item(0);
        val = pixelSizeNode.getAttributes().getNamedItem("value").getNodeValue();
        actualResolution = (int) Math.round(25.4 / Double.parseDouble(val));
        assertEquals("Y resolution doesn't match", expectedResolution, actualResolution);

        iis.close();
        reader.dispose();
    }
View Full Code Here

     * @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();
    }
View Full Code Here

TOP

Related Classes of javax.imageio.ImageReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.