我正试图用jaxb封送一个包含Image的对象,然后对其进行解组(即保存/加载)。
有没有办法存储那个图像?
我正试图创建一个函数,该函数返回描述swt.image图像数据的字节数组,但一旦我将其标记为@XmlElement,存储过程就会失败,并引发如下异常:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
JAXB annotation is placed on a method that is not a JAXB property
this problem is related to the following location:
at @javax.xml.bind.annotation.XmlElement()
此外,我已经测试过将SWT.Image转换为AWT.BufferedImage,但仍然得到相同的Exception。
您的异常表示您已在非访问器的方法(get/set方法)上放置了注释。以下是使用java.awt.Image
属性的示例:
根
package forum9094655;
import java.awt.Image;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Root {
private Image image;
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
}
演示
package forum9094655;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Root root = new Root();
Image image = new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB);
root.setImage(image);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<image>iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mNgYGAAAAAEAAHI6uv5AAAAAElFTkSuQmCC</image>
</root>