【问题标题】:ML Kit Barcode Scanner (used in reactnative-camera) cut displayValue after U+0000 / NULLML Kit Barcode Scanner(用于 react native-camera)剪切显示 U+0000 / NULL 后的值
【发布时间】:2022-01-08 01:07:12
【问题描述】:

我正在尝试使用二进制内容扫描 ECC 数据矩阵代码,但如果有一个 NULL 字节,我只能将字符串到达​​那里。

不幸的是,我无法控制这些矩阵代码,因为我必须扫描提供的代码。 有人知道吗?

是否可以转换原始数据?

如果我将内容作为十六进制值接收就足够了。

rawData 已经是十六进制,但与预期不符,可能它也已损坏或编码未知。

有人知道原始数据的编码吗?

https://developers.google.com/ml-kit/reference/ios/mlkitbarcodescanning/api/reference/Classes/MLKBarcode#rawdata

【问题讨论】:

  • 我打算推荐使用 Apple 的框架而不是 Google 的框架,但我看到 Apple 也将条形码编码的数据作为字符串返回,而不是数据对象。因此,我想 NULL 字节也将是一个问题。 developer.apple.com/documentation/vision/…
  • 感谢您的评论,但我今天在 Javascript 中找到了适合我的解决方案,因为在 Objective C 中,NUL 字节始终是字符串的问题。

标签: objective-c xcode google-mlkit


【解决方案1】:

我找到了适合我的解决方案:

这是我的 React-Native 代码:

import {DataMatrixDecodedBitStreamParser, ZXingStringEncoding} from "@zxing/library";
const bin2hex = (s)=> {
  //  discuss at: https://locutus.io/php/bin2hex/
  // original by: Kevin van Zonneveld (https://kvz.io)
  // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
  // bugfixed by: Linuxworld
  // improved by: ntoniazzi (https://locutus.io/php/bin2hex:361#comment_177616)
  //   example 1: bin2hex('Kev')
  //   returns 1: '4b6576'
  //   example 2: bin2hex(String.fromCharCode(0x00))
  //   returns 2: '00'
  let i;
  let l;
  let o = '';
  let n;
  s += '';
  for (i = 0, l = s.length; i < l; i++) {
    n = s.charCodeAt(i)
        .toString(16);
    o += n.length < 2 ? '0' + n : n;
  }
  return o;
}
const hex2bin = (s)=> {
  //  discuss at: https://locutus.io/php/hex2bin/
  // original by: Dumitru Uzun (https://duzun.me)
  //   example 1: hex2bin('44696d61')
  //   returns 1: 'Dima'
  //   example 2: hex2bin('00')
  //   returns 2: '\x00'
  //   example 3: hex2bin('2f1q')
  //   returns 3: false
  const ret = []
  let i = 0
  let l
  s += ''
  for (l = s.length; i < l; i += 2) {
    const c = parseInt(s.substr(i, 1), 16);
    const k = parseInt(s.substr(i + 1, 1), 16);
    if (isNaN(c) || isNaN(k)) return false;
    ret.push((c << 4) | k);
  }
  return String.fromCharCode.apply(String, ret);
}
const fromHexString = hexString => new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
const matrixcodeRAW2HEX = raw_hex => {
  let data = fromHexString(raw_hex);
  try {
    global.Buffer = global.Buffer || require('buffer').Buffer;
    ZXingStringEncoding.customDecoder = (stringContent, encodingName) => {
      let encodingName2 = encodingName;
      if(encodingName.toLowerCase()=="iso-8859-1"){
        encodingName2="latin1";
      }
      return new Buffer(stringContent).toString(encodingName2);
    }
    ZXingStringEncoding.customEncoder = (stringContent, encodingName) => {
      let encodingName2 = encodingName;
      if(encodingName.toLowerCase()=="iso-8859-1"){
        encodingName2="latin1";
      }
      return new Buffer(stringContent).toString(encodingName2);
    };
    let newData = DataMatrixDecodedBitStreamParser.decode(data);
    return bin2hex(newData.getText());
  }catch (e) {
    console.log(e);
  }
}

我的函数会将原始数据作为十六进制返回,所以NUL没有问题,但如果需要也可以使用hex2bin将其作为Text获取。

我正在为 JS 使用 zxing polyfill => https://github.com/zxing-js/library,因为 JS 不像 Objective C 那样剪切字符串。

我在Objective C中发现NUL总是会切字符串,所以还没有解决办法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-24
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 2021-01-23
    • 2023-03-02
    相关资源
    最近更新 更多