image.pro\recipe\code_intake.mbt
95.2% · 20 / 21 points · 84 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # code-intake:把图里的条码 / 二维码解成结构化记录 - 3
/// - 4
/// 与另外三条 Recipe 不同的是它**不改像素**:`RecipeResult.bytes` 原样返回输入字节, - 5
/// `codes` 才是产物。这样 CLI / MCP / wasm 三个出口拿到的是同一份记录, - 6
/// 不需要各自再调一次 `image.codec/barcode`。 - 7
- 8
///| - 9
/// 一条命中记录。`kind` 用短名(`ean13` / `upca` / `code128` / `qrcode` / `unknown`), - 10
/// 而不是枚举,方便直接落 JSON。 - 11
pub(all) struct CodeHit { - 12
kind : String - 13
data : String - 14
confidence : Double - 15
x : Int32 - 16
y : Int32 - 17
width : Int32 - 18
height : Int32 - 19
} - 20
- 21
///| - 22
fn barcode_kind_name(t : @barcode.BarcodeType) -> String { - 23●
match t { - 24●
@barcode.BarcodeType::Ean13 => "ean13" - 25●
@barcode.BarcodeType::UpcA => "upca" - 26●
@barcode.BarcodeType::Code128 => "code128" - 27●
@barcode.BarcodeType::QrCode => "qrcode" - 28●
@barcode.BarcodeType::Unknown => "unknown" - 29○
@barcode.BarcodeType::.. => "unknown" - 30
} - 31
} - 32
- 33
///| - 34
/// 解码后的图 → 结构化记录。顺序沿用 `decode_barcode` 的返回顺序(不重排、不去重): - 35
/// 同图多个码全部保留,调用方按坐标自行判定主次。 - 36
pub fn scan_codes(img : @types.Image) -> Array[CodeHit] raise @types.ImageError { - 37●
let found = @barcode.decode_barcode(img) catch { - 38●
e => raise @types.image_error_from_load(e) - 39
} - 40
let out : Array[CodeHit] = [] - 41
for r in found { - 42●
out.push({ - 43●
kind: barcode_kind_name(r.barcode_type), - 44
data: r.data, - 45
confidence: r.confidence, - 46
x: r.x, - 47
y: r.y, - 48
width: r.width, - 49
height: r.height, - 50
}) - 51
} - 52
out - 53
} - 54
- 55
///| - 56
/// Recipe 入口:解码 → 扫描 → 记步。不做编码,也不吃 `RecipeOpts` 的任何字段。 - 57
fn recipe_code_intake( - 58
data : Bytes, - 59
opts : RecipeOpts, - 60
) -> RecipeResult raise @types.ImageError { - 61●
ignore(opts) - 62
let steps : Array[StepRec] = [] - 63●
let img = step_decode(data, steps) - 64●
let codes = scan_codes(img) - 65●
steps.push({ - 66
name: "scan", - 67
note: "hits=\{codes.length()}", - 68
width: img.width, - 69
height: img.height, - 70
bytes: -1, - 71●
ms: seg_ms(), - 72
}) - 73
{ - 74
recipe: "code-intake", - 75
image: img, - 76
bytes: data, - 77●
format: @lib.detect_format(data), - 78
steps, - 79
quality: 0, - 80
redacted: 0, - 81
blurred: false, - 82
codes, - 83
} - 84
}