将AWT图像转换为SVG图像 [英] converting AWT image to SVG Image

查看:115
本文介绍了将AWT图像转换为SVG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码提取AWT图像(从PDF,通过PDFBox):

I am ingesting an AWT image (from PDF, through PDFBox) with the following code:

private java.awt.Graphics2D graphics;
public void drawImage(java.awt.Image awtImage, java.awt.geom.AffineTransform at) {
    graphics.setComposite(getGraphicsState().getStrokeJavaComposite());
    graphics.setClip(getGraphicsState().getCurrentClippingPath());
    graphics.drawImage( awtImage, at, null );
}

,并希望将图像捕获/输出为SVG.我一直在使用Batik库,该库生成以下形式的svg

and wish to capture/output the image as SVG. I have been using the Batik library which produces svg of the form

    <image x="0" y="0" transform="matrix(0.144,0,0,0.1439,251.521,271.844)" 
clip-path="url(#clipPath2)" width="1797" xlink:href="data:image/png;
base64,iVBORw0KGgoAAAANSUhEUgAABwUAAAV4CAMAAAB2DvLsAAADAFBMVEX////+/v56 
enpWVlZbW1taWlpZWVnHx8eRkZFVVVWMjIysrKxXV1dYWFhqamr5+fnMzMxeXl7c 
3NyUlJR/f3+3t7cAAACGhob29vYpKSliYmJPT083Nzf8/PyBgYENDQ3s7OwwMDD1 
    ...
    RERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERE 
RPQP/R8CiIK+y8Q6KQAAAABJRU5ErkJggg==" height="1400"
 preserveAspectRatio="none" stroke-width="0" xmlns:xlink="http://www.w3.org/1999/xlink"/>

我有我自己的SVG库,想将方法添加到SVGImage类中以产生类似的东西.我需要将AffineTransformation应用于数据吗?如果是,怎么办?

I have my own SVG library and want to add methods to my SVGImage class to produce something similar. Do I need to apply the AffineTransformation to the data, and if so how?

非常感谢您指出可以执行此操作的适当(F/OSS)方法或库.数据应该是内联的(如上例所示)且符合XML.

I would be grateful to be pointed to appropriate (F/OSS) methods or libraries that can do this. The data should be inline (as in the above example) and XML-compliant.

更新:在没有任何答案或评论的情况下,我不得不编写自己的实现.这并不完全是琐碎的事情,可能不是可靠的或最佳的解决方案.我将其作为答案-如果有缺陷或可以改进,请发表评论.

UPDATE: In the absence of any answer or comments I have had to write my own implementation. This wasn't completely trivial and may not be robust or the best solution. I append it as an answer - please comment if it's flawed or could be improved.

推荐答案

[回答自己的问题]

[ANSWERING OWN QUESTION]

我认为java.awt.ImageBufferedImage,因为它具有适当的方法.在目前的情况下,这已经奏效了(到目前为止).

I assume the java.awt.Image is a BufferedImage as that has the appropriate methods. In the present case this has worked (so far).

SVG需要图像的MIME类型(例如"image/png"). ImageIO需要一种类型,例如"PNG". mimeType2ImageTypeMap映射这些. Base64编解码器取自Xerces,但周围有很多. DOM实现是XOM,属性构造为xlink提供了命名空间

The SVG needs the MIME type of the image (e.g. "image/png"). ImageIO needs a type such as "PNG". mimeType2ImageTypeMap maps these. The Base64 codec is taken from Xerces but there are a lot around. The DOM implementation is XOM and the attribute construction provides the namespace for xlink

    public class SVGImage extends xom.nu.Element {
    private static final String DATA = "data";
    private static final String BASE64 = "base64";
    public static final String IMAGE_PNG = "image/png";
    public static final String PNG = "PNG";

    private static final String XLINK_PREF = "xlink";
    private static final String HREF = "href";
    private static final String XLINK_NS = "http://www.w3.org/1999/xlink";

        public void readImageData(BufferedImage bufferedImage, String mimeType) {
    String type = mimeType2ImageTypeMap.get(mimeType);
    if (type == null) {
        throw new RuntimeException("Cannot convert mimeType: "+mimeType);
    }
    double x = bufferedImage.getMinX();
    double y = bufferedImage.getMinY();
    double height = bufferedImage.getHeight();
    double width = bufferedImage.getWidth();
    this.setX(x);
    this.setY(y);
    this.setWidth(width);
    this.setHeight(height);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        ImageIO.write(bufferedImage, type, baos);
    } catch (IOException e) {
        throw new RuntimeException("Cannot read image", e);
    }
    byte[] byteArray = baos.toByteArray();
    String base64 = Base64.encode(byteArray);
    String attValue = DATA+":"+mimeType+";"+BASE64+","+base64;
    this.addAttribute(new Attribute(XLINK_PREF+":"+HREF, XLINK_NS, attValue));
    }
}

上面的代码已在一个测试用例上工作,因此我很乐观地将其扩展到大型图像和其他MIME类型,但尚未对其进行测试.

The code above has worked on a test case so I am optimistic it will scale to large Images and other MIME types but haven't tested them.

是:我必须应用仿射变换.我已经有了一些实用程序,可以将3 * 2的double矩阵转换为SVG transform="matrix(...)"属性.所以我的最终代码是:

YES: I do have to apply the affine transformation. I already have utility routines for this which convert a 3*2 matrix of doubles to an SVG transform="matrix(...)" attribute. So my final code is:

Transform2 t2 = new Transform2(at);              // converts matrix syntax
BufferedImage bImage = (BufferedImage) awtImage;
SVGImage svgImage = new SVGImage();
svgImage.setTransform(t2);
svgImage.readImageData(bImage, SVGImage.IMAGE_PNG);

这篇关于将AWT图像转换为SVG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆