charts added
This commit is contained in:
@@ -40,7 +40,7 @@ public class ReportSynthesisService {
|
|||||||
return new ArrayList<byte[]>();
|
return new ArrayList<byte[]>();
|
||||||
}
|
}
|
||||||
String svg = ollamaAnalyticsService.generateWithInstruction(json, buildSvgPrompt(json));
|
String svg = ollamaAnalyticsService.generateWithInstruction(json, buildSvgPrompt(json));
|
||||||
byte[] png = SvgToPngConverter.convertSvgToPng(svg);
|
byte[] png = SvgToPngConverter.convert(svg);
|
||||||
List<byte[]> list = new ArrayList<>();
|
List<byte[]> list = new ArrayList<>();
|
||||||
if (png != null && png.length > 0) {
|
if (png != null && png.length > 0) {
|
||||||
list.add(png);
|
list.add(png);
|
||||||
|
|||||||
@@ -2,39 +2,49 @@ package kz.konturai.parser.util;
|
|||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import org.apache.batik.transcoder.TranscoderInput;
|
||||||
|
import org.apache.batik.transcoder.TranscoderOutput;
|
||||||
|
import org.apache.batik.transcoder.image.PNGTranscoder;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
public class SvgToPngConverter {
|
public class SvgToPngConverter {
|
||||||
|
|
||||||
public static byte[] convertSvgToPng(String svg) {
|
/**
|
||||||
if (svg == null || svg.isBlank()) {
|
* Преобразует строку с SVG-кодом в массив байт PNG-изображения.
|
||||||
|
*
|
||||||
|
* @param svgCode Строка, содержащая валидный SVG.
|
||||||
|
* @return Массив байт PNG или null в случае ошибки.
|
||||||
|
*/
|
||||||
|
public static byte[] convert(String svgCode) {
|
||||||
|
if (svgCode == null || svgCode.isBlank()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Class<?> pngTranscoderClass = Class.forName("org.apache.batik.transcoder.image.PNGTranscoder");
|
// 1. Создаем транскодер для PNG
|
||||||
Object transcoder = pngTranscoderClass.getDeclaredConstructor().newInstance();
|
PNGTranscoder transcoder = new PNGTranscoder();
|
||||||
|
|
||||||
Class<?> transcoderInputClass = Class.forName("org.apache.batik.transcoder.TranscoderInput");
|
// 2. Устанавливаем входные данные (наш SVG-код)
|
||||||
Class<?> transcoderOutputClass = Class.forName("org.apache.batik.transcoder.TranscoderOutput");
|
InputStream inputStream = new ByteArrayInputStream(svgCode.getBytes(StandardCharsets.UTF_8));
|
||||||
|
TranscoderInput input = new TranscoderInput(inputStream);
|
||||||
|
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(svg.getBytes(StandardCharsets.UTF_8));
|
// 3. Устанавливаем, куда будет записан результат (в байтовый массив в памяти)
|
||||||
Constructor<?> inputCtor = transcoderInputClass.getConstructor(java.io.InputStream.class);
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
Object input = inputCtor.newInstance(in);
|
TranscoderOutput output = new TranscoderOutput(outputStream);
|
||||||
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
// 4. Запускаем процесс конвертации
|
||||||
Constructor<?> outputCtor = transcoderOutputClass.getConstructor(java.io.OutputStream.class);
|
transcoder.transcode(input, output);
|
||||||
Object output = outputCtor.newInstance(out);
|
|
||||||
|
|
||||||
Method transcode = pngTranscoderClass.getMethod("transcode", transcoderInputClass, transcoderOutputClass);
|
// 5. Получаем результат
|
||||||
transcode.invoke(transcoder, input, output);
|
outputStream.flush();
|
||||||
|
return outputStream.toByteArray();
|
||||||
|
|
||||||
out.flush();
|
|
||||||
return out.toByteArray();
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
return null;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
System.err.println("Ошибка при конвертации SVG в PNG: " + e.getMessage());
|
||||||
|
e.printStackTrace(); // Очень полезно для отладки
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user