爪哇 - 来自全球图像边界创建一个形状 [英] Java - Create a shape from border around image

查看:134
本文介绍了爪哇 - 来自全球图像边界创建一个形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从PNG图像绘制的形状,这样我可以使用的形状绘制的,我需要为我的项目自定义按钮边框的类。这里的code的类来绘制图像的形状:

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. 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;
    }
}

然而,这需要很长的时间来处理,我认为,通过pre-拉我的主要应用程序的形状,然后将它们存储到一个数组并传递到其它类,它会降低渲染时间。然而,采取给paintBorder()方法来绘制出该按钮的边框的时间花费pretty长的时间太长(虽然比以绘制形状所需的时间更短),因为该类生成的形状以上填补,不是空的。我试着用画Java2D的形状,例如,Ellipse2D的,并且按钮的呈现只需要很短的时间。任何人在这一领域的经验可以教我如何生成的形状是一个BufferedImage的边界?我使用类以上具有透明背景创建PNG图像的形状。

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. I've tried to draw shapes using java2d, for example, Ellipse2D, and the rendering of the button only takes a very short time. anyone experienced in this field can teach me how do i generate a shape that is the border of a BufferedImage? i use the class above to create the shape from PNG image with a transparent background.

推荐答案

有关提示,看看一个平滑锯齿路径。获得(原油)大纲的算法是在最后的版本相对较快。创建一个的GeneralPath 是令人惊讶的不是追加快的对象。

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.

重要的部分是此方法:

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);
}

这篇关于爪哇 - 来自全球图像边界创建一个形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆