我正在努力将svg文件转换为Swing中的图像。我试着用同样的蜡染库。在浏览器中打开svg文件时,会正确显示图像。但是当执行以下程序将svg转换为Swing中的Image / BufferedImage时,我得到异常,说TranscoderException:null过早结束文件。有人可以帮我解决这个问题吗?
由于
public class TestSymbolCreation {
static JFrameWin jFrameWindow;
static Image bufImage;
public static void main(String args[]) {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
System.out.println(new File(".").getAbsoluteFile());
File file = new File("./src/resources/milsymbol.js");
try {
Reader reader = new FileReader(file);
scriptEngine.eval(reader);
TestSymbolCreation testCreation = new TestSymbolCreation();
scriptEngine.put("testCreation", testCreation);
scriptEngine.eval("function run(testCreation){testCreation.getCanvas(new ms.Symbol('SFG-UCI----D',{size:35}).asSVG());} run(testCreation);");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getCanvas(String canvas) {
System.out.println("Value of Canvas111 =>"+canvas);
bufImage = createImageFromSVG();
SwingUtilities.invokeLater(runJFrameLater);
}
public void test() {
System.out.println("Test...");
}
public Image createImageFromSVG() {
File file = new File("./src/svgImg.svg");
System.out.println("File Exists =>"+file.exists());
System.out.println("File Exists =>"+file.isFile());
FileReader fr;
try {
fr = new FileReader(file);
BufferedReader reader = new BufferedReader(fr);
String s = "", line = null;
try {
while ((line = reader.readLine()) != null) {
s += line;
}
System.out.print(s);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
TranscoderInput svgImage = new TranscoderInput(reader);
Dimension component = new Dimension();
component.setSize(800, 800);
BufferedImageTranscoder transcoder = new BufferedImageTranscoder();
transcoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, (float) component.getWidth());
transcoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, (float) component.getHeight());
try {
transcoder.transcode(svgImage, null); // Exception thrown at this line
} catch (TranscoderException e) {
e.printStackTrace();;
}
return transcoder.createImage(60,60);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
public static class JFrameWin extends JPanel {
public JFrameWin(){
this.setSize(300, 200);
System.out.println("Going to set BufferedImage....");
JFrame jFrame = new JFrame();
jFrame.setTitle("Test Rotation!!!");
jFrame.getContentPane().add(this);
jFrame.pack();
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bufImage, 0, 0, this); // see javadoc for more info on the parameters
}
}
static Runnable runJFrameLater = new Runnable() {
@Override
public void run() {
jFrameWindow = new JFrameWin();
jFrameWindow.setVisible(true);
}
};
}
SVG文件内容:
<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" width="55.3" height="47.425" viewBox="21 18.5 158 135.5"><path d="M25,50 l150,0 0,100 -150,0 z" stroke-width="4" stroke="black" fill="rgb(128,224,255)" fill-opacity="1" ></path><path d="M25,50 L175,150 M25,150 L175,50" stroke-width="4" stroke="black" fill="black" ></path><g transform="translate(0,0)" stroke-width="4" stroke="black" fill="none" ><circle cx="100" cy="30" r="7.5" fill="black" ></circle><circle cx="70" cy="30" r="7.5" fill="black" ></circle><circle cx="130" cy="30" r="7.5" fill="black" ></circle></g></svg>
答案 0 :(得分:1)
BufferedReader reader = new BufferedReader(fr);
这里读者指向文件的开头。
String s = "", line = null;
try {
while ((line = reader.readLine()) != null) {
读者遍历文件。
s += line;
}
System.out.print(s);
...
所以这里的读者在文件的末尾,我们已经阅读了整个文件。
TranscoderInput svgImage = new TranscoderInput(reader);
当代码转换器尝试使用阅读器来读取文件时,没有什么可读的。
选项: