koishi-plugin-codec-tools 1.0.0 → 1.0.2
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.d.ts +2 -1
- package/lib/index.js +67 -0
- package/package.json +5 -3
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Context, Schema } from 'koishi';
|
|
2
2
|
export declare const name = "codec-tools";
|
|
3
|
+
export declare const using: readonly ["i18n"];
|
|
3
4
|
export interface Config {
|
|
4
5
|
}
|
|
5
6
|
export declare const Config: Schema<Config>;
|
|
6
|
-
export declare function apply(ctx: Context): void;
|
|
7
|
+
export declare function apply(ctx: Context, config: Config): void;
|
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.2",
|
|
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",
|