2017-01-02 96 views
0

是否可以從HEX建立PDF417條形碼?我用ZXing嘗試過一些東西,但是在我的情況下它不適用於編碼字符串。ZXing.Net PDF417來自HEX的條形碼

HEX: fe-30-09-33-31-37-30-31-30-32-30-31-f9-20-01-34-fe-30-01-20-fc-20-06 

其他發電機可以做到這一點(https://stackoverflow.com/a/39471232/3236231),但現在這個解決方案的成本幾千元。 ZXing符合我的所有需求,但我找不到使用我的數據的合適方式。

回答

2

下面的代碼片斷應該按預期工作:

[Test] 
    public void Hex2Pdf417() 
    { 
    var hexStr = "fe3009333137303130323031f9200134fe300120fc2006"; 
    var byteArray = Enumerable.Range(0, hexStr.Length/2).Select(x => Convert.ToByte(hexStr.Substring(x * 2, 2), 16)).ToArray(); 
    var byteArrayAsString = new String(byteArray.Select(b => (char)b).ToArray()); 

    // encode the string as PDF417 
    var writer = new BarcodeWriter 
    { 
     Format = BarcodeFormat.PDF_417, 
     Options = new PDF417EncodingOptions 
     { 
      Height = 200, 
      Width = 200, 
      Margin = 10 
     } 
    }; 
    var bitmap = writer.Write(byteArrayAsString); 

    // try to decode the PDF417 
    var reader = new BarcodeReader 
    { 
     Options = new DecodingOptions 
     { 
      PossibleFormats = new List<BarcodeFormat> 
      { 
       BarcodeFormat.PDF_417 
      }, 
      PureBarcode = true 
     } 
    }; 
    var result = reader.Decode(bitmap); 

    // make sure, the result is the same as the original hex 
    var resultBackToBytes = result.Text.Select(c => (byte)c).ToArray(); 
    var resultAsHexString = String.Join("", resultBackToBytes.Select(b => b.ToString("x2"))); 

    Assert.That(resultAsHexString, Is.EqualTo(hexStr)); 
    } 
+0

一些HEX像從CP850「81」引發錯誤: 非可編碼字符檢測到:(統一:132) 此外,當我解碼條形碼在線--barcode-reader.inliteresearch.com,它顯示保存的HEX數據不是原始的HEX。 「FE」保存爲「5F」。 – user3236231

+1

由於ZXing.Net當前版本0.14的所有情況下,代碼無法正常工作,因此存在一些小錯誤。 – Michael

+0

非常感謝!它與源代碼中的當前版本完美結合!自編自https://github.com/micjahn/ZXing.Net/! – user3236231