Algorithm to make a polygon from transparent png sprite

有些话、适合烂在心里 提交于 2019-12-06 22:54:39

See this question. It will be slow, but it depends on how accurate you want it (second answer is sloppier, but a bit faster). After you get the Area from getOutline() on the other question, try using this code (untested):

public static Polygon getPolygonOutline(BufferedImage image) {
    Area a  = getOutline(image, new Color(0, 0, 0, 0), false, 10); // 10 or whatever color tolerance you want
    Polygon p = new Polygon();
    FlatteningPathIterator fpi = new FlatteningPathIterator(a.getPathIterator(null), 0.1); // 0.1 or how sloppy you want it
    double[] pts = new double[6];
    while (!fpi.isDone()) {
        switch (fpi.currentSegment(pts)) {
        case FlatteningPathIterator.SEG_MOVETO:
        case FlatteningPathIterator.SEG_LINETO:
            p.addPoint((int) pts[0], (int) pts[1]);
            break;
        }
        fpi.next();
    }
    return p;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!