从多个文本框创建二维码并将其解码回文本框

Create a QR code from multiple textboxes and decode it back into the textboxes

我已经创建了一个应用程序。此应用程序包含五个文本框 id、name、surname、age 和 score。

当用户单击 "okay button" 时,这些值存储在 sql 数据库中。

此外,我想将所有这些信息存储在二维码中。当我解码它时,信息应该分别显示在文本框中。

这些是我目前使用的参考资料。

using AForge.Video.DirectShow;
using Zen.Barcode;
using ZXing.QrCode;
using ZXing;

我可以把身份证号码编码成图片框,像这样:

    CodeQrBarcodeDraw qrcode = BarcodeDrawFactory.CodeQr;
    pictureBox1.Image = qrcode.Draw(textBox1.Text, 50);

但我希望文本框中的所有值都存储在此二维码中。

我该怎么做?

解决方案的实质是,您必须将文本框中的所有值组合成一个字符串。要在解码 QR 码后将它们分开,您必须在数据值之间添加一个特殊字符,该字符在用户输入中不存在。解码 QR 码后,您可以通过在每次出现特殊字符时拆分字符串来分隔值。


这是一种快速而肮脏的方法。如果您希望二维码符合任何特定格式(如 vcard),您必须研究如何为这种格式编写数据。

我希望您的用户不能在文本框中输入超过一行,因此换行符可以用作分隔符。

将所有信息编码成一个二维码。

var qrText = textBox1.Text + "\n" + 
    textBox2.Text + "\n" + 
    textBox3.Text + "\n" + 
    textBox4.Text + "\n" + 
    textBox5.Text;

pictureBox1.Image = qrcode.Draw(qrText, 50);

您可以解码二维码并再次将数据分配到不同的文本框。

var bitmap = new Bitmap(pictureBox1.Image);
var lumianceSsource = new BitmapLuminanceSource(bitmap);
var binBitmap = new BinaryBitmap(new HybridBinarizer(source));

var reader = new MultiFormatReader();
Result result = null;

try
{
    result = reader.Decode(binBitmap);
}
catch (Exception err)
{
    // Handle the exceptions, in a way that fits to your application.
}

var resultDataArray = result.Text.Split(new char[] {'\n'});

// Only if there were 5 linebreaks to split the result string, it was a valid QR code.
if (resultDataArray.length == 5)
{
    textBox1.Text = resultDataArray[0];
    textBox2.Text = resultDataArray[1];
    textBox3.Text = resultDataArray[2];
    textBox4.Text = resultDataArray[3];
    textBox5.Text = resultDataArray[4];
}

您可以通过执行以下代码来完成此操作:

"{" + '"' + "name" + '"' + ":" + '"' + txtName.Text + '"' + "," + '"' + "lname" + '"' + ":" + '"' + txtLname.Text + '"' + "," + '"' + "Roll" + '"' + ":" + '"' + txtRoll.Text + '"' + '"' + "class" + '"' + ":" + '"' + txtClass.Text + '"' + "}"

结果将是:

{"name":"Diljit","lname":"Dosanjh","Roll","2071","class":"BCA"}

这样您的二维码扫描仪就会识别属于其特定字段的数据。