-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
971 lines (927 loc) · 29.1 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>BilibiliOcr</title>
<style>
.canvas {
position: relative;
/* height: 260px;
width: 260px; */
}
.pos {
color: white;
display: flex;
align-items: center;
justify-content: center;
/* position: absolute; */
/* height: 25px;
width: 25px; */
border: 3px solid white;
border-radius: 50%;
background-color: #539ffe;
box-sizing: border-box;
box-shadow: 0 0 10px black;
}
</style>
</head>
<body>
<div class="canvas"></div>
<!-- <img src="https://static.geetest.com/nerualpic/word_l1_zh_2020.03.16/harley1/2cbacc3068dbde3905fa848726eab3d2.jpg" alt=""> -->
</body>
<script>
class BilibiliOcr {
constructor(props) {
// props: url, el
this.props = props;
this.data = {
img: {
primaryWidth: 324,
primaryHeight: 324,
// width: 324,
// height: 324,
width: 260, // 其实就是可视范围的宽,也可以在el中取宽,但还是先分开再说
height: 260,
countAvg: 80, // 对比值 - 差
countStart: 2, // 对比起始值
countEnd: 20, // 结束值 --- 超出则算是背景图
recWidth: 50, // 锁定盒子宽度(越小盒子越多) - 该值修改可能recHandle内部也要微调
bigDataMax: 7000, // 像素点超出不计算
dupImpurityValue: 200, // 去除范围杂质(数值以内的矩形不考虑)保证个数到校验的数量含以上
recWidthAgain: 80, // 第二次锁定盒子宽度,扩大选取范围 - 该值修改可能recRender内部也要微调
diff: 60, // 计算像素点可包含的容差值
continuityDiff: 15, // 二次过滤像素点容差值 - 该值修改可能recRenderDataFilter内部也要微调
posDiff: 30, // 选中矩形与字符进行像素点比较值,若没有则会递归变大
canvas: "",
ctx: "",
// 1296 为下一个起始列值
// nextCol: 1296,
// 被选中的点位
bigData: [],
// data数值
imageData: {},
// 原始数据
oldImageData: {},
// 被选中的矩形方阵数据
recData: [],
// 被选中的矩形方阵数据 - 转换为颜色 - 渲染就绪
colorArr: [],
...props.img
},
verImg: {
width: 116,
height: 40,
// 验证码跟图是在一块的,则x、y是从该图的坐标开始截取
verImgX: 0,
verImgY: 344,
canvas: "",
ctx: "",
imageData: {},
// 几个字
num: 0,
// 字数数据
textArr: [],
...props.verImg
},
pos: {
width: 25
// height: "25px",
// position: "absolute"
}
};
this.call = {
success: () => {},
fail: () => {}
};
this.init();
}
init() {
this.verImgInit();
// this.imgInit()
}
verImgInit() {
const img = new Image();
const _this = this;
let { canvas, ctx, width, height, verImgX, verImgY } = this.data.verImg;
img.crossOrigin = "Anonymous";
img.src = this.props.url;
img.onload = function() {
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
canvas.width = width;
canvas.height = height;
ctx.drawImage(
this,
verImgX,
verImgY,
width,
height,
0,
0,
width,
height
);
_this.data.verImg.canvas = canvas;
_this.data.verImg.ctx = ctx;
// document.body.appendChild(canvas);
_this.verImgHandle();
};
}
verImgHandle() {
// 检查几个字
const num = this.checkNumber();
this.data.verImg.num = num;
console.log("当前字个数为:", num);
// 不光是这边退出,textPix也没做4个字的分割处理
if (num > 3) {
console.warn("识别数字大于3,跳过");
this.call.fail();
return;
}
// 计算像素点
const arr = this.textPix();
console.log("校验字体像素点为:", arr);
// arr.map(item => console.log(`第${item.key}个像素点为`, item.val))
this.data.verImg.textArr = arr;
// 初始化图片
this.imgInit();
}
imgInit() {
const { width, height, primaryWidth, primaryHeight } = this.data.img;
const img = new Image();
const _this = this;
img.crossOrigin = "Anonymous";
img.src = this.props.url;
img.onload = function() {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
// canvas.width = 344
canvas.width = width;
canvas.height = height;
ctx.drawImage(
this,
10,
10,
primaryWidth,
primaryHeight,
0,
0,
width,
height
);
// ctx.drawImage(this, 0, 0)
// TODO 测试
// canvas.width = 100
// canvas.height = 100
// ctx.drawImage(this, 60, 195, 100, 100, 0, 0, 100, 100)
_this.data.img.canvas = canvas;
_this.data.img.ctx = ctx;
// document.querySelector(".canvas").appendChild(canvas);
_this.imgHandle();
};
}
imgHandle() {
let { width, height, ctx } = this.data.img;
this.data.img.imageData = ctx.getImageData(0, 0, width, height);
this.data.img.oldImageData = JSON.parse(
JSON.stringify(this.data.img.imageData)
);
// 置灰
this.grayscale();
// 初步捕获对比度
this.pixels();
// 矩形降噪
this.recHandle();
// 矩形渲染
this.recRender();
// 重置像素点 - 会影响效果 注释吧
// this.rezCanvas()
// 计算像素点 - 及计算区域中的高频反差像素
this.computePix();
// 打点
this.posRender();
}
pixels() {
const { imageData, countAvg, countStart, countEnd } = this.data.img;
// 100 * 100 * 4 = 40000 像素点
// 100 * 1 * 4 = 400
// 前后对比值 突然超过 90 且 超过后在 > 2 < 10 个像素点内 比开始的对比值高 即可,若超过10个像素仍然很高,则放弃
const _this = this;
let data = imageData.data;
let preVal,
nowVal,
isBig,
bigCount = 0,
bigData = [];
for (let i = 0, j = 0; i < data.length; i += 4, j += 4) {
if (!preVal) {
preVal = data[i];
// 当前无值,则跳过第一次
if (!nowVal) {
continue;
}
}
nowVal = data[i];
if (Math.abs(preVal - nowVal) > countAvg && bigCount < countEnd) {
bigCount++;
bigData.push(i);
} else {
bigCount = 0;
bigData = [];
preVal = data[i];
}
if (bigCount > countStart && bigCount < countEnd) {
for (let _i = 0; _i < bigData.length; _i++) {
data[bigData[_i]] = 0;
data[bigData[_i] + 1] = 255;
data[bigData[_i] + 2] = 0;
_this.data.img.bigData.push(bigData[_i]);
}
bigData = [];
}
}
this.data.img.ctx.putImageData(imageData, 0, 0);
}
// 矩形去噪 - 盒子宽度
recHandle(recWidth = this.data.img.recWidth) {
const {
imageData,
width,
bigDataMax,
dupImpurityValue
} = this.data.img;
const nextCol = width * 4;
let data = imageData.data;
let bigData = this.data.img.bigData;
let newBigData = [];
// 盒子统计数据
let recData = [];
let recDataAvg;
if (bigData.length > bigDataMax) {
console.warn(
`捕获到超出${bigDataMax}的像素点,过于复杂不计算`,
bigData.length
);
this.call.fail();
return;
}
if (!bigData.length) {
console.warn("无像素点捕获,退出");
this.call.fail();
return;
}
// console.log("%c" + this.data.img.bigData, "color: green")
// TODO 可以修改i值调试位置
for (let i = 0; i < bigData.length; i++) {
// 列
let line =
bigData[i] - parseInt(recWidth / 2) * (nextCol - recWidth * 4);
// let line = bigData[i]
// 防止突破顶端
if (line <= 0) {
line = bigData[i] - parseInt(recWidth / 2) * 4;
}
// 防止突破左端
if (nextCol - (line % nextCol) <= recWidth * 4) {
line += recWidth * 4 - (nextCol - (line % nextCol));
}
// 防止突破右端
if (nextCol - (line % nextCol) <= recWidth * 4) {
line -= recWidth * 4 - (nextCol - (line % nextCol));
}
recData.push({
data: [],
val: 0,
bigDataKey: i,
sum: 0
});
for (let j = 0; j < recWidth; j++) {
// 行
let j_s = line + j * nextCol;
let j_e = line + j * nextCol + recWidth * 4;
for (let z = j_s; z < j_e; z += 4) {
if (data[z + 1] === 255) {
recData[i].data.push(z);
recData[i].sum++;
}
}
}
// recData[i].val = bigData[i]
recData[i].val = line;
}
// 升序
recData = recData.sort(this.compare("sum"));
// 综合去重
recData = this.dupRemoval(recData);
// 去除范围杂质
recData = this.dupImpurity(recData, dupImpurityValue);
// recDataAvg = (recData[0].sum + recData[recData.length - 1].sum) / 2
console.log("一共点数:", bigData.length);
console.log("降噪后值,取前六名:", recData);
// console.log('平均值:', recDataAvg)
recData.map(item => {
item.data.map(items => {
data[items] = 255;
data[items + 1] = 0;
data[items + 2] = 0;
newBigData.push(items);
});
});
this.data.img.ctx.putImageData(imageData, 0, 0);
this.data.img.recData = recData;
this.data.img.bigData = newBigData;
console.log("捕获点数:", newBigData.length);
}
// 矩形填充
recRender(recWidth = this.data.img.recWidthAgain) {
const {
bigData,
width,
recData,
imageData,
ctx,
canvas
} = this.data.img;
const nextCol = width * 4;
let data = imageData.data;
// let len = recData.length > 6 ? 6 : recData.length
let recRenderData = [];
for (let i = 0; i < recData.length; i++) {
// 15 是 (recWidth - 50 / 2)
let line = recData[i].val - 15 * nextCol - 15 * 4;
if (line < 0) {
line = recData[i].val;
}
recData[i].recRenderData = [];
for (let j = 0; j < recWidth; j++) {
// 行
let j_s = line + j * nextCol;
let j_e = line + j * nextCol + recWidth * 4;
for (let z = j_s; z < j_e; z += 4) {
// TODO 测试矩形位置
// data[z] = 0
// data[z + 1] = 0
// data[z + 2] = 255
data[z + 3] = 100;
recData[i].recRenderData.push(z);
}
}
}
this.data.img.ctx.putImageData(imageData, 0, 0);
}
// 还原canvas
rezCanvas() {
const { imageData, oldImageData } = this.data.img;
let data = imageData.data;
let oldData = oldImageData.data;
for (let i = 0; i < data.length; i += 4) {
data[i] = oldData[i]; // red
data[i + 1] = oldData[i + 1]; // green
data[i + 2] = oldData[i + 2]; // blue
}
this.data.img.ctx.putImageData(imageData, 0, 0);
}
// 计算像素点
computePix() {
const {
recData,
imageData,
diff,
width,
height,
continuityDiff
} = this.data.img;
// TODO 容差值
let data = imageData.data;
let colorArr = [];
let sortArr = [];
// 转换像素点,重组数组
recData.map((item, i) => {
colorArr.push({
key: i,
data: [],
colorMax: 0,
recRenderData: item.recRenderData,
sum: 0,
x: 0,
y: 0,
xyCenter: 0,
chooseNo: 0
});
item.data.map(val => {
colorArr[i].data.push(data[val]);
});
colorArr[i].colorMax = this.colorMax(colorArr[i].data);
});
colorArr.map((item, _i) => {
// if (_i > 0) return
item.recRenderData.map(i => {
if (Math.abs(data[i] - item.colorMax) < diff) {
item.sum++;
data[i] = 255;
data[i + 1] = 0;
data[i + 2] = 0;
// data[i + 3] = 0
}
});
// 矩形去噪 --- 是否要从 大 到小 递归下continuityDiff值?因为有些图去不掉
// 还有各种 纵横长方形的东西 - 长,高,diff值
this.recRenderDataFilter(
item,
data,
"longRec",
continuityDiff * 4,
continuityDiff * width,
continuityDiff
);
this.recRenderDataFilter(
item,
data,
"heightRec",
continuityDiff,
continuityDiff * width * 2 * 2,
continuityDiff
);
// 计算居中值 - 不是取值的居中数而是取数组的中间数
// 矩形像素点为偶数
if (item.recRenderData.length % 2 === 0) {
// 列数 + 行数 = 居中数
item.xyCenter =
item.recRenderData[
item.recRenderData.length / 2 +
Math.sqrt(item.recRenderData.length) / 2
];
} else {
// 奇数
item.xyCenter =
item.recRenderData[parseInt(item.recRenderData.length / 2)];
}
// 计算坐标
item.x = ((item.xyCenter / 4) % width) - 10;
item.y = (item.xyCenter / 4 - item.x) / width - 10;
});
// 添加排序字段
sortArr = JSON.parse(JSON.stringify(colorArr)).sort(
this.compare("sum")
);
sortArr.map((item, i) => {
colorArr.find(items => {
if (item.key === items.key) {
items.chooseNo = i;
return true;
}
});
});
this.data.img.ctx.putImageData(imageData, 0, 0);
this.data.img.colorArr = colorArr;
console.log(colorArr);
}
// 长宽矩形去噪
recRenderDataFilter(
item,
data,
type,
diffILen,
diffJLen,
continuityDiff
) {
const { width, imageData } = this.data.img;
let recRenderDataHandle = [];
let _sum = 0;
item.recRenderData.map((__i, index) => {
// if (index !== 4400) return
for (let j = __i; j < __i + diffILen; j += 4) {
for (let z = j; z < j + diffJLen; z += 4 * width) {
if (data[z] === 255) {
_sum++;
recRenderDataHandle.push(__i);
// data[z] = 0
// data[z + 1] = 255
// data[z + 2] = 0
} else {
_sum = 0;
recRenderDataHandle = [];
break;
}
}
}
// console.log(_sum)
if (_sum >= continuityDiff * 6) {
recRenderDataHandle.map(___i => {
data[___i] = 0;
data[___i + 1] = 255;
data[___i + 2] = 0;
// data[___i + 3] = 255
});
item.sum--;
_sum = 0;
recRenderDataHandle = [];
}
});
}
// 数据匹配以及打点
posRender(diff = this.data.img.posDiff, txAlArr = [], colorAlArr = []) {
const { colorArr } = this.data.img;
const { textArr } = this.data.verImg;
const pos = this.data.pos;
textArr.map(item => {
// 若捕获值跟校验个数相同,则按大到小选中
if (textArr.length === colorArr.length) {
colorArr.find(items => {
if (items.chooseNo === item.chooseNo) {
const dom = document.createElement("div");
dom.className = "pos";
// dom.style.left = items.x + "px";
// dom.style.top = items.y + "px";
dom.innerText = item.key + 1;
// dom.style.visibility = "hidden";
// dom.style = {
// ...dom.style,
// ...pos,
// visibility: "hidden",
// left: items.x + "px",
// top: items.y + "px"
// };
dom.style.height = dom.style.width = pos.width + "px";
dom.style.left = items.x + "px";
dom.style.top = items.y + "px";
dom.style.visibility = "hidden";
dom.style.position = "absolute";
this.props.el && this.props.el.appendChild(dom);
// document.querySelector(".canvas").appendChild(dom);
setTimeout(() => {
this.call.success(
item.key,
this.getOffsetLeft(dom) + pos.width / 2,
this.getOffsetTop(dom) + pos.width / 2,
item.key === textArr.length - 1
);
}, item.key * 200);
txAlArr.push(item.key);
return true;
}
});
return;
}
if (txAlArr.includes(item.key)) {
return;
}
// 若捕获数不相同(其实只可能比字数多,前面已经做校验了),则按照像素点临近的去匹配
colorArr.find(items => {
if (colorAlArr.includes(items.key)) {
return;
}
if (Math.abs(items.sum - item.val) < diff) {
// TODO 这里是模拟渲染,正式脚本环境请转换为模拟点击图片
const dom = document.createElement("div");
dom.className = "pos";
// dom.style = {
// ...dom.style,
// ...pos,
// visibility: "hidden",
// left: items.x + "px",
// top: items.y + "px"
// };
dom.style.height = dom.style.width = pos.width + "px";
dom.style.left = items.x + "px";
dom.style.top = items.y + "px";
dom.style.visibility = "hidden";
dom.style.position = "absolute";
dom.innerText = item.key + 1;
this.props.el && this.props.el.appendChild(dom);
// document.querySelector(".canvas").appendChild(dom);
setTimeout(() => {
this.call.success(
item.key,
this.getOffsetLeft(dom) + pos.width / 2,
this.getOffsetTop(dom) + pos.width / 2,
item.key === textArr.length - 1
);
}, item.key * 200);
txAlArr.push(item.key);
colorAlArr.push(items.key);
return true;
}
});
});
// debugger
// 若最相近的点位找不到,则扩大diff值递归
if (txAlArr.length < textArr.length) {
this.posRender(diff + 30, txAlArr, colorAlArr);
}
}
// 计算color中出现最多的值
colorMax(arr) {
let data = {};
let newArr = [];
// 累积
arr.map(val => {
if (data[val + ""] >= 0) {
data[val + ""]++;
return;
}
data[val + ""] = 0;
});
let max = {
key: 0,
val: 0
};
newArr = Object.keys(data);
// 超过10个像素点才可进行分割
if (newArr.length > 10) {
for (let i = 0; i < newArr.length / 2; i++) {
delete data[newArr[i]];
}
}
// 比较
Object.keys(data).map(key => {
if (data[key] > max.val) {
max = {
key,
val: data[key]
};
}
});
// console.log('color中出现最多的值', max)
return max.key;
}
textPix() {
const { num, width } = this.data.verImg;
const mid = parseInt(width / 4 / 2) * 4;
let arr = [];
let sortArr = [];
if (num === 3) {
arr.push(
{
key: 0,
val: this.isCheckThickness(
mid + parseInt(width / 4) * 4 - 4,
width,
mid + parseInt(width / 4) * 4,
true
)
},
{
key: 1,
val: this.isCheckThickness(
mid + (width / 2) * 4,
width,
width,
true
)
},
{
key: 2,
val: this.isCheckThickness(
width * 4 - 4,
width,
mid + parseInt(width / 4) * 4,
true
)
}
);
} else {
// 2字
arr.push(
{
key: 0,
val: this.isCheckThickness(
parseInt(width / 2) * 4 - 4,
width,
parseInt(width / 2) * 4,
true
)
},
{
key: 1,
val: this.isCheckThickness(
width * 4 - 4,
width,
parseInt(width / 2) * 4,
true
)
}
);
}
// 添加值排序字段(不改变当前顺序)
// TODO
sortArr = JSON.parse(JSON.stringify(arr)).sort(this.compare("val"));
sortArr.map((item, i) => {
arr.find(items => {
if (item.key === items.key) {
items.chooseNo = i;
return true;
}
});
});
return arr;
}
checkNumber() {
let { canvas, width, ctx } = this.data.verImg;
let imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
let data = imageData.data;
const mid = parseInt(width / 4 / 2) * 4;
this.data.verImg.imageData = imageData;
// 测试左侧是否有值
let isL1 = this.isCheckThickness(mid, width);
let isL2 = this.isCheckThickness(mid + parseInt(width / 4) * 4, width);
let isL3 = this.isCheckThickness(mid + (width / 2) * 4, width);
let isL4 = this.isCheckThickness(mid * 3 + (width / 2) * 4, width);
// 处理特殊情况 - 居中线
let isL5 = this.isCheckThickness(mid * 2 + (width / 2) * 2, width);
// 都碰到为4
if (isL1 && isL2 && isL3 && isL4) {
return 4;
}
// 左边没擦到,最右边没擦到
else if (!isL5) {
return 2;
}
return 3;
}
// 检测个数
isCheckThickness(mid, width, fine = 3, isSum = false) {
let { imageData } = this.data.verImg;
const data = imageData.data;
let isTrue = false;
let sum = 0;
for (let i = mid; i < data.length; i += width * 4) {
for (let j = 0; j < fine; j += 4) {
let _i = i - j;
if (data[_i] < 200) {
if (isSum) {
sum++;
} else {
return true;
}
// return true
// break
}
// data[_i] = 255; // red
// data[_i + 1] = 0; // green
// data[_i + 2] = 0; // blue
}
}
this.data.verImg.ctx.putImageData(imageData, 0, 0);
if (isSum) {
return sum * 3;
}
return isTrue;
}
// 置灰
grayscale() {
const { imageData } = this.data.img;
let data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
let avg = (data[i] + data[i + 1] + data[i + 2]) / 3;
data[i] = avg;
data[i + 1] = avg;
data[i + 2] = avg;
}
this.data.img.ctx.putImageData(imageData, 0, 0);
}
// 综合去重
dupRemoval(data) {
let dupArr = [];
return data.map(item => {
let newItem = [];
item.data.map(val => {
if (!dupArr.includes(val)) {
dupArr.push(val);
newItem.push(val);
} else {
item.sum--;
}
});
return {
...item,
data: newItem
};
});
}
// 去杂质 --- 递归直到算到符合校验的个数
dupImpurity(data, val) {
const { num } = this.data.verImg;
let newData = [];
data.map(item => item.sum >= val && newData.push(item));
if (newData.length < num) {
return this.dupImpurity(data, val - 10);
}
return newData;
}
// 升序
compare(property) {
return (now, next) => {
let val1 = now[property];
let val2 = next[property];
return val2 - val1;
};
}
getOffsetTop(el) {
let top = el.offsetTop;
let parent = el.offsetParent;
while (parent) {
top += parent.offsetTop;
parent = parent.offsetParent;
}
return top;
}
getOffsetLeft(el) {
let left = el.offsetLeft;
let parent = el.offsetParent;
while (parent) {
left += parent.offsetLeft;
parent = parent.offsetParent;
}
return left;
}
on(key, fn) {
if (this.call[key]) {
this.call[key] = fn;
}
}
}
const Ocr = new BilibiliOcr({
url: "./img/1.jpg",
el: document.querySelector(".canvas")
});
// 返回的是屏幕坐标
Ocr.on("success", (key, x, y, isEnd) => {
console.log(key, x, y);
});
Ocr.on("fail", () => {
console.log("fail");
});
// b站自动执行脚本
const autoBilibiliOcr = () => {
setTimeout(() => {
// 重置 - 尝试次数过多
if (
document.querySelector(".geetest_panel_error").style.display !==
"none"
) {
document.querySelector(".geetest_panel_error_content").click();
autoBilibiliOcr();
return;
}
// 成功
if (
document.querySelector(".geetest_panel_next").style.display === "none"
) {
return;
}
var Ocr = new BilibiliOcr({
url: document.querySelector(".geetest_item_img").src,
el: document.querySelector(".geetest_item_wrap")
});
// 返回的是屏幕坐标
Ocr.on("success", (key, x, y, isEnd) => {
console.log(key, x, y);
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent(
"click",
true,
false,
null,
0,
0,
0,
x - 135,
y - 180,
false,
false,
false,
false,
0,
null
);
document.querySelector(".geetest_item_wrap").dispatchEvent(evt);
if (isEnd) {
document.querySelector(".geetest_commit").click();
setTimeout(() => {
// 重置 - 等待fail状态结束
if (
document.querySelector(
".geetest_result_tip.geetest_up.geetest_fail"
)
) {
setTimeout(() => {
autoBilibiliOcr();
}, 1500 + parseInt(Math.random() * 1000));
return;
}
}, 500);
}
});
Ocr.on("fail", () => {
// 重置
document.querySelector(".geetest_refresh").click();
console.log("fail");
autoBilibiliOcr();
});
}, 800 + parseInt(Math.random() * 1000));
};
</script>
</html>