2D指向Java中的SVG或PDF文件

想要将画布的2D点转换为svg或pdf文件 .

{“points”:[{“x”:158,“y”:786},{“x”:158,“y”:781},{“x”:158,“y”:764},{“ X “:158,” Y “:754},{” × “:158,” Y “:743},{” × “:158,” Y “:717},{” × “:158,” y”的:699},{ “×”:158, “Y”:678},{ “×”:158, “Y”:631},{ “×”:160, “Y”:606},{ “x” 的:163, “Y”:583},{ “×”:171, “Y”:531},{ “×”:173, “Y”:505},{ “×”:176, “Y”:480 },{ “×”:184, “Y”:428},{ “×”:184, “Y”:413},{ “×”:184, “Y”:406},{ “×”:188 , “Y”:372},{ “×”:193, “Y”:348},{ “×”:197, “Y”:333},{ “×”:197, “Y”:303}, { “×”:199, “Y”:284},{ “×”:201, “Y”:277},{ “×”:201, “Y”:269},{ “×”:201,” Y “:260},{” × “:201,” Y “:256},{” × “:201,” Y “:243},{” × “:201,” Y “:236},{” X “:201,” Y “:234},{” × “:201,” Y “:230},{” × “:201,” Y “:228},{” × “:201,” y”的:234},{ “×”:201, “Y”:238},{ “×”:203, “Y”:240},{ “×”:206, “Y”:243},{ “x” 的:210, “Y”:251},{ “×”:211, “Y”:255},{ “×”:214, “Y”:260},{ “×”:227, “Y”:273 },{ “×”:231, “Y”:281},{ “×”:236, “Y”:290},{ “×”:253, “Y”:325},{ “×”:257 , “Y”:334},{ “×”:270, “Y”:359},{ “×”:288, “Y”:398},{ “×”:293, “Y”:413}, { “×”:296, “Y”:419},{ “×”:309, “Y”:450},{ “×”:317, “Y”:471},{ “×”:322,” Y “:484},{” × “:335,” Y “:514},{” × “:344,” Y “:536},{” × “:348,” Y “:544},{” X “:357,” Y “:566},{” × “:360,” Y“:573 },{ “×”:361, “Y”:575},{ “×”:365, “Y”:596},{ “×”:372, “Y”:607},{ “×”:374 , “Y”:609},{ “×”:378, “Y”:618},{ “×”:382, “Y”:622},{ “×”:383, “Y”:622}, { “×”:383, “Y”:626},{ “×”:386, “Y”:630},{ “×”:387, “Y”:631},{ “×”:387,” Y “:635},{” × “:387,” Y “:637},{” × “:387,” Y “:639},{” × “:391,” Y “:639},{” X “:395,” Y “:635},{” × “:395,” Y “:635},{” × “:188,” Y “:514},{” × “:193,” y”的:514},{ “×”:201, “Y”:514},{ “×”:201, “Y”:514},{ “×”:210, “Y”:514},{ “x” 的:223, “Y”:514},{ “×”:224, “Y”:514},{ “×”:236, “Y”:514},{ “×”:242, “Y”:514 },{ “×”:249, “Y”:514},{ “×”:257, “Y”:514},{ “×”:258, “Y”:514},{ “×”:266 , “Y”:514},{ “×”:270, “Y”:514},{ “×”:270, “Y”:514},{ “×”:272, “Y”:514}, { “×”:275, “Y”:514},{ “×”:277, “Y”:514},{ “×”:279, “Y”:514},{ “×”:292,” Y “:506},{” × “:302,” Y “:495},{” × “:309,” Y “:488},{” × “:326,” Y “:475},{” X “:331,” Y “:470},{” × “:335,” Y “:467},{” × “:348,” Y “:454},{” × “:356,” y”的:451},{ “×”:361, “Y”:450},{ “×”:365, “Y”:441},{ “×”:371, “Y”:438},{ “x” 的:374, “Y”:437},{ “×”:377, “Y”:437},{ “×”:378, “Y”:437},{ “×”:380, “Y”:437 },{ “×”:378, “Y”:432},{ “×”:374, “Y”:432},{ “×”:374, “Y”:432},{ “×”:426 , “Y”:337},{ “×”:426, “Y”:350},{ “×”:426 , “Y”:368},{ “×”:426, “Y”:394},{ “×”:426, “Y”:403},{ “×”:426, “Y”:419}, { “×”:426, “Y”:458},{ “×”:426, “Y”:477},{ “×”:426, “Y”:497},{ “×”:426,” Y “:510},{” × “:426,” Y “:526},{” × “:426,” Y “:549},{” × “:426,” Y “:583},{” X “:426,” Y “:597},{” × “:426,” Y “:609},{” × “:426,” Y “:613},{” × “:426,” y”的:620},{ “×”:426, “Y”:626},{ “×”:426, “Y”:635},{ “×”:426, “Y”:638},{ “x” 的:426, “Y”:639},{ “×”:426, “Y”:643},{ “×”:426, “Y”:645},{ “×”:426, “Y”:645 },{ “×”:525, “Y”:480},{ “×”:525, “Y”:475},{ “×”:521, “Y”:480},{ “×”:515 , “Y”:482},{ “×”:512, “Y”:484},{ “×”:499, “Y”:493},{ “×”:493, “Y”:498}, { “×”:490, “Y”:501},{ “×”:477, “Y”:506},{ “×”:467, “Y”:512},{ “×”:464,” Y “:514},{” × “:460,” Y “:514},{” × “:454,” Y “:517},{” × “:452,” Y “:518},{” X “:443,” Y “:523},{” × “:440,” Y “:526},{” × “:439,” Y “:527},{” × “:434,” y”的:527},{ “×”:431, “Y”:527},{ “×”:434, “Y”:523},{ “×”:439, “Y”:523},{ “x” 的:441, “Y”:523},{ “×”:443, “Y”:523},{ “×”:447, “Y”:523},{ “×”:452, “Y”:528 },{ “×”:456, “Y”:531},{ “×”:469, “Y”:540},{ “×”:476, “Y”:547},{ “×”:482 , “Y”:553},{ “×”:499, “Y”:566},{ “×”:512, “Y”:579},{ “×”:521, “Y”:587}, { “×”:538, “Y”:600},{ “×”:550, “Y”:613}, { “×”:555, “Y”:618},{ “×”:559, “Y”:622},{ “×”:569, “Y”:633},{ “×”:572,” Y “:635},{” × “:581,” Y “:639},{” × “:584,” Y “:646},{” × “:585,” Y “:648},{” X “:590,” Y “:652},{” × “:593,” Y “:652},{” × “:594,” Y “:652},{” × “:594,” y”的:656},{ “×”:594, “Y”:658},{ “×”:598, “Y”:656},{ “×”:598, “Y”:661},{ “x” 的:598, “Y”:661},{ “×”:598, “Y”:664},{ “×”:598, “Y”:664},{ “×”:628, “Y”:350 },{ “×”:628, “Y”:355},{ “×”:628, “Y”:368},{ “×”:628, “Y”:385},{ “×”:628 , “Y”:388},{ “×”:628, “Y”:411},{ “×”:628, “Y”:437},{ “×”:628, “Y”:446}, { “×”:628, “Y”:467},{ “×”:628, “Y”:506},{ “×”:628, “Y”:519},{ “×”:628,” Y “:518},{” × “:628,” Y “:553},{” × “:628,” Y “:556},{” × “:628,” Y “:562},{” X “:628,” Y “:575},{” × “:628,” Y “:580},{” × “:628,” Y “:587},{” × “:628,” y”的:592},{ “×”:628, “Y”:594},{ “×”:628, “Y”:596},{ “×”:628, “Y”:598},{ “x” 的:633, “Y”:579},{ “×”:633, “Y”:579},{ “×”:736, “Y”:708},{ “×”:736, “Y”:712 },{ “×”:736, “Y”:712},{ “×”:628, “Y”:531},{ “×”:633, “Y”:531},{ “×”:636 , “Y”:531},{ “×”:637, “Y”:531},{ “×”:640, “Y”:531},{ “×”:641, “Y”:531}, { “×”:646, “Y”:531},{ “×”:648, “Y”:531},{ “×”:650, “Y”:531},{ “×”:651,” Y “:531},{” × “:654,” Y “:531},{” × “:659,” Y “:531},{” × “:663,” Y “:531},{” × “:667,” Y “:531},{” × “:672,” Y“:531}],

“身高”:1548,“宽度”:1536}

回答(1)

2 years ago

这样的东西只能转换为svg中的路径,而不是转换为pdf,除非先将其转换为图形 . SVG生成代码:

public static String generatePath(String input, boolean closePath) {
    Pattern vertexPattern = Pattern.compile("\\{\\\"x\\\"\\:(\\d+),\\\"y\\\"\\:(\\d+)\\}");
    Matcher matcher = vertexPattern.matcher(input);
    List<Point> points = new LinkedList<>();

    while (matcher.find()) {
        int x = Integer.parseInt(matcher.group(1));
        int y = Integer.parseInt(matcher.group(2));
        points.add(new Point(x, y));
    }

    String template = "<svg width=\"100\" height=\"100\" xmlns=\"http://www.w3.org/2000/svg\">"
            + "<path d=\"%s\"/>"
            + "</svg>";
    Point previousPoint = null;
    StringBuilder path = new StringBuilder(256);
    for (Point point : points) {
        if (previousPoint == null || (previousPoint.x != point.x && previousPoint.y != point.y)) {
            path.append(previousPoint == null ? "M " : " L ").append(point.x).append(" ").append(point.y);
        } else if (previousPoint.x != point.x) {
            path.append(" H ").append(point.x);
        } else if (previousPoint.y != point.y) {
            path.append(" V ").append(point.y);
        } else {
            // no actual distance is passed.
        }
        previousPoint = point;
    }
    if (closePath) {
        path.append(" Z");
    }
    return String.format(template, path.toString());
}

此代码将接受您的输入(您提供的输入)并将其转换为 path . 但是,您仍需要添加有关如何处理路径的信息,例如颜色等 . 为此,您可以修改模板字符串 .