2017-09-10 326 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中的所有代碼,使讀者在文件的開始,當你將它傳遞給轉碼器
  • 使用標記和重置爲讀者重置開始該文件之前將它傳遞給轉碼器
  • 通做得相當不是讀者的轉碼器