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

时间:2018-01-07 20:12:20

标签: c# winforms qr-code barcode zxing

我创建了一个应用程序。此应用包含五个文本框ID,姓名,姓氏,年龄和分数。

当用户点击" okay按钮"时,这些值将存储在sql数据库中。

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

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

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

我可以将ID号编码到一个图片框中,如下所示:

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

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

我该怎么做?

2 个答案:

答案 0 :(得分:2)

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

这是快速而肮脏的方式。如果您希望QR码符合任何特定格式(如vcard),则必须重新制作为此格式撰写数据所需的内容。

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

将所有信息编码为一个QR码。

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

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

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

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];
}

答案 1 :(得分:0)

您可以通过以下代码实现此目的:

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

结果将是:

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

这样您的QR扫描仪将识别属于其特定字段的数据。