Class: ThinClient::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/thinclient/image.rb

Class Method Summary collapse

Class Method Details

.getPosition(img1, img2, similar = 0.8) ⇒ Object

查找图片坐标.任何错误导致的查找失败都返回坐标(0,0),查找成功返回具体坐标和查找时匹配的图片名称



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/thinclient/image.rb', line 14

def self.getPosition(img1, img2, similar = 0.8)
	position = {:X => 0, :Y => 0, :resultImg => ""}
	if (ThinClient::Error::SUCCESS != setImageMatch())
		return position
	end
	if not File.exist?(img1)
		print("Image #{img1} Not Found\n")
		return position
	end
	if not File.exist?(img2)
		print("Image #{img2} Not Found\n")
		return position
	end

	info = `#{@ImageMatch} #{img1} #{img2} #{similar}`
	if 0 === info.split(/,/)[0]
		print("#{img1} and #{img2} Not Match\n")
		return position
	end

	position[:X] = info.split(/,/)[0].to_i
	position[:Y] = info.split(/,/)[1].to_i
	position[:resultImg] = info.split(/,/)[2].to_s
	return position
end

.getPositionOnScreen(img, similar = 0.8) ⇒ Object

在当前屏幕中查找指定图片的坐标



41
42
43
44
45
46
# File 'lib/thinclient/image.rb', line 41

def self.getPositionOnScreen(img, similar = 0.8)
	if false == screenCap("tmp.png")
		return false
	end
	return getPosition("tmp.png", img, similar)
end

.include?(img1, img2, similar = 0.8) ⇒ Boolean

查找图片是否存在

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/thinclient/image.rb', line 49

def self.include?(img1, img2, similar = 0.8)
	position = getPosition(img1, img2, similar)
	if (position[:X] != 0 && position[:Y] != 0)
		return true
	end
	return false
end

.includeOnScreen?(img, similar = 0.8) ⇒ Boolean

在当前屏幕中是否存在指定图片

Returns:

  • (Boolean)


58
59
60
61
62
63
64
# File 'lib/thinclient/image.rb', line 58

def self.includeOnScreen?(img, similar = 0.8)
	if false == screenCap("tmp.png")
		return false
	end
	return include?("tmp.png", img, similar)

end

.mouseClick(img, similar = 0.8) ⇒ Object

通过鼠标点击图片(无论是瘦客户机的还是虚拟桌面的都可以点击到)



80
81
82
83
84
85
86
87
88
89
# File 'lib/thinclient/image.rb', line 80

def self.mouseClick(img, similar = 0.8)
	position = getPositionOnScreen(img, similar)
	if (position[:X] === 0 || position[:Y] === 0)
		return false
	end
	#print ("position:#{position}\n")
	Mouse.moveTo(position[:X], position[:Y])
	Mouse.click()
	return true
end

.screenCap(name = "tmp.png") ⇒ Object

截图保存



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/thinclient/image.rb', line 67

def self.screenCap(name = "tmp.png")
	tempFile = "/sdcard/screen.png"
	system("adb shell screencap -p #{tempFile}")
	system("adb pull #{tempFile} #{name}")
	unless File.exist?(name)
		print "Screencap Fail"
		return false
	end
	return true

end

.setImageMatch(path = "ImageMatch.exe") ⇒ Object

图片匹配程序ImageMatch.exe.如果没有找到则返回NO_IMAGEMATCH



4
5
6
7
8
9
10
11
# File 'lib/thinclient/image.rb', line 4

def self.setImageMatch(path = "ImageMatch.exe")
	@ImageMatch = path.nil? ? "ImageMatch.exe" : path
	if not File.exist?(@ImageMatch)
		print("ImageMatch.exe Not Found\n")
		return ThinClient::Error::NO_IMAGEMATCH
	end
	return ThinClient::Error::SUCCESS
end

.tap(img, similar = 0.8) ⇒ Object

触摸图片(这个操作无法触摸到虚拟桌面内的图片)



92
93
94
95
96
97
98
99
# File 'lib/thinclient/image.rb', line 92

def self.tap(img, similar = 0.8)
	position = getPositionOnScreen(img, similar)
	if (position[:X] === 0 || position[:Y] === 0)
		return false
	end
	#print ("position:#{position}\n")
	system("adb shell input tap #{position[:X]} #{position[:Y]}")
end