java - 使用 java 工具将 SVG 子集转换为 Java 的麻烦
问题描述
我正在使用这个工具:
https://github.com/codenameone/flamingo-svg-transcoder
你可以在这里读更多关于它的内容:
https://www.codenameone.com/blog/flamingo-svg-transcoder.html#commento-login-box-container
我用它来转换一个 svg 文件,结果如下:
/**
* This class has been automatically generated using
* <a href="http://ebourg.github.io/flamingo-svg-transcoder/">Flamingo SVG transcoder</a>.
*/
public class MyImageFromSVGSubset extends com.codename1.ui.Image implements Painter {
private int width, height;
private Transform t = Transform.makeIdentity(), t2 = Transform.makeIdentity();
public MyImageFromSVGSubset() {
super(null);
width = getOrigWidth();
height = getOrigHeight();
}
public MyImageFromSVGSubset(int width, int height) {
super(null);
this.width = width;
this.height = height;
fixAspectRatio();
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}
@Override
public void scale(int width, int height) {
this.width = width;
this.height = height;
fixAspectRatio();
}
@Override
public MyImageFromSVGSubset scaled(int width, int height) {
MyImageFromSVGSubset f = new MyImageFromSVGSubset(width, height);
f.fixAspectRatio();
return f;
}
public Image toImage() {
Image i = Image.createImage(width, height, 0);
Graphics g = i.getGraphics();
drawImage(g, null, 0, 0, width, height);
return i;
}
private void fixAspectRatio() {
if(width == -1) {
float ar = ((float)getOrigWidth()) / ((float)getOrigHeight());
width = Math.round(((float)height) * ar);
}
if (height == -1) {
float ar = ((float)getOrigHeight()) / ((float)getOrigWidth());
height = Math.round(((float)width) * ar);
}
}
@Override
public Image fill(int width, int height) {
return new MyImageFromSVGSubset(width, height);
}
@Override
public Image applyMask(Object mask) {
return new MyImageFromSVGSubset(width, height);
}
@Override
public boolean isAnimation() {
return true;
}
@Override
public boolean requiresDrawImage() {
return true;
}
@Override
protected void drawImage(Graphics g, Object nativeGraphics, int x, int y) {
drawImage(g, nativeGraphics, x, y, width, height);
}
@Override
protected void drawImage(Graphics g, Object nativeGraphics, int x, int y, int w, int h) {
g.getTransform(t);
t2.setTransform(t);
float hRatio = ((float) w) / ((float) getOrigWidth());
float vRatio = ((float) h) / ((float) getOrigHeight());
t2.translate(x + g.getTranslateX(), y + g.getTranslateY());
t2.scale(hRatio, vRatio);
g.setTransform(t2);
paint(g);
g.setTransform(t);
}
private static void paint(Graphics g) {
int origAlpha = g.getAlpha();
Stroke baseStroke;
Shape shape;
g.setAntiAliased(true);
g.setAntiAliasedText(true);
/*Composite origComposite = g.getComposite();
if (origComposite instanceof AlphaComposite) {
AlphaComposite origAlphaComposite = (AlphaComposite)origComposite;
if (origAlphaComposite.getRule() == AlphaComposite.SRC_OVER) {
origAlpha = origAlphaComposite.getAlpha();
}
}
*/
java.util.LinkedList<Transform> transformations = new java.util.LinkedList<Transform>();
//
transformations.push(g.getTransform());
g.transform(new AffineTransform(3.7795277f, 0, 0, 3.7795277f, 0, 0).toTransform());
// _0
// _0_0
// _0_0_0
shape = new Rectangle2D(0.008569182828068733, 0.0054579987190663815, 6.3905863761901855, 6.390747547149658);
g.setColor(0xffffff);
g.fillShape(shape);
// _0_0_1
shape = new GeneralPath();
((GeneralPath) shape).moveTo(5.0011573, 1.454892);
((GeneralPath) shape).curveTo(4.914109, 1.454892, 4.8439946, 1.526065, 4.8439946, 1.6128483);
...
...
//very long sequence of graphical instructions that corresponds to the actual drawing
...
...
((GeneralPath) shape).closePath();
g.fillShape(shape);
g.setTransform(transformations.pop()); // _0
g.setAlpha(origAlpha);
}
/**
* Returns the X of the bounding box of the original SVG image.
*
* @return The X of the bounding box of the original SVG image.
*/
public static int getOrigX() {
return 1;
}
/**
* Returns the Y of the bounding box of the original SVG image.
*
* @return The Y of the bounding box of the original SVG image.
*/
public static int getOrigY() {
return 1;
}
/**
* Returns the width of the bounding box of the original SVG image.
*
* @return The width of the bounding box of the original SVG image.
*/
public static int getOrigWidth() {
return 24;
}
/**
* Returns the height of the bounding box of the original SVG image.
*
* @return The height of the bounding box of the original SVG image.
*/
public static int getOrigHeight() {
return 24;
}
@Override
public void paint(Graphics g, Rectangle rect) {
drawImage(g, null, rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
}
}
这是要在我的应用程序中使用的 Java 代码(代号一),但它对我不起作用。
我将以下代码放入工作布局中:
button.setIcon(new MyImageFromSVGSubset(256,256));
图像存在是因为它占据了正确的空间 (256x256),但没有绘制任何内容。
错误是什么?
解决方案
推荐阅读
- python - Pytorch 将张量转换为一个热
- c# - 使用 ISNULL 函数会引发错误
- react-native - 归档时“命令 PhaseScriptExecution 失败,退出代码非零”
- javascript - 如何通过单击从两个不同的 div(其中一个是隐藏的)复制内容,但每个都在单独的行上?
- r - r 为什么 if_else 将字符转换为数字以及如何保留字符
- directory - 在 VBA Excel 中工作,我可以获得目录中所有文件的总大小,不包括子目录
- python - 如何使用一个循环删除多个 XML 元素?
- scala - spark sbt 获取工件时出错 https://repo1.maven.org/maven2/org/apache/hadoop/hadoop-hdfs/2.6.5/hadoop-hdfs-2.6.5.jar 校验和错误
- java - 我尝试获取 PrivateKey.getEncode 时出错
- jsonp - 回调参数不起作用 JSONP javascript