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">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.
<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
82RhFANKS0tJwqMGjBqA1QBSsMx3GVQDQAxyMQMusGDevP/ImIFUQLIBu3bt+k8KhukDAHrNjasT
jbHgAAAAAElFTkSuQmCC
</string>
</void>
<void method="put">
<string>/resource/image/twaver/link_icon.png</string>
<string>iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAKUlEQVR42mNgoAYoLS39j4zJMgAb
m74GUOwNbIaNGjKYDRlGKZYqORkAfhloKfeZLLAAAAAASUVORK5CYII=
</string>
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