Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extracting info from a node into a buffered reader

Status
Not open for further replies.

dexter195

Programmer
Jun 18, 2003
220
EU
hi,
im using nodes to store the metadata contained in a png file. im adding other metadata to this and then i want to create a file with this information.

im having problems with the buffered reader.
ive tried the two following approaches with no success. its not allowing me to convert the node into an object or a file.

java.lang.ClassCastException
Code:
Iterator iterReader = ImageIO.getImageReadersByFormatName( "PNG" );
	        ImageReader imageReaders = (ImageReader)iterReader.next();
	        
	        ImageInputStream iisInput = ImageIO.createImageInputStream((Object) nodeImageMetaData);
	        imageReaders.setInput(iisInput, false);

BufferedImage biInput = ImageIO.read(iisInput);

and also
Code:
BufferedImage biInput = ImageIO.read((File)nodeImageMetaData);

thanks for your help

dex
 
My guess is that your class nodeImageMetaData is not acceptable to the createImageInputStream method. What class is it ?

Eg, this code works fine :

Code:
Iterator iterReader = ImageIO.getImageReadersByFormatName( "PNG" );
ImageReader imageReaders = (ImageReader)iterReader.next();
ImageInputStream iisInput = ImageIO.createImageInputStream(new File("C:/test.png"));
imageReaders.setInput(iisInput, false);
BufferedImage biInput = ImageIO.read(iisInput);
System.out.println(biInput);

--------------------------------------------------
Free Database Connection Pooling Software
 
hi sedj,
the nodeImageMetaData is an instance of the node class. ive been trying to find a way of converting the node into a file (because it contains all the information about the .png image file) but with no success.

do you know if its possible to do this?

thanks again

dex
 
I'm a little confused, by several things :

- What is a "node class" ? Do you mean org.w3c.dom.Node or something else ?

- If you have this image meta data as some kind of name=value type thing, then this is surely not an actual PNG - it is data about a PNG, so why are you trying to use imaging libraries to try and load something that is clearly not an image ?

- To load an image, you are doing quite a lot of work. If you look at the ImageIO source code, all of what you do, is done for you, so all you really need to do is :

Code:
BufferedImage biInput = ImageIO.read(new FileInputStream("myimage.png"));

Below is the relevant javax.imageio.ImageIO code :
Code:
    /**
     * Returns a <code>BufferedImage</code> as the result of decoding
     * a supplied <code>InputStream</code> with an <code>ImageReader</code>
     * chosen automatically from among those currently registered.
     * The <code>InputStream</code> is wrapped in an
     * <code>ImageInputStream</code>.  If no registered
     * <code>ImageReader</code> claims to be able to read the
     * resulting stream, <code>null</code> is returned.
     *
     * <p> The current cache settings from <code>getUseCache</code>and
     * <code>getCacheDirectory</code> will be used to control caching in the
     * <code>ImageInputStream</code> that is created.
     *
     * <p> This methods does not attempt to locate
     * <code>ImageReader</code>s that can read directly from an
     * <code>InputStream</code>; that may be accomplished using
     * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
     *
     * @param input an <code>InputStream</code> to read from.
     *
     * @return a <code>BufferedImage</code> containing the decoded
     * contents of the input, or <code>null</code>.
     *
     * @exception IllegalArgumentException if <code>input</code> is 
     * <code>null</code>.
     * @exception IOException if an error occurs during reading.
     */
    public static BufferedImage read(InputStream input) throws IOException {
        if (input == null) {
            throw new IllegalArgumentException("input == null!");
        }

        ImageInputStream stream = createImageInputStream(input);
        return read(stream);
    }

    /**
     * Returns a <code>BufferedImage</code> as the result of decoding
     * a supplied <code>ImageInputStream</code> with an
     * <code>ImageReader</code> chosen automatically from among those
     * currently registered.  If no registered
     * <code>ImageReader</code> claims to be able to read the stream,
     * <code>null</code> is returned.
     *
     * @param stream an <code>ImageInputStream</code> to read from.
     *
     * @return a <code>BufferedImage</code> containing the decoded
     * contents of the input, or <code>null</code>.
     *
     * @exception IllegalArgumentException if <code>stream</code> is 
     * <code>null</code>.
     * @exception IOException if an error occurs during reading.
     */
    public static BufferedImage read(ImageInputStream stream)
        throws IOException {
        if (stream == null) {
            throw new IllegalArgumentException("stream == null!");
        }

        Iterator iter = getImageReaders(stream);
        if (!iter.hasNext()) {
            return null;
        }

        ImageReader reader = (ImageReader)iter.next();
        ImageReadParam param = reader.getDefaultReadParam();
        reader.setInput(stream, true, true);
        BufferedImage bi = reader.read(0, param);
        stream.close();
        reader.dispose();
        return bi;
    }


--------------------------------------------------
Free Database Connection Pooling Software
 
hi sedj,
i had to perform this a completely different way. it is actually possible to insert the node into an imageIO. you have to insert the info into a file first and then insert that into a bufferedImage. here is the code.

Code:
 public void writeImageFile(String aInputFileName) throws Exception
	    {
	        File fileInput = new File(aInputFileName);
	        String aFileFormat = aInputFileName.substring(aInputFileName.length()-3,aInputFileName.length());

	        ImageWriter iw = (ImageWriter)ImageIO.getImageWritersByFormatName(aFileFormat).next();

	        ImageTypeSpecifier type = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
	        IIOMetadata meta = iw.getDefaultImageMetadata(type, null);


			meta.setFromTree("javax_imageio_png_1.0", theRootNode);
	        BufferedImage input = ImageIO.read(fileInput );
	        Iterator iter = ImageIO.getImageWritersByFormatName( "PNG" );
	        ImageWriter writer = (ImageWriter)iter.next();
	        ImageWriteParam iwp = writer.getDefaultWriteParam();
	        IIOImage image = new IIOImage(input, null, meta);

			ImageOutputStream ios = ImageIO.createImageOutputStream(theOutputFile);
			writer.setOutput(ios);
			writer.write(image);


    }
 
crikey, got to love JAI & imageio package !
Glad you got it working & thanks for posting back the solution for others to see :)


--------------------------------------------------
Free Database Connection Pooling Software
 
no worries (it caused me a big headache, it'l save others the pain :) )its not a bad little setup they've made. now on to gif's!!!!

dex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top