Class: ScreenDriver::Screen
- Inherits:
-
Object
- Object
- ScreenDriver::Screen
- Defined in:
- lib/driver/screen.rb
Instance Method Summary collapse
- #byte_array(image) ⇒ Object
- #get_image(filename) ⇒ Object
- #img_to_rgb_array(image) ⇒ Object
-
#initialize ⇒ Screen
constructor
A new instance of Screen.
- #put_image(image, filename) ⇒ Object
- #screenshot ⇒ Object
- #search_image(i1, i2) ⇒ Object
Constructor Details
#initialize ⇒ Screen
Returns a new instance of Screen.
4 5 6 |
# File 'lib/driver/screen.rb', line 4 def initialize end |
Instance Method Details
#byte_array(image) ⇒ Object
68 69 70 71 72 |
# File 'lib/driver/screen.rb', line 68 def byte_array(image) os = ByteArrayOutputStream.new ImageIO::write(image, "png", os) return os.toByteArray() end |
#get_image(filename) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/driver/screen.rb', line 59 def get_image(filename) file = java::io::File.new(filename) buf = ImageIO::read(file) conv = BufferedImage.new(buf.getWidth, buf.getHeight, BufferedImage::TYPE_USHORT_565_RGB) conv.getGraphics().drawImage(buf, 0, 0, nil); return conv # return ImageIO::read(file) end |
#img_to_rgb_array(image) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/driver/screen.rb', line 46 def img_to_rgb_array(image) w=image.getWidth-1 h=image.getHeight-1 arr=[] (0..w).each do |x| tmp=[] (0..h).each do |y| tmp.push(image.getRGB(x,y)) end arr.push(tmp) end end |
#put_image(image, filename) ⇒ Object
74 75 76 77 |
# File 'lib/driver/screen.rb', line 74 def put_image(image, filename) file = java::io::File.new(filename) ImageIO::write(image, "png", file) end |
#screenshot ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/driver/screen.rb', line 8 def screenshot toolkit = Toolkit::getDefaultToolkit() screen_size = toolkit.getScreenSize() rect = Rectangle.new(screen_size) robot = Robot.new image = robot.createScreenCapture(rect) conv = BufferedImage.new(image.getWidth, image.getHeight, BufferedImage::TYPE_USHORT_565_RGB) conv.getGraphics().drawImage(image, 0, 0, nil); put_image(conv, "conv.png") return conv end |
#search_image(i1, i2) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/driver/screen.rb', line 20 def search_image(i1, i2) w2=i2.getWidth h2=i2.getHeight firstpx=i2.getRGB(0,0) midpx=i2.getRGB(w2/2, h2/2) lastpx=i2.getRGB(w2-1, h2-1) b2 = byte_array(i2) (0..i1.getWidth-w2).each do |x| (0..i1.getHeight-h2).each do |y| if (i1.getRGB(x+w2/2, y+h2/2) == midpx && i1.getRGB(x,y) == firstpx) find = i1.getSubimage(x, y, w2, h2) #puts x.to_s + " " + y.to_s + " " + i1.getRGB(x, y).to_s b1 = byte_array(find) if (java::util::Arrays.equals(b1, b2)) #puts "Images are Equal" return x+w2/2, y+h2/2 end end end end #puts "BOOOOO No Find" return -1, -1 end |