2017-11-28 1 views
0

ist es möglich, reinen SVG-Code in JPG mit Java zu konvertieren? Ich habe versucht, Batik SVG zu verwenden, aber es muss Dateipfad zu unterhalten, und ich möchte nur Code verwenden.Konvertieren SVG geschrieben in HTML5 zu JPG mit JAVA

SVG-Code:

String chart = "<svg width=\"700\" height=\"297\" aria-label=\"A chart.\" style=\"overflow: hidden;\"><defs id=\"defs\"><clipPath id=\"_ABSTRACT_RENDERER_ID_0\"><rect x=\"175\" y=\"57\" width=\"350\" height=\"184\"></rect></clipPath><filter id=\"_ABSTRACT_RENDERER_ID_1\"><feGaussianBlur in=\"SourceAlpha\" stdDeviation=\"2\"></feGaussianBlur><feOffset dx=\"1\" dy=\"1\"></feOffset><feComponentTransfer><feFuncA type=\"linear\" slope=\"0.1\"></feFuncA></feComponentTransfer><feMerge><feMergeNode></feMergeNode><feMergeNode in=\"SourceGraphic\"></feMergeNode></feMerge></filter></defs><rect x=\"0\" y=\"0\" width=\"700\" height=\"297\" stroke=\"none\" stroke-width=\"0\" fill=\"#ffffff\"></rect><g><rect x=\"538\" y=\"57\" width=\"149\" height=\"13\" stroke=\"none\" stroke-width=\"0\" fill-opacity=\"0\" fill=\"#ffffff\"></rect><g><rect x=\"538\" y=\"57\" width=\"149\" height=\"13\" stroke=\"none\" stroke-width=\"0\" fill-opacity=\"0\" fill=\"#ffffff\"></rect><g><text text-anchor=\"start\" x=\"569\" y=\"68.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">Wynik</text></g><rect x=\"538\" y=\"57\" width=\"26\" height=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#3366cc\"></rect></g></g><g><rect x=\"175\" y=\"57\" width=\"350\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill-opacity=\"0\" fill=\"#ffffff\"></rect><g clip-path=\"url(http://localhost:8080/summary#_ABSTRACT_RENDERER_ID_0)\"><g><rect x=\"175\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect><rect x=\"262\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect><rect x=\"350\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect><rect x=\"437\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect><rect x=\"524\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect></g><g><rect x=\"176\" y=\"64\" width=\""+ String.valueOf(getGroupAnswer.get(0).getValue() * 5.79) +"\" height=\"23\" stroke=\"#ffa500\" stroke-width=\"1\" fill=\"#ffa500\"></rect><rect x=\"176\" y=\"101\" width=\""+ String.valueOf(getGroupAnswer.get(1).getValue() * 5.79) +"\" height=\"23\" stroke=\"#b87333\" stroke-width=\"1\" fill=\"#b87333\"></rect><rect x=\"176\" y=\"138\" width=\""+ String.valueOf(getGroupAnswer.get(2).getValue() * 5.79) +"\" height=\"22\" stroke=\"#c0c0c0\" stroke-width=\"1\" fill=\"#c0c0c0\"></rect><rect x=\"176\" y=\"174\" width=\""+ String.valueOf(getGroupAnswer.get(3).getValue() * 5.79) +"\" height=\"23\" stroke=\"#ffd700\" stroke-width=\"1\" fill=\"#ffd700\"></rect><rect x=\"176\" y=\"211\" width=\""+ String.valueOf(getGroupAnswer.get(4).getValue() * 5.79) +"\" height=\"23\" stroke=\"#e5e4e2\" stroke-width=\"1\" fill=\"#e5e4e2\"></rect></g><g><rect x=\"175\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#333333\"></rect></g></g><g></g><g><g><text text-anchor=\"middle\" x=\"175.5\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">0</text></g><g><text text-anchor=\"middle\" x=\"262.75\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">15</text></g><g><text text-anchor=\"middle\" x=\"350\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">30</text></g><g><text text-anchor=\"middle\" x=\"437.25\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">45</text></g><g><text text-anchor=\"middle\" x=\"524.5\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">60</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"80.35\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST4</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"116.95\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST1</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"153.55\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST5</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"190.15\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST3</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"226.75000000000003\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST2</text></g></g></g><g><g><text text-anchor=\"middle\" x=\"350\" y=\"284.4562\" font-family=\"Arial\" font-size=\"13\" font-style=\"italic\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">Wynik całkowity</text><rect x=\"175\" y=\"273.4062\" width=\"350\" height=\"13\" stroke=\"none\" stroke-width=\"0\" fill-opacity=\"0\" fill=\"#ffffff\"></rect></g></g><g></g></svg>"; 
+0

möglich doppelte https://stackoverflow.com/questions/4437806/convert-html-in-image-in-byte-array-java –

Antwort

2

Sie können String in Inputstream konvertieren:

String chart = "<svg width=\"700\..."; 
InputStream stream = new ByteArrayInputStream(chart.getBytes(StandardCharsets.UTF_8.name())); 

und passieren, dass an TranscoderInput Konstruktor:

TranscoderInput input = new TranscoderInput(stream); 
+0

TranscoderInput? Tut mir leid, aber ich bin mit Java und dieser Bibliothek nicht vertraut - können Sie mehr erklären? –

+0

Sie sagten, Sie verwenden Batik, um SVG in JPG zu konvertieren. TranscoderInput ist das Objekt für die Konvertierung der Eingabe (SVG). Sie können ein vollständiges Beispiel finden http://www.java2s.com/Code/Java/XML/SVGtoJPEG.htm –

+0

So anstelle von neuen FileInputStream ("rectangles.svg") im Beispiel können Sie neue ByteArrayInputStream (chart.getBytes (StandardCharsets.UTF_8.name())), um SVG-Daten aus Ihrer Zeichenfolge anstelle der .svg-Datei zu erhalten. –