2017-09-10 269 views
0

我努力将svg文件转换为Swing中的图像。我试图使用蜡染库的相同。在浏览器中打开svg文件,可以正确显示图像。但是当执行下面的程序将Swv中的svg转换为Image/BufferedImage时,我收到异常,说TranscoderException:null提前结束文件。有人能帮我解决这个问题吗?org.apache.batik.transcoder.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

如果您传递之前,打印S的值变码器,它匹配输入文件? –

+0

是的完全一样 – User

回答

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

当代码转换器试图使用阅读器读取文件时,没有什么可读的。

选项:

  • 删除创建S中的所有代码,使读者在文件的开始,当你将它传递给转码器
  • 使用标记和重置为读者重置开始该文件之前将它传递给转码器
  • 通做得相当不是读者的转码器