☻Blog("Laziji")

System.out.print("辣子鸡的博客");

安装

https://github.com/microsoft/terminal/releases

1
PowerShell -Command "Set-ExecutionPolicy RemoteSigned -scope Process; iwr -useb https://raw.githubusercontent.com/gerardog/gsudo/master/installgsudo.ps1 | iex"

sudo管理员权限

settings.json profiles.list

1
2
3
4
5
6
7
8
9
{
"guid": "{41dd7a51-f0e1-4420-a2ec-1a7130b7e950}",
"name": "Windows PowerShell Elevated",
"commandline": "gsudo.exe powershell.exe",
"hidden": false,
"colorScheme": "Solarized Dark",
"fontFace": "Fira Code",
"icon": "C:\\shell\\icon.png"
},

背景

setting.json profile.default

1
2
3
4
5
6
7
8
9
10
"defaults": {
"startingDirectory": ".",
"useAcrylic": true,
"acrylicOpacity": 0.6,
"colorScheme": "Dracula2",
"fontSize": 11,
"backgroundImage": "C:\\shell\\bg1.jpg",
"backgroundImageOpacity": 0.2,
"backgroundImageStrechMode": "fill",
},

##加入右键菜单
https://raw.githubusercontent.com/microsoft/terminal/main/res/terminal.ico

wt.reg

1
2
3
4
5
6
7
8
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt]
@="Windows Terminal here"
"Icon"="C:\\shell\\terminal.ico"

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt\command]
@="C:\\Users\\laziji\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe"

settings.json profiles.default

1
"startingDirectory": "."

关键步骤

  • 加上干扰点
    1
    2
    3
    4
    for (let i = 0.05 * w * h; i > 0; i--) {
    ctx.fillStyle = randomColor(0, 256);
    ctx.fillRect(randomInt(0, w), randomInt(0, h), 1, 1);
    }
  • 显示数字
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    ctx.font = `${h - 4}px Consolas`;
    ctx.fillStyle = randomColor(160, 200);
    let value = "";
    for (let i = 0; i < n; i++) {
    let x = (w - 10) / n * i + 5,
    y = h - 12;
    let r = Math.PI * randomFloat(-0.12, 0.12);
    let ch = CHARTS[randomInt(0, CHARTS.length)];
    value += ch;
    ctx.translate(x, y);
    ctx.rotate(r);
    ctx.fillText(ch, 0, 0);
    ctx.rotate(-r);
    ctx.translate(-x, -y);
    }
  • 获取验证码图片base64值
    1
    canvas.toDataURL('image/jpg')

完整实现

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
<!DOCTYPE html>
<html>

<head>
<title></title>
</head>

<body>
<img id="img">
<script type="text/javascript">
function getCaptcha(w, h, n) {
const CHARTS = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ".split("");
const randomInt = (s, e) => {
if (s > e) {
let t = s;
s = e;
e = t;
}
s = Math.ceil(s);
e = Math.floor(e);
return s + Math.floor(Math.random() * (e - s))
}
const randomFloat = (s, e) => {
return s + Math.random() * (e - s);
}
const randomColor = (s, e) => {
return `rgb(${randomInt(s,e)},${randomInt(s,e)},${randomInt(s,e)})`;
}

let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');

ctx.rect(0, 0, w, h);
ctx.clip();

ctx.fillStyle = randomColor(200, 250);
ctx.fillRect(0, 0, w, h);

for (let i = 0.05 * w * h; i > 0; i--) {
ctx.fillStyle = randomColor(0, 256);
ctx.fillRect(randomInt(0, w), randomInt(0, h), 1, 1);
}

ctx.font = `${h - 4}px Consolas`;
ctx.fillStyle = randomColor(160, 200);
let value = "";
for (let i = 0; i < n; i++) {
let x = (w - 10) / n * i + 5,
y = h - 12;
let r = Math.PI * randomFloat(-0.12, 0.12);
let ch = CHARTS[randomInt(0, CHARTS.length)];
value += ch;
ctx.translate(x, y);
ctx.rotate(r);
ctx.fillText(ch, 0, 0);
ctx.rotate(-r);
ctx.translate(-x, -y);
}

let base64Src = canvas.toDataURL('image/jpg');
return {
value,
base64Src
};
}
let res = getCaptcha(100, 40, 4);
console.log(res);
document.querySelector("#img").src = res.base64Src;
</script>
</body>

</html>

输出

1
2
3
4
{
base64Src: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASw....",
value: "Z9KF"
}

效果展示

验证码

0%