在使用 leadtool进行二维码识别的过程中,经常或碰到中文识别为乱码的情况,让人比较郁闷,今天和大家分享的就是如何来读取二维码中的中文
中文在编为二维码的时候,实际上是先按照一定的字符集编码被转换为了二进制编码,而我们如果需要正确的读取出二维码中的中文,就必须要确认编码字符集,如UTF8,UTF16,GB2312,GBK等等。
在确认了编码方式后,我们对通过BarcodeEngine 引擎读取出的BarcodeData ,不用value属性直接去获取它的对应字符串。因为这样默认是得到的按照ascii码编码来翻译出的字符串,自然会有各种乱码,leadtools提供了另一个方法GetData(),通过这个方法可以获取原始的byte[] 数据。然后按照对应的编码方式进行转换,即可得到对应的中文字符串。
下面的例子中是针对UTF8编码的中文二维码进行识别的代码,大家可以在编译以后,对文末的二维码进行识别。
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms;
using Leadtools.Barcode;
using Leadtools.ImageProcessing;
public void BarcodeData_GetDataExample()
{
string imageFileName = Path.Combine(LEAD_VARS.ImagesDir, "Barcode2.tif");
// Create a Barcode engine
BarcodeEngine engine = new BarcodeEngine();
// Load the image
using (RasterCodecs codecs = new RasterCodecs())
{
using (RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
{
// Read the first QR barcode from the image
BarcodeData data = engine.Reader.ReadBarcode(image, LogicalRectangle.Empty, BarcodeSymbology.QR);
// Show the barcode data found (if any)
if (data != null)
{
Console.WriteLine("Raw data is:");
byte[] bytes = data.GetData();
if (bytes != null)
{
string text = System.Text.Encoding.UTF8.GetString(bc);
Console.WriteLine(text);
}
else
{
Console.WriteLine("Empty");
}
}
else
{
Console.WriteLine("No barcode found");
}
}
}
}
支持
关于其他产品的更多信息,请访问我们的官方网站,
欢迎下载免费的全功能评估版SDK,全功能试用版中有这个例子的全部源代码。
同时欢迎您使用评估版期间的免费技术支持。