prev table of contents next

2.2.17 Image Data

An element containing a JPEG image has an XML schema definition like this:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:tns="http://example.com/know-type"
           xmlns:xmime="http://www.w3.org/2005/05/xmlmime"
           targetNamespace="http://example.com/know-type">

<xs:import namespace="http://www.w3.org/2005/05/xmlmime"
           schemaLocation="http://www.w3.org/2005/05/xmlmime"/>

<xs:complexType name="Item">
  <xs:sequence>
    <xs:element name="JPEGPicture" type="xs:base64Binary"
                xmime:expectedContentTypes="image/jpeg"/>
  </xs:sequence>
</xs:complexType>

<xs:element name="Picture" type="ItemType"/>
</xs:schema>
The <xs:import> element is not required; JAXB appears to "know" about this namespace. The schema type xs:base64binary is the type best suited for representing image data.

To create XML content and to marshal an instance document containing an image you can write code to create an object of type Image and set its reference in the appropriate element of the content tree:

BufferedImage bimg = ImageIO.read( imgFile );
Image img = bimg.getScaledInstance( 512, -1, Image.SCALE_DEFAULT );

// Create the Item element and store the image reference
ItemType itemEl = of.createItemType();
itemEl.setJPEGPicture( img ); 
JAXBElement<ItemType> jbe = of.createPicture( itemEl );
Unmarshalling is just as simple. You extract the Image object from its parent element and call Graphics.drawImage() with this object as the first argument.
// Get the image from the content tree.
JAXBElement<ItemType> jbe = ...;
Image img = jbe.getValue().getJPEGPicture();

// Draw it
Graphics g = ...;
g.drawImage( img, 0, 0, null );
Well, and here she is:


prev table of contents next