Skip to content

Commit 1b49a33

Browse files
committedFeb 20, 2025
8342098: Write a test to compare the images
Backport-of: 47ebf8d868b2e15b943a227ad3cf2ee12eed10f6
1 parent 2ff0232 commit 1b49a33

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.awt.BorderLayout;
25+
import java.awt.Canvas;
26+
import java.awt.Color;
27+
import java.awt.Dimension;
28+
import java.awt.EventQueue;
29+
import java.awt.Font;
30+
import java.awt.Frame;
31+
import java.awt.Graphics;
32+
import java.awt.GraphicsEnvironment;
33+
import java.awt.Point;
34+
import java.awt.Rectangle;
35+
import java.awt.Robot;
36+
import java.awt.image.BufferedImage;
37+
import java.io.File;
38+
import java.io.IOException;
39+
import javax.imageio.ImageIO;
40+
41+
/*
42+
* @test
43+
* @key headful
44+
* @bug 8342098
45+
* @summary Verify that the image captured from the screen using a Robot
46+
* and the source image are same.
47+
* @run main/othervm -Dsun.java2d.uiScale=1 ScreenCaptureRobotTest
48+
*/
49+
public class ScreenCaptureRobotTest {
50+
51+
private static final int IMAGE_WIDTH = 200;
52+
private static final int IMAGE_HEIGHT = 100;
53+
private static final int OFFSET = 10;
54+
55+
private static Frame frame;
56+
private static Canvas canvas;
57+
58+
private static BufferedImage realImage;
59+
60+
private static volatile Point point;
61+
62+
public static void main(String[] args) throws Exception {
63+
try {
64+
EventQueue.invokeAndWait(ScreenCaptureRobotTest::initializeGUI);
65+
doTest();
66+
} finally {
67+
EventQueue.invokeAndWait(ScreenCaptureRobotTest::disposeFrame);
68+
}
69+
}
70+
71+
private static void initializeGUI() {
72+
frame = new Frame("ScreenCaptureRobotTest Frame");
73+
realImage = GraphicsEnvironment.getLocalGraphicsEnvironment()
74+
.getDefaultScreenDevice()
75+
.getDefaultConfiguration()
76+
.createCompatibleImage(IMAGE_WIDTH, IMAGE_HEIGHT);
77+
78+
Graphics g = realImage.createGraphics();
79+
g.setColor(Color.YELLOW);
80+
g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
81+
g.setColor(Color.RED);
82+
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
83+
g.drawString("Capture This", 10, 40);
84+
g.dispose();
85+
86+
canvas = new ImageCanvas();
87+
canvas.setBackground(Color.YELLOW);
88+
canvas.setPreferredSize(new Dimension(IMAGE_WIDTH + (OFFSET * 2),
89+
IMAGE_HEIGHT + (OFFSET * 2)));
90+
frame.setLayout(new BorderLayout());
91+
frame.add(canvas);
92+
frame.pack();
93+
frame.setLocationRelativeTo(null);
94+
frame.setVisible(true);
95+
}
96+
97+
private static void doTest() throws Exception {
98+
Robot robot = new Robot();
99+
robot.waitForIdle();
100+
robot.delay(500);
101+
102+
EventQueue.invokeAndWait(() -> point = canvas.getLocationOnScreen());
103+
104+
Rectangle rect = new Rectangle(point.x + OFFSET, point.y + OFFSET,
105+
IMAGE_WIDTH, IMAGE_HEIGHT);
106+
107+
BufferedImage capturedImage = robot.createScreenCapture(rect);
108+
109+
if (!compareImages(capturedImage, realImage)) {
110+
String errorMessage = "FAIL : Captured Image is different from "
111+
+ "the real image";
112+
saveImage(capturedImage, "CapturedImage.png");
113+
saveImage(realImage, "RealImage.png");
114+
throw new RuntimeException(errorMessage);
115+
}
116+
}
117+
118+
private static boolean compareImages(BufferedImage capturedImg,
119+
BufferedImage realImg) {
120+
int capturedPixel;
121+
int realPixel;
122+
int imgWidth = capturedImg.getWidth();
123+
int imgHeight = capturedImg.getHeight();
124+
125+
if (imgWidth != IMAGE_WIDTH || imgHeight != IMAGE_HEIGHT) {
126+
System.out.println("Captured and real images are different in size");
127+
return false;
128+
}
129+
130+
for (int i = 0; i < imgWidth; i++) {
131+
for (int j = 0; j < imgHeight; j++) {
132+
capturedPixel = capturedImg.getRGB(i, j);
133+
realPixel = realImg.getRGB(i, j);
134+
if (capturedPixel != realPixel) {
135+
System.out.println("Captured pixel ("
136+
+ Integer.toHexString(capturedPixel) + ") at "
137+
+ "(" + i + ", " + j + ") is not equal to real pixel ("
138+
+ Integer.toHexString(realPixel) + ")");
139+
return false;
140+
}
141+
}
142+
}
143+
return true;
144+
}
145+
146+
private static class ImageCanvas extends Canvas {
147+
@Override
148+
public void paint(Graphics g) {
149+
g.drawImage(realImage, OFFSET, OFFSET, this);
150+
}
151+
}
152+
153+
private static void saveImage(BufferedImage image, String fileName) {
154+
try {
155+
ImageIO.write(image, "png", new File(fileName));
156+
} catch (IOException ignored) {
157+
System.err.println(ignored.getMessage());
158+
}
159+
}
160+
161+
private static void disposeFrame() {
162+
if (frame != null) {
163+
frame.dispose();
164+
}
165+
}
166+
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Feb 20, 2025

@openjdk-notifier[bot]
Please sign in to comment.