AVMetadataMachineReadableCodeObject
AVMetadataMachineReadableCodeObject 表示在取景画面里认到的一个码:二维码、商品条码这类。除了继承来的种类和位置,它还带着码里的文字(stringValue)和码的四个角(corners)。
它从 AVCaptureMetadataOutput 的 detect 事件里来。事件的 objects 的元素类型写的是父类 AVMetadataObject,读码的字段之前先用 instanceof AVMetadataMachineReadableCodeObject 判断。
和父类一样,它是一个只读的值。给字段赋值抛 TypeError,信息是 stringValue is read-only 这样的一句(实测)。
它属于 @nacrejs/av-capture 这个包,安装见 AVCaptureSession。
继承关系:AVMetadataObject → AVMetadataMachineReadableCodeObject
构造函数
不能用 new 创建,抛 TypeError,信息是 Illegal constructor: AVMetadataObject comes from the detect event of AVCaptureMetadataOutput(实测)。
实例属性
还继承 AVMetadataObject 的 type 和 bounds。
| 属性 | 说明 |
|---|---|
stringValue 只读 | 码里的文字。内容不是文字时是 null |
corners 只读 | 码的四个角,CGPoint 的数组 |
实例方法
没有。
示例
const output = new AVCaptureMetadataOutput();
output.addEventListener('detect', (e) => {
for (const found of e.objects) {
if (found instanceof AVMetadataMachineReadableCodeObject && found.stringValue !== null) {
console.log(found.type + ' ' + found.stringValue + ',' + found.corners.length + ' 个角');
}
}
});效果:对着二维码扫时,每一帧输出一行,比如 qrCode https://example.com/a,4 个角。实测认到的二维码 type 是 'qrCode',corners 有 4 个点。
对应的 iOS API
AVMetadataMachineReadableCodeObject。stringValue 一样;corners 在 ObjC 里是一组字典,这里是 CGPoint 的数组。
参见
AVMetadataObject:父类,type和boundsdetect- 把认到的码框出来