Skip to content

Instantly share code, notes, and snippets.

@alexanderfloh
Created October 1, 2014 12:55
Show Gist options
  • Save alexanderfloh/443c2328f907843d9452 to your computer and use it in GitHub Desktop.
Save alexanderfloh/443c2328f907843d9452 to your computer and use it in GitHub Desktop.
image copy
package imageCopy;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.imageio.ImageIO;
import com.borland.silktest.jtf.Control;
import com.borland.silktest.jtf.Desktop;
import com.borland.silktest.jtf.common.types.Point;
import org.junit.Test;
import com.borland.silktest.jtf.Window;
import com.borland.silktest.jtf.common.types.MouseButton;
import com.borland.silktest.jtf.TextField;
import com.borland.silktest.jtf.common.types.TextRange;
import com.borland.silktest.jtf.PushButton;
public class ImageCopy {
private static Desktop d= new Desktop();
public static void main(String[] args) throws IOException {
BufferedImage img = ImageIO.read(new File("C:/temp/cat.jpg"));
Raster data = img.getData();
Control canvas = d.find("Untitled - Paint.//Control[@windowClassName='MSPaintView']");
Map<Integer, Set<Point>> points = new HashMap<Integer, Set<Point>>();
for(int x = 0; x < data.getWidth(); x++) {
for(int y = 0; y < data.getHeight(); y++) {
int[] pixel = data.getPixel(x, y, (int[])null);
Point point = new Point(x + 5, y + 5);
Set<Point> ps = points.putIfAbsent(pixel[0], new HashSet(Arrays.asList(point)));
if(ps != null) ps.add(point);
}
}
Point zero = new Point(0, 0);
Set<Entry<Integer, Set<Point>>> entrySet = points.entrySet();
for (Entry<Integer, Set<Point>> entry : entrySet) {
setColor(entry.getKey().intValue());
Set<Point> pointsToPaint = entry.getValue();
Point previousPoint = zero;
for (Point point : pointsToPaint) {
if (tooClose(point, previousPoint)) {
canvas.click(MouseButton.LEFT, zero);
}
canvas.click(MouseButton.LEFT, point);
previousPoint = point;
}
}
}
private static boolean tooClose(Point point, Point previousPoint) {
return Math.sqrt(Math.pow(point.getX() - previousPoint.getX(), 2) + Math.pow(point.getY() - previousPoint.getY(), 2)) <= 10;
}
public static void setColor(final int rgb) {
int b = rgb & 0xFF;
int g = (rgb >> 16) & 0xFF;
int r = (rgb >> 32);
Window window = d.<Window> find("Untitled - Paint.Window");
window.setActive();
window.click(MouseButton.LEFT,
new Point(661, 74));
d.<TextField> find("Untitled - Paint.Edit Colors.Red")
.setSelectionRange(new TextRange(0, 1, 0, 0));
d.<TextField> find("Untitled - Paint.Edit Colors.Red").setText(String.valueOf(r));
d.<TextField> find("Untitled - Paint.Edit Colors.Green").setText(String.valueOf(g));
d.<TextField> find("Untitled - Paint.Edit Colors.Blue").setText(String.valueOf(b));
d.<PushButton> find("Untitled - Paint.Edit Colors.OK").select();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment