简体   繁体   English

Java - 从图像边框创建一个形状

[英]Java - Create a shape from border around image

i have a class that draws a shape from a png image, so that i can use the shape to draw the border of custom buttons that i need for my project. 我有一个从png图像中绘制形状的类,这样我就可以使用该形状绘制我项目所需的自定义按钮的边框。 here's the code for the class to draw the shape of an image: 这是绘制图像形状的类的代码:

public class CreateShapeClass {
    public static Area createArea(BufferedImage image, int maxTransparency) {
        Area area = new Area();
        Rectangle rectangle = new Rectangle();
        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                int rgb = image.getRGB(x, y);
                rgb = rgb >>> 24;
                if (rgb >= maxTransparency) {
                    rectangle.setBounds(x, y, 1, 1);
                    area.add(new Area(rectangle));
                }
            }
        }
        return area;
    }
}

however, this takes a very long time to process, and I thought that by pre-drawing the shapes in my main application and then storing them into an array and passing to other classes, it will reduce the rendering time. 然而,这需要很长时间来处理,我认为通过在我的主应用程序中预先绘制形状然后将它们存储到数组并传递给其他类,它将减少渲染时间。 however, the time taken for the paintBorder() method to draw out the border of the button takes a pretty long time too (although shorter than the time required to draw the shape), because the shape generated by the class above is filled and not empty. 但是,paintBorder()方法绘制按钮边框所花费的时间也需要相当长的时间(尽管比绘制形状所需的时间短),因为上面的类生成的形状是填充的而不是空。 I've tried to draw shapes using java2d, for example, Ellipse2D, and the rendering of the button only takes a very short time. 我曾尝试使用java2d绘制形状,例如,Ellipse2D,并且按钮的渲染只需要很短的时间。 anyone experienced in this field can teach me how do i generate a shape that is the border of a BufferedImage? 在这个领域有经验的人可以教我如何生成一个BufferedImage边界的形状? i use the class above to create the shape from PNG image with a transparent background. 我使用上面的类从PNG图像创建具有透明背景的形状。

For tips, look at Smoothing a jagged path. 有关提示,请查看平滑锯齿状路径。 The algorithm to obtain the (crude) outline was relatively quick in the final versions. 在最终版本中获得(粗略)轮廓的算法相对较快。 Creating a GeneralPath is astonishingly faster than appending Area objects. 创建GeneralPath比附加Area对象快得多。

The important part is this method: 重要的是这个方法:

public Area getOutline(Color target, BufferedImage bi) {
    // construct the GeneralPath
    GeneralPath gp = new GeneralPath();

    boolean cont = false;
    int targetRGB = target.getRGB();
    for (int xx=0; xx<bi.getWidth(); xx++) {
        for (int yy=0; yy<bi.getHeight(); yy++) {
            if (bi.getRGB(xx,yy)==targetRGB) {
                if (cont) {
                    gp.lineTo(xx,yy);
                    gp.lineTo(xx,yy+1);
                    gp.lineTo(xx+1,yy+1);
                    gp.lineTo(xx+1,yy);
                    gp.lineTo(xx,yy);
                } else {
                    gp.moveTo(xx,yy);
                }
                cont = true;
            } else {
                cont = false;
            }
        }
        cont = false;
    }
    gp.closePath();

    // construct the Area from the GP & return it
    return new Area(gp);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM