| 1 |
/* ComponentFinder.java |
| 2 |
Copyright (C) 2012 Red Hat, Inc. |
| 3 |
|
| 4 |
This file is part of IcedTea. |
| 5 |
|
| 6 |
IcedTea is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, version 2. |
| 9 |
|
| 10 |
IcedTea is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with IcedTea; see the file COPYING. If not, write to |
| 17 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 |
02110-1301 USA. |
| 19 |
|
| 20 |
Linking this library statically or dynamically with other modules is |
| 21 |
making a combined work based on this library. Thus, the terms and |
| 22 |
conditions of the GNU General Public License cover the whole |
| 23 |
combination. |
| 24 |
|
| 25 |
As a special exception, the copyright holders of this library give you |
| 26 |
permission to link this library with independent modules to produce an |
| 27 |
executable, regardless of the license terms of these independent |
| 28 |
modules, and to copy and distribute the resulting executable under |
| 29 |
terms of your choice, provided that you also meet, for each linked |
| 30 |
independent module, the terms and conditions of the license of that |
| 31 |
module. An independent module is a module which is not derived from |
| 32 |
or based on this library. If you modify this library, you may extend |
| 33 |
this exception to your version of the library, but you are not |
| 34 |
obligated to do so. If you do not wish to do so, delete this |
| 35 |
exception statement from your version. |
| 36 |
*/ |
| 37 |
|
| 38 |
|
| 39 |
package net.sourceforge.jnlp.awt.imagesearch; |
| 40 |
|
| 41 |
import java.awt.Color; |
| 42 |
import java.awt.Point; |
| 43 |
import java.awt.Rectangle; |
| 44 |
import java.awt.image.BufferedImage; |
| 45 |
import java.io.IOException; |
| 46 |
import javax.imageio.ImageIO; |
| 47 |
|
| 48 |
public class ComponentFinder { |
| 49 |
public static final BufferedImage defaultIcon; |
| 50 |
|
| 51 |
static{ |
| 52 |
try { |
| 53 |
defaultIcon = ImageIO.read(ClassLoader.getSystemClassLoader().getResource("net/sourceforge/jnlp/awt/imagesearch/marker.png")); |
| 54 |
} catch (IOException e) { |
| 55 |
throw new RuntimeException("ComponentFinder - problem initializing defaultIcon",e); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* method findColoredRectangle determines coordinates of a rectangle colored |
| 61 |
* by rectangleColor surrounded by a neighbourhood of surroundingColor |
| 62 |
* |
| 63 |
* @param rectangleColor |
| 64 |
* @param surroundingColor |
| 65 |
* @param screenshot |
| 66 |
* @return |
| 67 |
*/ |
| 68 |
public static Rectangle findColoredRectangle(Color rectangleColor, Color surroundingColor, BufferedImage screenshot) { |
| 69 |
|
| 70 |
Rectangle r = ImageSeeker.findColoredAreaGap(screenshot, rectangleColor, surroundingColor, 0, screenshot.getHeight(), 0); |
| 71 |
if( ImageSeeker.isRectangleValid(r)){ |
| 72 |
return r; |
| 73 |
}else{ |
| 74 |
return null; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* method findColoredRectangle determines coordinates of a rectangle colored |
| 80 |
* by rectangleColor surrounded by a neighbourhood of surroundingColor with |
| 81 |
* possible gap of several pixels |
| 82 |
* |
| 83 |
* @param rectangleColor |
| 84 |
* @param surroundingColor |
| 85 |
* @param screenshot |
| 86 |
* @param gap |
| 87 |
* @return |
| 88 |
*/ |
| 89 |
public static Rectangle findColoredRectangle(Color rectangleColor, Color surroundingColor, BufferedImage screenshot, int gap) { |
| 90 |
|
| 91 |
Rectangle r = ImageSeeker.findColoredAreaGap(screenshot, rectangleColor, surroundingColor, 0, screenshot.getHeight(), gap); |
| 92 |
if( ImageSeeker.isRectangleValid(r)){ |
| 93 |
return r; |
| 94 |
}else{ |
| 95 |
return null; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Method findWindowByIcon finds the application area assuming there is a |
| 102 |
* given icon in given position on the application window |
| 103 |
* the dimension of the window has to be given. |
| 104 |
* |
| 105 |
* @param icon |
| 106 |
* @param iconPosition |
| 107 |
* @param windowWidth |
| 108 |
* @param windowHeight |
| 109 |
* @param screenshot |
| 110 |
* @return Rectangle rectangle where the applet resides |
| 111 |
*/ |
| 112 |
public static Rectangle findWindowByIcon(BufferedImage icon, Point iconPosition, int windowWidth, int windowHeight, BufferedImage screenshot) { |
| 113 |
Rectangle r = ImageSeeker.findExactImage(icon, screenshot); |
| 114 |
if( ImageSeeker.isRectangleValid(r)){ |
| 115 |
return windowPositionFromIconPosition(r.getLocation(), iconPosition, windowWidth, windowHeight); |
| 116 |
}else{ |
| 117 |
return null; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public static Rectangle findWindowByIconBlurred(BufferedImage icon, Point iconPosition, int windowWidth, int windowHeight, BufferedImage screenshot, double minCorrelation) { |
| 122 |
Rectangle r = ImageSeeker.findBlurredImage(icon, screenshot, minCorrelation); |
| 123 |
if( ImageSeeker.isRectangleValid(r)){ |
| 124 |
return windowPositionFromIconPosition(r.getLocation(), iconPosition, windowWidth, windowHeight); |
| 125 |
}else{ |
| 126 |
return null; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public static Rectangle windowPositionFromIconPosition(Point iconAbsolute, Point iconRelative, int windowWidth, int windowHeight){ |
| 131 |
return new Rectangle( iconAbsolute.x - iconRelative.x, iconAbsolute.y - iconRelative.y, |
| 132 |
windowWidth, windowHeight); |
| 133 |
} |
| 134 |
} |