koishi-plugin-codec-tools 1.0.7 → 1.0.9
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 +51 -30
- package/package.json +1 -1
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');
|
|
@@ -38,42 +59,42 @@ function apply(ctx, config) {
|
|
|
38
59
|
ctx.command('decode-code')
|
|
39
60
|
.action(async ({ session }) => {
|
|
40
61
|
if (!session)
|
|
41
|
-
return;
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
return session.send(
|
|
45
|
-
|
|
46
|
-
content
|
|
47
|
-
]);
|
|
48
|
-
};
|
|
49
|
-
if (!image)
|
|
50
|
-
return replyWithQuote(session.text('.no-image'));
|
|
51
|
-
const url = image.attrs.src;
|
|
52
|
-
const buffer = await ctx.http.get(url, { responseType: 'arraybuffer' });
|
|
53
|
-
const img = await jimp_1.default.read(Buffer.from(buffer));
|
|
54
|
-
const imageData = {
|
|
55
|
-
data: new Uint8ClampedArray(img.bitmap.data),
|
|
56
|
-
width: img.bitmap.width,
|
|
57
|
-
height: img.bitmap.height
|
|
58
|
-
};
|
|
59
|
-
const qrResult = (0, jsqr_1.default)(imageData.data, imageData.width, imageData.height);
|
|
60
|
-
if (qrResult)
|
|
61
|
-
return replyWithQuote(session.text('.qr-content', { content: qrResult.data }));
|
|
62
|
-
const hints = new Map();
|
|
63
|
-
hints.set(library_1.DecodeHintType.POSSIBLE_FORMATS, [
|
|
64
|
-
library_1.BarcodeFormat.QR_CODE, library_1.BarcodeFormat.CODE_128, library_1.BarcodeFormat.EAN_13,
|
|
65
|
-
library_1.BarcodeFormat.EAN_8, library_1.BarcodeFormat.UPC_A, library_1.BarcodeFormat.UPC_E,
|
|
66
|
-
library_1.BarcodeFormat.CODE_39, library_1.BarcodeFormat.ITF
|
|
67
|
-
]);
|
|
68
|
-
const reader = new library_1.MultiFormatReader();
|
|
62
|
+
return '';
|
|
63
|
+
const imageUrl = await extractImageFromSession(session);
|
|
64
|
+
if (!imageUrl) {
|
|
65
|
+
return session.send(session.text('.no-image'), { quote: session.messageId });
|
|
66
|
+
}
|
|
69
67
|
try {
|
|
68
|
+
const buffer = await ctx.http.get(imageUrl, { responseType: 'arraybuffer' });
|
|
69
|
+
const img = await jimp_1.default.read(Buffer.from(buffer));
|
|
70
|
+
if (img.bitmap.width > 1000 || img.bitmap.height > 1000) {
|
|
71
|
+
img.scaleToFit(800, 800);
|
|
72
|
+
}
|
|
73
|
+
img.contrast(0.2);
|
|
74
|
+
const imageData = {
|
|
75
|
+
data: new Uint8ClampedArray(img.bitmap.data),
|
|
76
|
+
width: img.bitmap.width,
|
|
77
|
+
height: img.bitmap.height
|
|
78
|
+
};
|
|
79
|
+
const qrResult = (0, jsqr_1.default)(imageData.data, imageData.width, imageData.height);
|
|
80
|
+
if (qrResult) {
|
|
81
|
+
return session.send(session.text('.qr-content', { content: qrResult.data }), { quote: session.messageId });
|
|
82
|
+
}
|
|
83
|
+
const hints = new Map();
|
|
84
|
+
hints.set(library_1.DecodeHintType.POSSIBLE_FORMATS, [
|
|
85
|
+
library_1.BarcodeFormat.QR_CODE, library_1.BarcodeFormat.CODE_128, library_1.BarcodeFormat.EAN_13,
|
|
86
|
+
library_1.BarcodeFormat.EAN_8, library_1.BarcodeFormat.UPC_A, library_1.BarcodeFormat.UPC_E,
|
|
87
|
+
library_1.BarcodeFormat.CODE_39, library_1.BarcodeFormat.ITF
|
|
88
|
+
]);
|
|
89
|
+
const reader = new library_1.MultiFormatReader();
|
|
70
90
|
const luminanceSource = new library_1.RGBLuminanceSource(imageData.data, imageData.width, imageData.height);
|
|
71
91
|
const binaryBitmap = new library_1.BinaryBitmap(new library_1.HybridBinarizer(luminanceSource));
|
|
72
92
|
const barcodeResult = reader.decode(binaryBitmap, hints);
|
|
73
|
-
return
|
|
93
|
+
return session.send(session.text('.barcode-content', { content: barcodeResult.text }), { quote: session.messageId });
|
|
74
94
|
}
|
|
75
95
|
catch (e) {
|
|
76
|
-
|
|
96
|
+
console.error('解析失败:', e);
|
|
97
|
+
return session.send(session.text('.no-code'), { quote: session.messageId });
|
|
77
98
|
}
|
|
78
99
|
});
|
|
79
100
|
}
|