decode方法报错,Error decoding QR code: e: No MultiFormat Readers were able to detect the code.
对图片截取出二维码或者用选取手机相册的二维码也是一样,二维码没问题
下面是源码
onReady() {
this.ctx = wx.createCameraContext();
this.setZoom();
this.listener = this.ctx.onCameraFrame(this.processFrame.bind(this));
this.listener.start();
},
processFrame(frame) {
const {
width,
height,
data
} = frame;
this.setData({
cutSize: height
});
/* 对当前帧的视频流进行处理 */
let imageData = new Uint8ClampedArray(data);
let result = null
try {
// 检查数据长度是否符合预期:width * height * 4
if (imageData.length !== width * height * 4) {
console.error('图像数据尺寸不匹配');
return;
}
const reader = new MultiFormatReader();
const luminanceSource = new RGBLuminanceSource(imageData, width, height);
const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
const hints = new Map();
// 修改 hints 配置
hints.set(DecodeHintType.TRY_HARDER, true); // 深度扫描
hints.set(DecodeHintType.POSSIBLE_FORMATS, [BarcodeFormat.QR_CODE]); // 限定格式
hints.set(DecodeHintType.PURE_BARCODE, true); // 假设整个图像就是二维码
hints.set(DecodeHintType.CHARACTER_SET, 'UTF-8'); // 指定字符集
hints.set(DecodeHintType.ALSO_INVERTED, true); // 允许反转颜色
result = reader.decode(binaryBitmap, hints);
this.setData({
scanResult: result.getText()
});
//this.stopScan();
} catch (e) {
console.log('Error decoding QR code:', e);
this.setData({
scanResult: e.message
});
}
const ctxZO = wx.createCanvasContext('cutCanvasZO');
wx.canvasPutImageData({
canvasId: 'cutCanvasZO',
x: 0,
y: 0,
width: width,
height: height,
data: imageData,
success: () => {},
fail: (err) => {
console.error('croppedData 失败', err);
}
});
},
灰度化二值化之后再处理已经试过了,也是不行