koishi-plugin-codec-tools 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +67 -0
- package/package.json +5 -3
- package/readme.md +2 -16
package/lib/index.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Config = exports.using = exports.name = void 0;
|
|
7
|
+
exports.apply = apply;
|
|
8
|
+
const koishi_1 = require("koishi");
|
|
9
|
+
const jimp_1 = __importDefault(require("jimp"));
|
|
10
|
+
const jsqr_1 = __importDefault(require("jsqr"));
|
|
11
|
+
const library_1 = require("@zxing/library");
|
|
12
|
+
exports.name = 'codec-tools';
|
|
13
|
+
exports.using = ['i18n'];
|
|
14
|
+
exports.Config = koishi_1.Schema.object({}).description('编解码工具插件配置(暂无配置项)');
|
|
15
|
+
function apply(ctx, config) {
|
|
16
|
+
ctx.i18n.define('zh-CN', {
|
|
17
|
+
'codec-tools.no-image': '请发送包含图片的消息',
|
|
18
|
+
'codec-tools.no-code': '未识别到二维码或条形码',
|
|
19
|
+
'codec-tools.qr-content': '二维码内容:{content}',
|
|
20
|
+
'codec-tools.barcode-content': '条形码内容:{content}',
|
|
21
|
+
});
|
|
22
|
+
ctx.command('url-encode <text:text>', 'URL 编码 - 对文本进行 URL 编码')
|
|
23
|
+
.action((_, text) => encodeURIComponent(text));
|
|
24
|
+
ctx.command('url-decode <text:text>', 'URL 解码 - 对 URL 编码的文本进行解码')
|
|
25
|
+
.action((_, text) => decodeURIComponent(text));
|
|
26
|
+
ctx.command('base64-encode <text:text>', 'Base64 编码 - 对文本进行 Base64 编码')
|
|
27
|
+
.action((_, text) => Buffer.from(text).toString('base64'));
|
|
28
|
+
ctx.command('base64-decode <text:text>', 'Base64 解码 - 对 Base64 编码的文本进行解码')
|
|
29
|
+
.action((_, text) => Buffer.from(text, 'base64').toString());
|
|
30
|
+
ctx.command('unicode-encode <text:text>', 'Unicode 编码 - 将文本转换为 Unicode 转义格式')
|
|
31
|
+
.action((_, text) => text.split('').map(c => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0')).join(''));
|
|
32
|
+
ctx.command('unicode-decode <text:text>', 'Unicode 解码 - 将 Unicode 转义格式还原为原文本')
|
|
33
|
+
.action((_, text) => text.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))));
|
|
34
|
+
ctx.command('decode-code', '识别二维码/条形码 - 解析消息图片中的二维码或条形码内容')
|
|
35
|
+
.action(async ({ session }) => {
|
|
36
|
+
const image = session?.elements.find(el => el.type === 'image');
|
|
37
|
+
if (!image)
|
|
38
|
+
return session.text('codec-tools.no-image');
|
|
39
|
+
const url = image.attrs.src;
|
|
40
|
+
const buffer = await ctx.http.get(url, { responseType: 'arraybuffer' });
|
|
41
|
+
const img = await jimp_1.default.read(Buffer.from(buffer));
|
|
42
|
+
const imageData = {
|
|
43
|
+
data: new Uint8ClampedArray(img.bitmap.data),
|
|
44
|
+
width: img.bitmap.width,
|
|
45
|
+
height: img.bitmap.height
|
|
46
|
+
};
|
|
47
|
+
const qrResult = (0, jsqr_1.default)(imageData.data, imageData.width, imageData.height);
|
|
48
|
+
if (qrResult)
|
|
49
|
+
return session.text('codec-tools.qr-content', { content: qrResult.data });
|
|
50
|
+
const hints = new Map();
|
|
51
|
+
hints.set(library_1.DecodeHintType.POSSIBLE_FORMATS, [
|
|
52
|
+
library_1.BarcodeFormat.QR_CODE, library_1.BarcodeFormat.CODE_128, library_1.BarcodeFormat.EAN_13,
|
|
53
|
+
library_1.BarcodeFormat.EAN_8, library_1.BarcodeFormat.UPC_A, library_1.BarcodeFormat.UPC_E,
|
|
54
|
+
library_1.BarcodeFormat.CODE_39, library_1.BarcodeFormat.ITF
|
|
55
|
+
]);
|
|
56
|
+
const reader = new library_1.MultiFormatReader();
|
|
57
|
+
try {
|
|
58
|
+
const luminanceSource = new library_1.RGBLuminanceSource(imageData.data, imageData.width, imageData.height);
|
|
59
|
+
const binaryBitmap = new library_1.BinaryBitmap(new library_1.HybridBinarizer(luminanceSource));
|
|
60
|
+
const barcodeResult = reader.decode(binaryBitmap, hints);
|
|
61
|
+
return session.text('codec-tools.barcode-content', { content: barcodeResult.text });
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
return session.text('codec-tools.no-code');
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-codec-tools",
|
|
3
3
|
"description": "A Koishi plugin for URL/Base64/Unicode encoding/decoding and barcode/qrcode decoding from images",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.3",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
],
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"scripts": {
|
|
13
|
-
"build": "
|
|
13
|
+
"build": "tsc -b"
|
|
14
14
|
},
|
|
15
15
|
"keywords": [
|
|
16
16
|
"chatbot",
|
|
@@ -31,7 +31,9 @@
|
|
|
31
31
|
"url": "https://github.com/Minecraft-1314/koishi-plugin-codec-tools/issues"
|
|
32
32
|
},
|
|
33
33
|
"homepage": "https://github.com/Minecraft-1314/koishi-plugin-codec-tools#readme",
|
|
34
|
-
"devDependencies": {
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"typescript": "^5.3.3"
|
|
36
|
+
},
|
|
35
37
|
"dependencies": {
|
|
36
38
|
"@zxing/library": "^0.21.3",
|
|
37
39
|
"jimp": "^0.16.13",
|
package/readme.md
CHANGED
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
## 项目介绍 (Project Introduction)
|
|
4
4
|
|
|
5
5
|
### 中文
|
|
6
|
-
这是一个为 Koishi
|
|
6
|
+
这是一个为 Koishi 聊天机器人框架开发的编解码工具插件。该插件具有以下特点:
|
|
7
7
|
- 支持 URL 编码/解码
|
|
8
8
|
- 支持 Base64 编码/解码
|
|
9
9
|
- 支持 Unicode 编码/解码
|
|
10
10
|
- 支持从图片中解析二维码/条形码内容
|
|
11
11
|
|
|
12
12
|
### English
|
|
13
|
-
This is
|
|
13
|
+
This is an encoding/decoding tool plugin developed for the Koishi chatbot framework. This plugin has the following features:
|
|
14
14
|
- Support for URL encoding/decoding
|
|
15
15
|
- Support for Base64 encoding/decoding
|
|
16
16
|
- Support for Unicode encoding/decoding
|
|
@@ -40,20 +40,6 @@ This is a codec tool plugin developed for the Koishi chatbot framework, built on
|
|
|
40
40
|
| unicode-decode <text> | Decode the specified Unicode-encoded text |
|
|
41
41
|
| decode-code | Parse QR code/barcode content from the attached image |
|
|
42
42
|
|
|
43
|
-
## 依赖说明 (Dependencies)
|
|
44
|
-
|
|
45
|
-
### 中文
|
|
46
|
-
该插件运行依赖以下第三方库:
|
|
47
|
-
- jimp:图片处理
|
|
48
|
-
- jsqr:二维码解析
|
|
49
|
-
- @zxing/library:条形码解析
|
|
50
|
-
|
|
51
|
-
### English
|
|
52
|
-
This plugin depends on the following third-party libraries to run:
|
|
53
|
-
- jimp: Image processing
|
|
54
|
-
- jsqr: QR code parsing
|
|
55
|
-
- @zxing/library: Barcode parsing
|
|
56
|
-
|
|
57
43
|
## 项目贡献者 (Contributors)
|
|
58
44
|
| 贡献者 (Contributor) | 贡献内容 (Contribution) |
|
|
59
45
|
| --- | --- |
|