JAXB 编组/解组 - SWT.Image 或 AWT BufferedImage

发布于 2025-01-01 23:25:52 字数 524 浏览 2 评论 0原文

我正在尝试使用 jaxb 编组一个包含图像的对象,然后对其进行解组(即保存/加载)。

有没有办法存储该图像?

我正在尝试创建一个返回描述 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,但我仍然得到相同的异常。

I am trying to marshal with jaxb an object which contains an Image, and afterwards unmarshalling it (i.e. save/load).

Is there a way to store that image?

I am trying to create a function which returns the byte array describing the swt.image imagedata, but once I marked it as an @XmlElement, the process of storing it fails throwing an Exception like this:

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()

Also, I have tested to convert the SWT.Image to a AWT.BufferedImage, but I still get the same Exception.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2025-01-08 23:25:52

您的异常表明您已在不是访问器的方法(get/set 方法)上放置了注释。下面是使用 java.awt.Image 属性的示例:

Root

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;
    }

}

Demo

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);
    }

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <image>iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mNgYGAAAAAEAAHI6uv5AAAAAElFTkSuQmCC</image>
</root>

Your exception indicates that you have placed an annotation on a method that isn't an accessor (get/set method). Below is an example using a java.awt.Image property:

Root

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;
    }

}

Demo

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);
    }

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <image>iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mNgYGAAAAAEAAHI6uv5AAAAAElFTkSuQmCC</image>
</root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文