将QR解码解决方案从Python转换为C#(EU DGC)

2024-05-29 07:13:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将此Python代码转换为C#(理想情况下是.NET核心)。 Source

我的目标是将QR输入字符串转换为另一个包含json数据的字符串。请参阅提供的链接

#! /usr/bin/env python3
import json
import sys
import zlib
 
import base45
import cbor2
from cose.messages import CoseMessage
 
payload = sys.argv[1][4:]
print("decoding payload: "+ payload)
 
# decode Base45 (remove HC1: prefix)
decoded = base45.b45decode(payload)
 
# decompress using zlib
decompressed = zlib.decompress(decoded)
# decode COSE message (no signature verification done)
cose = CoseMessage.decode(decompressed)
# decode the CBOR encoded payload and print as json
print(json.dumps(cbor2.loads(cose.payload), indent=2))

我找不到任何适用于Zlib的NuGet软件包,该软件包可以正常工作。所以我在base45解码后被卡住了。谢谢你的提示

using System.Text; //Rystem.Text.Base45 NuGet
    
var removedHeader = testQrData.Substring(4);
var decoded = removedHeader.FromBase45();
byte[] rawBytes = Encoding.ASCII.GetBytes(decoded);

这个link可能有助于进一步调查

Decoding scheme


Tags: 字符串importjsonsysdecompresspayloaddecodedprint
3条回答

我过去曾使用ZXing库解码和创建二维码:
https://github.com/micjahn/ZXing.Net

你也可以试试:https://github.com/codebude/QRCoder

来自github页面的快速ZXing示例:

// create a barcode reader instance
IBarcodeReader reader = new BarcodeReader();
// load a bitmap
var barcodeBitmap = (Bitmap)Image.LoadFrom("C:\\sample-barcode-image.png");
// detect and decode the barcode inside the bitmap
var result = reader.Decode(barcodeBitmap);
// do something with the result
if (result != null)
{
   txtDecoderType.Text = result.BarcodeFormat.ToString();
   txtDecoderContent.Text = result.Text;
}

此示例读取二维码图像。
我不确定你的输入是什么,但我会假设它也是一个二进制格式的图像,所以你可能需要四处玩才能让它工作

IBarcodeReader reader = new BarcodeReader();//using Zxing
var barcodeBitmap = (Bitmap)Bitmap.FromFile("qrcode.png");

var barcodeReader = new BarcodeReader();

var qrcontent = barcodeReader.Decode(barcodeBitmap).Text;

var qrmessage = qrcontent.Substring(4);//remove first 4 chars

byte[] decodedBase45 = Base45Encoding.Decode(qrmessage);//using base45 lib
var cose = ZlibStream.UncompressBuffer(decodedBase45);//using zlib or similar

var decrypted = Message.DecodeFromBytes(cose).GetContent(); //using COSE
CBORObject cbor = CBORObject.DecodeFromBytes(decrypted);    //using Peter.O.. CBOR

var jsonDecoded = cbor.ToJSONString(); //or deserialize it to custom class

我从NuGet添加了“Zlib.Portable”,并使用了“ZlibStream.UncompressString”

byte[] decoded = Base45Encoding.Decode(input);
var stringResult = ZlibStream.UncompressString(decoded);`

我被困在下一步“CoseMessage.decode”:/

相关问题 更多 >

    热门问题