2012/11/01

How to Export the Base64 Message of an Image

The export of network has been provided in TWaver, so the Network in it can be exported as an image and the data of that Network can be done as binary information or Persistent XML data. Now we will emphasize on how to save images in the form of base64 format when serializing a DataBox.

If you have seen TWaver demo, you must know that the button “Save XML File” has been available on the toolbar of TWaver Network. In addition, there is also a button “Save Image Data” which is to save the image of an element and to serialize it into xml in the form of base64 when TWaver is saving XMLs.
Now let’s see the xml after it has been saved:
<void property="imageURL">
     <string>/demo/resource/images/usa.gif</string>
    </void>
   </object>
  </void>
  <void property="images">
   <object>
    <void method="put">
     <string>/resource/image/twaver/node_icon.png</string>
     <string>iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAASElEQVR42mP4TwGYPGnSfwYQ4/z5&#13;
82RhFANKS0tJwqMGjBqA1QBSsMx3GVQDQAxyMQMusGDevP/ImIFUQLIBu3bt+k8KhukDAHrNjasT&#13;
jbHgAAAAAElFTkSuQmCC&#13;
</string>
    </void>
    <void method="put">
     <string>/resource/image/twaver/link_icon.png</string>
     <string>iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAKUlEQVR42mNgoAYoLS39j4zJMgAb&#13;
m74GUOwNbIaNGjKYDRlGKZYqORkAfhloKfeZLLAAAAAASUVORK5CYII=&#13;
</string>
It can be seen that TWaver saves not only the path of the image of an element, but also the Base64 message of it. If you are careful enough, you will find that the Base64 message of the background image of the network is not included in this xml. Rather, it only saves the Base64 data of the image and the icon of that node. If we have other images or we want to invoke the serialize function ourselves, how to save the Base64 message of images? Now let’s first see the realization of “SaverXML” on the toolbar.
DataBoxOutputSetting setting = new DataBoxOutputSetting();
         setting.setWithElementId(saveElementID);
         setting.setWithAlarmState(saveAlarmState);
         setting.setWithLayers(saveLayers);
      if (saveCurrentSubNetwork) {
       TSubNetwork subNetwork = network.getCurrentSubNetwork();
    setting.setElementFilter(new SubNetworkPersistentFilter(subNetwork));
   }
      if(saveImageData){
       Map images = new HashMap();
       Iterator it = network.getDataBox().iterator();
       while(it.hasNext()){
        Element element = (Element)it.next();
        this.addImage(images, element.getImageURL());
        this.addImage(images, element.getIconURL());
       }
       setting.setImages(images);      
      }
      network.getDataBox().output(fileName, setting); 

private void addImage(Map map, String url) {
    ImageIcon image = TWaverUtil.getImageIcon(url, false);
    if (image != null) {
        String formatName = url.substring(url.length() - 3, url.length());
        byte[] data = TWaverUtil.getByteArrayFromImage(image.getImage(), formatName);
        map.put(url, TWaverUtil.encodeBase64Buffer(data));
    }
}

From the codes above, we can see that TWaver saves the corresponding base64 message of the url of an image into a map, and then “setting.setImages(images);”(in this way, everytime TWaver saves images with their urls when serializing, base64 can be used.) When invoking databox.output or databox.toXML by yourself, you can set DataBoxOutputSetting and save the Base64 messages of the images you need.

At last, there is a complete example in which the Base64 messages of the image of an element and the background image of a network are saved.

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import twaver.DataBoxOutputSetting;
import twaver.Element;
import twaver.Link;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverUtil;
import twaver.network.TNetwork;
import twaver.network.background.ImageBackground;

public class Base64ImageDemo extends JPanel {
    private TDataBox box = new TDataBox();
    private TNetwork network = new TNetwork(box);

    public Base64ImageDemo() {
        Node fromNode = new Node();
        fromNode.setLocation(585, 406);
        fromNode.setName("boy");
        fromNode.setImage("/images/boy.png");
        box.addElement(fromNode);

        Node toNode = new Node();
        toNode.setLocation(99, 222);
        toNode.setImage("/images/girl.png");
        toNode.setName("girl");
        box.addElement(toNode);
        this.setLayout(new BorderLayout());
        this.add(network, BorderLayout.CENTER);

        Link link = new Link(fromNode, toNode);
        link.putRenderColor(Color.GREEN);
        box.addElement(link);

        network.setImageBackground("/images/china_map.gif");

        exportDataBox();
    }

    private void exportDataBox() {
        DataBoxOutputSetting setting = new DataBoxOutputSetting();
        Map images = new HashMap();
        // add all element images
        Iterator it = network.getDataBox().iterator();
        while (it.hasNext()) {
            Element element = (Element) it.next();
            this.addImage(images, element.getImageURL());
            this.addImage(images, element.getIconURL());
        }
        // add network background image
        if (network.getDataBox().getBackground() instanceof ImageBackground) {
            ImageBackground imageBackground = (ImageBackground) network
                    .getDataBox().getBackground();
            this.addImage(images, imageBackground.getImageURL());
        }

        setting.setImages(images);
        System.out.println(box.toXML(setting));
    }

    private void addImage(Map map, String url) {
        ImageIcon image = TWaverUtil.getImageIcon(url, false);
        if (image != null) {
            String formatName = url.substring(url.length() - 3, url.length());
            byte[] data = TWaverUtil.getByteArrayFromImage(image.getImage(), formatName);
            map.put(url, TWaverUtil.encodeBase64Buffer(data));
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Base64ImageDemo demo = new Base64ImageDemo();
        frame.setContentPane(demo);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(880, 800);
        frame.setTitle("Image Base64 Demo");
        TWaverUtil.centerWindow(frame);
        frame.setVisible(true);
    }
}

No comments:

Post a Comment