koishi-plugin-codec-tools 1.0.7 → 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 +45 -20
- 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');
|
|
@@ -39,40 +60,44 @@ function apply(ctx, config) {
|
|
|
39
60
|
.action(async ({ session }) => {
|
|
40
61
|
if (!session)
|
|
41
62
|
return;
|
|
42
|
-
const image = session.elements.find(el => el.type === 'image');
|
|
43
63
|
const replyWithQuote = (content) => {
|
|
44
64
|
return session.send([
|
|
45
65
|
(0, koishi_1.h)('quote', { id: session.messageId }),
|
|
46
66
|
content
|
|
47
67
|
]);
|
|
48
68
|
};
|
|
49
|
-
|
|
69
|
+
const imageUrl = await extractImageFromSession(session);
|
|
70
|
+
if (!imageUrl)
|
|
50
71
|
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();
|
|
69
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();
|
|
70
94
|
const luminanceSource = new library_1.RGBLuminanceSource(imageData.data, imageData.width, imageData.height);
|
|
71
95
|
const binaryBitmap = new library_1.BinaryBitmap(new library_1.HybridBinarizer(luminanceSource));
|
|
72
96
|
const barcodeResult = reader.decode(binaryBitmap, hints);
|
|
73
97
|
return replyWithQuote(session.text('.barcode-content', { content: barcodeResult.text }));
|
|
74
98
|
}
|
|
75
99
|
catch (e) {
|
|
100
|
+
console.error('解析失败:', e);
|
|
76
101
|
return replyWithQuote(session.text('.no-code'));
|
|
77
102
|
}
|
|
78
103
|
});
|