koishi-plugin-codec-tools 1.0.6 → 1.0.8

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 CHANGED
@@ -18,6 +18,27 @@ exports.Config = koishi_1.Schema.object({})
18
18
  .i18n({
19
19
  'zh-CN': {},
20
20
  });
21
+ async function extractImageFromSession(session) {
22
+ if (session.quote) {
23
+ try {
24
+ const quotedMsg = await session.bot.getMessage(session.channelId, session.quote.messageId);
25
+ if (quotedMsg && quotedMsg.elements) {
26
+ const quotedImage = quotedMsg.elements.find(el => el.type === 'image');
27
+ if (quotedImage) {
28
+ return quotedImage.attrs.src;
29
+ }
30
+ }
31
+ }
32
+ catch (e) {
33
+ console.warn('获取引用消息失败:', e);
34
+ }
35
+ }
36
+ const currentImage = session.elements.find(el => el.type === 'image');
37
+ if (currentImage) {
38
+ return currentImage.attrs.src;
39
+ }
40
+ return null;
41
+ }
21
42
  function apply(ctx, config) {
22
43
  const zhCNPath = path_1.default.join(__dirname, './locales/zh-CN.yml');
23
44
  const zhCNContent = fs_1.default.readFileSync(zhCNPath, 'utf8');
@@ -37,35 +58,47 @@ function apply(ctx, config) {
37
58
  .action((_, text) => text.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))));
38
59
  ctx.command('decode-code')
39
60
  .action(async ({ session }) => {
40
- const image = session?.elements.find(el => el.type === 'image');
41
- if (!image)
42
- return session.text('.no-image');
43
- const url = image.attrs.src;
44
- const buffer = await ctx.http.get(url, { responseType: 'arraybuffer' });
45
- const img = await jimp_1.default.read(Buffer.from(buffer));
46
- const imageData = {
47
- data: new Uint8ClampedArray(img.bitmap.data),
48
- width: img.bitmap.width,
49
- height: img.bitmap.height
61
+ if (!session)
62
+ return;
63
+ const replyWithQuote = (content) => {
64
+ return session.send([
65
+ (0, koishi_1.h)('quote', { id: session.messageId }),
66
+ content
67
+ ]);
50
68
  };
51
- const qrResult = (0, jsqr_1.default)(imageData.data, imageData.width, imageData.height);
52
- if (qrResult)
53
- return session.text('.qr-content', { content: qrResult.data });
54
- const hints = new Map();
55
- hints.set(library_1.DecodeHintType.POSSIBLE_FORMATS, [
56
- library_1.BarcodeFormat.QR_CODE, library_1.BarcodeFormat.CODE_128, library_1.BarcodeFormat.EAN_13,
57
- library_1.BarcodeFormat.EAN_8, library_1.BarcodeFormat.UPC_A, library_1.BarcodeFormat.UPC_E,
58
- library_1.BarcodeFormat.CODE_39, library_1.BarcodeFormat.ITF
59
- ]);
60
- const reader = new library_1.MultiFormatReader();
69
+ const imageUrl = await extractImageFromSession(session);
70
+ if (!imageUrl)
71
+ return replyWithQuote(session.text('.no-image'));
61
72
  try {
73
+ const buffer = await ctx.http.get(imageUrl, { responseType: 'arraybuffer' });
74
+ const img = await jimp_1.default.read(Buffer.from(buffer));
75
+ if (img.bitmap.width > 1000 || img.bitmap.height > 1000) {
76
+ img.scaleToFit(800, 800);
77
+ }
78
+ img.contrast(0.2);
79
+ const imageData = {
80
+ data: new Uint8ClampedArray(img.bitmap.data),
81
+ width: img.bitmap.width,
82
+ height: img.bitmap.height
83
+ };
84
+ const qrResult = (0, jsqr_1.default)(imageData.data, imageData.width, imageData.height);
85
+ if (qrResult)
86
+ return replyWithQuote(session.text('.qr-content', { content: qrResult.data }));
87
+ const hints = new Map();
88
+ hints.set(library_1.DecodeHintType.POSSIBLE_FORMATS, [
89
+ library_1.BarcodeFormat.QR_CODE, library_1.BarcodeFormat.CODE_128, library_1.BarcodeFormat.EAN_13,
90
+ library_1.BarcodeFormat.EAN_8, library_1.BarcodeFormat.UPC_A, library_1.BarcodeFormat.UPC_E,
91
+ library_1.BarcodeFormat.CODE_39, library_1.BarcodeFormat.ITF
92
+ ]);
93
+ const reader = new library_1.MultiFormatReader();
62
94
  const luminanceSource = new library_1.RGBLuminanceSource(imageData.data, imageData.width, imageData.height);
63
95
  const binaryBitmap = new library_1.BinaryBitmap(new library_1.HybridBinarizer(luminanceSource));
64
96
  const barcodeResult = reader.decode(binaryBitmap, hints);
65
- return session.text('.barcode-content', { content: barcodeResult.text });
97
+ return replyWithQuote(session.text('.barcode-content', { content: barcodeResult.text }));
66
98
  }
67
99
  catch (e) {
68
- return session.text('.no-code');
100
+ console.error('解析失败:', e);
101
+ return replyWithQuote(session.text('.no-code'));
69
102
  }
70
103
  });
71
104
  }
@@ -14,9 +14,9 @@ commands:
14
14
  decode-code:
15
15
  description: 识别二维码/条形码
16
16
  messages:
17
- no-image: 请发送包含图片的消息
18
- no-code: 未识别到二维码或条形码
19
- qr-content: 二维码内容:{content}
20
- barcode-content: 条形码内容:{content}
17
+ no-image: 未找到图片!请发送图片或引用包含图片的消息后重试。
18
+ qr-content: 二维码内容:{{content}}
19
+ barcode-content: 条形码内容:{{content}}
20
+ no-code: 未识别到二维码/条形码!
21
21
 
22
22
  _config: {}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-codec-tools",
3
3
  "description": "编码&解码 URL/Base64/Unicode 和条形码/二维码的 Koishi 插件",
4
- "version": "1.0.6",
4
+ "version": "1.0.8",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -14,9 +14,9 @@ commands:
14
14
  decode-code:
15
15
  description: 识别二维码/条形码
16
16
  messages:
17
- no-image: 请发送包含图片的消息
18
- no-code: 未识别到二维码或条形码
19
- qr-content: 二维码内容:{content}
20
- barcode-content: 条形码内容:{content}
17
+ no-image: 未找到图片!请发送图片或引用包含图片的消息后重试。
18
+ qr-content: 二维码内容:{{content}}
19
+ barcode-content: 条形码内容:{{content}}
20
+ no-code: 未识别到二维码/条形码!
21
21
 
22
22
  _config: {}