11function OcrTool ( ) {
22 this . img = null ;
3+ this . offset = [ 0 , 0 ] ;
34
4- this . prepare = function ( ) {
5+ this . prepare = function ( landscape ) {
56 // 请求截图并点击开始
67 threads . start ( function ( ) {
78 let sel = packageName ( 'com.android.systemui' ) . text ( '立即开始' ) ;
89 let btn = sel . findOne ( 3000 ) ;
910 if ( btn ) btn . click ( ) ;
1011 } ) ;
11- if ( ! requestScreenCapture ( false ) ) {
12+ if ( ! requestScreenCapture ( landscape ) ) {
1213 throw new Error ( "请求截图失败" ) ;
1314 }
1415 sleep ( 500 ) ;
1516 }
1617
17- this . captureOrClip = function ( rect ) {
18- toastLog ( "截图" ) ;
19- let img = images . captureScreen ( ) ;
20- this . img = rect ? images . clip ( img , rect [ 0 ] , rect [ 1 ] , rect [ 2 ] , rect [ 3 ] ) : img ;
21- }
18+ this . capture = function ( ) {
19+ log ( "截图" ) ;
20+ this . offset = [ 0 , 0 ] ;
21+ this . img = images . captureScreen ( ) ;
22+ return this . img ;
23+ } ;
24+
25+ this . clip = function ( rect ) {
26+ log ( "裁剪" ) ;
27+ this . offset = [ rect [ 0 ] , rect [ 1 ] ] ;
28+ return images . clip ( this . img , rect [ 0 ] , rect [ 1 ] , rect [ 2 ] , rect [ 3 ] ) ;
29+ } ;
30+
31+ this . captureAndClip = function ( rect ) {
32+ this . capture ( ) ;
33+ return this . clip ( rect ) ;
34+ } ;
35+
36+ this . setOffset = function ( x , y ) {
37+ this . offset = [ x , y ] ;
38+ } ;
39+
40+ this . getOffset = function ( ) {
41+ return this . offset ;
42+ } ;
2243
23- this . findText = function ( keywords ) {
24- toastLog ( "OCR开始" ) ;
25- let list = ocr . detect ( this . img ) ;
26- toastLog ( "OCR结束" ) ;
27- this . img . recycle ( ) ;
44+ this . findText = function ( img , keywords , recycle ) {
45+ let list = ocr . detect ( img ) ;
46+ if ( recycle ) img . recycle ( ) ;
2847 let n = keywords . length ;
2948 let result = [ ] ;
3049 let found = 0 ;
@@ -34,7 +53,7 @@ function OcrTool() {
3453 if ( obj . label . indexOf ( keywords [ i ] ) === - 1 ) {
3554 result [ i ] = [ ] ;
3655 } else {
37- result [ i ] = [ obj . bounds . left , obj . bounds . top ] ;
56+ result [ i ] = [ obj . bounds . left + this . offset [ 0 ] , obj . bounds . top + this . offset [ 1 ] ] ;
3857 ++ found ;
3958 }
4059 }
0 commit comments