我已经创建了一个应用程序。该应用程序包含五个文本框 ID、姓名、姓氏、年龄和分数。
当用户单击“确定按钮”时,这些值将存储在 SQL 数据库中。
此外,我想将所有这些信息存储在二维码中。当我解码它时,信息应该分别显示在文本框中。
这些是我迄今为止使用的引用资料。
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 码后将它们分开,您必须在数据值之间添加一个特殊字符,该字符在用户输入中不存在。解码 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);
您可以解码 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];
}
关于c# - 从多个文本框创建 QR 码并将其解码回文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48141111/