2017-02-20 1 views
-2

Hier versuche ich ein SVG-Bild an den lokalen Server zu senden und in der Ausgabe möchte ich das Bild im PNG/JPEG-Format herunterladen.Konvertieren von SVG-Bild in Png in Java von Servlet

Während ich einige Lösungen gefunden habe, aber diese werden von BATIK-Bibliotheken gemacht, aber in meinem Eclipse werden BATIK-Bibliotheken nicht unterstützt, daher kann ich die Batik-Bibliotheken nicht verwenden.

+2

poste deinen Code. – techhunter

+0

Erstellen Sie einen Ordner namens lib innerhalb Ihres Eclipse-Projekts, kopieren Sie die BATIK-Bibliothek in den lib-Ordner, klicken Sie mit der rechten Maustaste auf das Libraray "jar" und wählen Sie -> add to buildpath. –

+0

Herr, wo kann ich Batikbibliotheken finden ??? @krzysztof Cichocki –

Antwort

2

Verwenden Sie Batikbibliothek. Unten ist der Code.

import java.io.*; 
    import org.apache.batik.transcoder.image.PNGTranscoder; 
    import org.apache.batik.transcoder.TranscoderInput; 
    import org.apache.batik.transcoder.TranscoderOutput; 
    import java.nio.file.Paths; 
    import java.nio.file.Path; 
    public class svg2png { 
     public static void main(String[] args) throws Exception { 
      //Step -1: We read the input SVG document into Transcoder Input 
      //We use Java NIO for this purpose 
      String svg_URI_input = Paths.get("chessboard.svg").toUri().toURL().toString(); 
      TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input);   
      //Step-2: Define OutputStream to PNG Image and attach to TranscoderOutput 
      OutputStream png_ostream = new FileOutputStream("chessboard.png"); 
      TranscoderOutput output_png_image = new TranscoderOutput(png_ostream);    
      // Step-3: Create PNGTranscoder and define hints if required 
      PNGTranscoder my_converter = new PNGTranscoder();   
      // Step-4: Convert and Write output 
      my_converter.transcode(input_svg_image, output_png_image); 
      // Step 5- close/flush Output Stream 
      png_ostream.flush(); 
      png_ostream.close();   
     } 
} 

Ich hoffe, es wird Ihnen helfen.

Siehe dazu: http://thinktibits.blogspot.com/2012/12/Batik-Convert-SVG-PNG-Java-Program-Example.html

+0

Ich habe auch diesen Code, aber in meiner Eklipse gibt es Fehler von Batik-Bibliotheken. BATIK-Bibliotheken werden unterstützt, was soll ich tun ?? –

+0

Auf Kompatibilität der Version prüfen. https://www.java-forums.org/new-java/34405-installing-batik-use-eclipse.html – techhunter

+0

siehe auch: https://xmlgraphics.apache.org/batik/ – techhunter

0

Sie auch SVG PNG-Format ohne die Verwendung von Batik Transcoder umwandeln kann. Folgen Sie folgenden Link: https://nupur28ag.blogspot.in/

BufferedImage input_image = null; 
input_image = ImageIO.read(new File("Convert_to_PNG.svg")); //read svginto input_image object 
File outputfile = new File("imageio_png_output.png"); //create new outputfile object 
ImageIO.write(input_image, "PNG", outputfile); 

Durch einfaches ImageIO Bibliothek. Hoffe das wird helfen!

+0

Dieser Code funktioniert nicht –

Verwandte Themen