1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 package complex.bean; 24 25 26 import java.io.File; 27 import java.awt.Rectangle; 28 // import java.awt.BorderLayout; 29 import java.awt.image.BufferedImage; 30 import java.awt.image.PixelGrabber; 31 // import java.awt.event.*; 32 // import java.awt.Frame; 33 import javax.imageio.ImageIO; 34 // import javax.imageio.stream.FileImageOutputStream; 35 36 37 38 class ScreenComparer 39 { 40 Rectangle m_rect; 41 BufferedImage m_img1; 42 BufferedImage m_img2; 43 BufferedImage m_imgDiff; 44 45 int m_diffColor; ScreenComparer(int x, int y, int width, int height)46 public ScreenComparer(int x, int y, int width, int height) 47 { 48 this(new Rectangle(x, y, width, height)); 49 } 50 ScreenComparer(Rectangle location)51 public ScreenComparer(Rectangle location) 52 { 53 m_rect = location; 54 int red = 0xff; 55 int alpha = 0xff; 56 m_diffColor = (alpha << 24); 57 m_diffColor = m_diffColor | (red << 16); 58 } 59 ScreenComparer()60 public ScreenComparer() 61 { 62 this(new Rectangle(0, 0, 0, 0)); 63 } 64 reset()65 public void reset() 66 { 67 m_rect = null; 68 m_img1 = null; 69 m_img2 = null; 70 m_imgDiff = null; 71 } 72 getLocation()73 public Rectangle getLocation() 74 { 75 return m_rect; 76 } grabOne()77 public void grabOne() throws Exception 78 { 79 grabOne(m_rect); 80 } 81 grabOne(Rectangle r)82 public void grabOne(Rectangle r) throws Exception 83 { 84 java.awt.Robot robot = new java.awt.Robot(); 85 m_img1 = robot.createScreenCapture(r); 86 } 87 grabTwo()88 public void grabTwo() throws Exception 89 { 90 grabTwo(m_rect); 91 } 92 grabTwo(Rectangle r)93 public void grabTwo(Rectangle r) throws Exception 94 { 95 java.awt.Robot robot = new java.awt.Robot(); 96 m_img2 = robot.createScreenCapture(r); 97 } 98 compare()99 public boolean compare() throws Exception 100 { 101 if (m_img1 == null || m_img2 == null) 102 { 103 throw new Exception("Only one image captured!"); 104 } 105 boolean ret = true; 106 int w1 = m_img1.getWidth(); 107 int h1 = m_img1.getHeight(); 108 int w2 = m_img2.getWidth(); 109 int h2 = m_img2.getHeight(); 110 111 if (w1 != w2 || h1 != h2) 112 { 113 System.out.println("### 1\n"); 114 //Different size. Create an image that holds both images. 115 int w = w1 > w2 ? w1 : w2; 116 int h = h1 > h2 ? h1 : h2; 117 m_imgDiff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 118 for (int y = 0; y < h; y ++) 119 { 120 for (int x = 0; x < w; x++) 121 { 122 boolean bOutOfRange = false; 123 int pixel1 = 0; 124 int pixel2 = 0; 125 //get the pixel for m_img1 126 if (x < w1 && y < h1) 127 { 128 pixel1 = m_img1.getRGB(x, y); 129 } 130 else 131 { 132 bOutOfRange = true; 133 } 134 135 if (x < w2 && y < h2) 136 { 137 pixel2 = m_img2.getRGB(x, y); 138 } 139 else 140 { 141 bOutOfRange = true; 142 } 143 144 if (bOutOfRange || pixel1 != pixel2) 145 { 146 m_imgDiff.setRGB(x, y, m_diffColor); 147 } 148 else 149 { 150 m_imgDiff.setRGB(x, y, pixel1); 151 } 152 153 } 154 } 155 return false; 156 } 157 158 //Images have same dimension 159 int[] pixels1 = new int[w1 * h1]; 160 PixelGrabber pg = new PixelGrabber( 161 m_img1.getSource(), 0, 0, w1, h1, pixels1, 0, w1); 162 pg.grabPixels(); 163 164 int[] pixels2 = new int[w2 * h2]; 165 PixelGrabber pg2 = new PixelGrabber( 166 m_img2.getSource(), 0, 0, w2, h2, pixels2, 0, w2); 167 pg2.grabPixels(); 168 169 m_imgDiff = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_ARGB); 170 171 //First check if the images differ. 172 int lenAr = pixels1.length; 173 int index = 0; 174 for (index = 0; index < lenAr; index++) 175 { 176 if (pixels1[index] != pixels2[index]) 177 { 178 break; 179 } 180 } 181 182 //If the images are different, then create the diff image 183 if (index < lenAr) 184 { 185 for (int y = 0; y < h1; y++) 186 { 187 for (int x = 0; x < w1; x++) 188 { 189 int offset = y * w1 + x; 190 if (pixels1[offset] != pixels2[offset]) 191 { 192 ret = ret && false; 193 m_imgDiff.setRGB(x, y, m_diffColor); 194 } 195 else 196 { 197 m_imgDiff.setRGB(x, y, pixels1[offset]); 198 } 199 } 200 } 201 } 202 return ret; 203 } 204 205 /** Writes Images to a location. The 206 * directory is determined by the java property OOoBean.Images 207 * 208 */ writeImages()209 public void writeImages() throws Exception 210 { 211 String imgLocation = System.getProperty("OOoBean.Images", ""); 212 File file_tmp = File.createTempFile("OOoBean", "", new File(imgLocation)); 213 File file1 = new File(file_tmp.getPath()+".png"); 214 file_tmp.delete(); 215 if (m_img1 != null) 216 { 217 ImageIO.write(m_img1, "png", file1); 218 System.out.println("\nCompared images:"); 219 System.out.println("1. " + file1.getPath()); 220 } 221 file1= null; 222 file_tmp= null; 223 file_tmp = File.createTempFile("OOoBean", "", new File(imgLocation)); 224 file1 = new File(file_tmp.getPath()+".png"); 225 file_tmp.delete(); 226 if (m_img2 != null) 227 { 228 ImageIO.write(m_img2, "png", file1); 229 System.out.println("2. " + file1.getPath()); 230 } 231 file1= null; 232 file_tmp= null; 233 file_tmp = File.createTempFile("OOoBean", "_diff", new File(imgLocation)); 234 file1 = new File(file_tmp.getPath()+".png"); 235 file_tmp.delete(); 236 if (m_imgDiff != null) 237 { 238 ImageIO.write(m_imgDiff, "png", file1); 239 System.out.println("Diff image: " + file1.getPath() + "\n"); 240 } 241 } 242 243 } 244