Java SVG或矢量基数据以形成形状
因此,我正在尝试创建一个工具,它可以将.svg文件类型转换为Java形状或某种类,从而允许我执行.contains(x,y)或.contains(Rectangle2D)。然而,我一直无法找到任何这样做的方法。我发现这篇文章似乎给出了答案,但没有明确描述如何回答。我查看了这些类,不知道如何加载文件,然后将其转换为形状。我最初是用任何一种图像来做这件事的,但结果证明这是不切实际的,而且速度非常慢。代码:Java SVG或矢量基数据以形成形状,java,svg,awt,Java,Svg,Awt,因此,我正在尝试创建一个工具,它可以将.svg文件类型转换为Java形状或某种类,从而允许我执行.contains(x,y)或.contains(Rectangle2D)。然而,我一直无法找到任何这样做的方法。我发现这篇文章似乎给出了答案,但没有明确描述如何回答。我查看了这些类,不知道如何加载文件,然后将其转换为形状。我最初是用任何一种图像来做这件事的,但结果证明这是不切实际的,而且速度非常慢。代码: public static Area toArea(URL url, Color color,
public static Area toArea(URL url, Color color, int tolerance) {
return toArea(toBufferedImage(url), color, tolerance);
}
public static Area toArea(Image image, Color color, int tolerance) {
return toArea(toBufferedImage(image), color, tolerance);
}
/**
* Creates an Area with PixelPerfect precision
*
* @param image
* @param color The color that is draws the Custom Shape
* @param tolerance The color tolerance
* @return Area
*/
public static Area toArea(BufferedImage image, Color color, int tolerance) {
if (image == null) {
return null;
}
Area area = new Area();
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
Color pixel = new Color(image.getRGB(x, y));
if (isIncluded(color, pixel, tolerance)) {
Rectangle r = new Rectangle(x, y, 1, 1);
area.add(new Area(r));
}
}
}
return area;
}
public static Area toArea(URL url) {
return toArea(toBufferedImage(url));
}
public static Area toArea(Image image) {
return toArea(toBufferedImage(image));
}
public static Area toArea(BufferedImage image) {
//Assumes Black as Shape Color
if (image == null) {
return null;
}
Area area = new Area();
Rectangle r;
int y1, y2;
for (int x = 0; x < image.getWidth(); x++) {
y1 = 99;
y2 = -1;
for (int y = 0; y < image.getHeight(); y++) {
Color pixel = new Color(image.getRGB(x, y));
//-16777216 entspricht RGB(0,0,0)
if (pixel.getRGB() == -16777216) {
if (y1 == 99) {
y1 = y;
y2 = y;
}
if (y > (y2 + 1)) {
r = new Rectangle(x, y1, 1, y2 - y1);
area.add(new Area(r));
y1 = y;
y2 = y;
}
y2 = y;
}
}
if ((y2 - y1) >= 0) {
r = new Rectangle(x, y1, 1, y2 - y1);
area.add(new Area(r));
}
}
return area;
}
private static boolean isIncluded(Color target, Color pixel, int tolerance) {
int rT = target.getRed();
int gT = target.getGreen();
int bT = target.getBlue();
int rP = pixel.getRed();
int gP = pixel.getGreen();
int bP = pixel.getBlue();
return ((rP - tolerance <= rT) && (rT <= rP + tolerance)
&& (gP - tolerance <= gT) && (gT <= gP + tolerance)
&& (bP - tolerance <= bT) && (bT <= bP + tolerance));
}
public static BufferedImage toBufferedImage(Image image) {
BufferedImage buffer = new BufferedImage(image.getHeight(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
buffer.createGraphics().drawImage(image, null, null);
return buffer;
}
public static BufferedImage toBufferedImage(URL url) {
try {
return toBufferedImage(ImageIO.read(url));
} catch (IOException ex) {
return null;
}
}
private ImageShaper() {
}
公共静态区域到区域(URL、颜色、整数容差){
返回区域(toBufferedImage(url)、颜色、公差);
}
公共静态区域到区域(图像、颜色、内部公差){
返回区域(缓冲尺寸(图像)、颜色、公差);
}
/**
*创建具有像素完美精度的区域
*
*@param图像
*@param color用于绘制自定义形状的颜色
*@param公差颜色公差
*@返回区
*/
公共静态区域到区域(缓冲区图像、颜色、整数公差){
if(image==null){
返回null;
}
面积=新面积();
对于(int x=0;x(y2+1)){
r=新矩形(x,y1,1,y2-y1);
增加(新区域(r));
y1=y;
y2=y;
}
y2=y;
}
}
如果((y2-y1)>=0){
r=新矩形(x,y1,1,y2-y1);
增加(新区域(r));
}
}
返回区;
}
包含私有静态布尔值(颜色目标、颜色像素、整数容差){
int rT=target.getRed();
int gT=target.getGreen();
int bT=target.getBlue();
int rP=pixel.getRed();
int gP=pixel.getGreen();
int bP=pixel.getBlue();
返回((rP-tolerance请看一看。我们使用它作为将SVG图标转换为Java类的基础,然后将其编译到我们的应用程序中。它基于Apache Batik库,但一旦编译,我们就能够消除对该项目的依赖性…我不明白这是如何处理我的问题的?如果您在在您发布的示例中,试着看看Kirill实现的一个,它可能会给您一些关于该代码的想法,但我记得我曾尝试添加矩形,但发现使用lineTo
速度明显更快。@andrewhompson您能提供一个示例吗?我不确定lineTo函数在哪里。另外,我正在尝试创建一个将SVG转换为形状或具有.contains函数的对象的函数。我提供了该代码作为我之前所做工作的示例。同样,我更希望找到可以解析.SVG的东西,找出要点并将其转换为可以执行的对象。contains(x,y)ex a Shape(java.awt)