Commit 437486c7 LN

增加zxing的识别方式

1 个父辈 2b470fb9
......@@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CodeLibrary</RootNamespace>
<AssemblyName>CodeLibrary</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
......@@ -22,6 +22,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
......@@ -30,6 +31,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Basler.Pylon, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e389355f398382ab, processorArchitecture=x86">
......@@ -55,6 +57,9 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="zxing">
<HintPath>..\dll\zxing.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="FrmBase.cs">
......@@ -83,6 +88,7 @@
<Compile Include="ImageHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CodeResourceControl.cs" />
<Compile Include="ZXingCodeHelper.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="FrmBase.resx">
......
......@@ -103,7 +103,7 @@ namespace CodeLibrary
{
if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{
MessageBox.Show(selImage,title,MessageBoxButtons.OK,MessageBoxIcon.Error);
MessageBox.Show(selImage, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Bitmap map = new Bitmap(pictureBox1.Image);
......@@ -114,7 +114,7 @@ namespace CodeLibrary
{
if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{
MessageBox.Show(selImage ,title, MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(selImage, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Bitmap map = new Bitmap(pictureBox1.Image);
......@@ -130,7 +130,7 @@ namespace CodeLibrary
txtResult.Text += "\r\n code list:";
foreach (CodeInfo code in list)
{
txtResult.Text += "\r\n" + ""+code.CodeType+" (X:" + code.X + ",Y:" + code.Y + ") " + code.CodeStr;
txtResult.Text += "\r\n" + "" + code.CodeType + " (X:" + code.X + ",Y:" + code.Y + ") " + code.CodeStr;
}
}
else
......@@ -141,12 +141,12 @@ namespace CodeLibrary
private void btnbarCode_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null )
if (pictureBox1.Image == null)
{
MessageBox.Show(selImage ,title, MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(selImage, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int count = cmbCount.SelectedIndex ;
int count = cmbCount.SelectedIndex;
txtResult.Text = "";
stopwatch.Restart();
HDCodeHelper.HalconWindow = this.hWindowControl1.HalconWindow;
......@@ -176,10 +176,10 @@ namespace CodeLibrary
{
if (pictureBox1.Image == null)
{
MessageBox.Show(selImage ,title, MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(selImage, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int count = cmbCount.SelectedIndex ;
int count = cmbCount.SelectedIndex;
txtResult.Text = "";
stopwatch.Restart();
......@@ -205,10 +205,22 @@ namespace CodeLibrary
{
codeList = HDCodeHelper.DecodeCode(ho_image, count, codeParamPath, cmbCodeType.Text);
}
ShowCode(codeList);
if (codeList.Count <= 0)
{
string result = ZXingCodeHelper.DecodeQRCode(map);
if (!String.IsNullOrEmpty(result))
{
txtResult.Text = " zxing decode:\r\n" + result;
txtResult.Text += "\r\n elapsed time:" + stopwatch.Elapsed.ToString();
}
}
else
{
ShowCode(codeList);
txtResult.Text += "\r\n elapsed time:" + stopwatch.Elapsed.ToString();
}
}
private void btnClearLog_Click(object sender, EventArgs e)
{
......
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing;
using ZXing.Common;
namespace CodeLibrary
{
public class ZXingCodeHelper
{
public static string DecodeQRCode(Bitmap bmp)
{
string text = "";
DecodingOptions option = new DecodingOptions();
//option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE, BarcodeFormat.All_1D };
option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE };
BarcodeReader br = new BarcodeReader();
br.Options = option;
Result rs = br.Decode(bmp);
if (rs == null)
{
text = "";
}
else
{
text = rs.ToString();
if (!IsGBCode(text))
{
text = ConvertISO88591ToEncoding(text, Encoding.Default);
}
}
return text;
}
//转换
private static string ConvertISO88591ToEncoding(string srcString, Encoding dstEncode)
{
String sResult;
Encoding ISO88591Encoding = Encoding.GetEncoding("ISO-8859-1");
Encoding GB2312Encoding = Encoding.GetEncoding("GB2312"); //这个地方很特殊,必须利用GB2312编码
byte[] srcBytes = ISO88591Encoding.GetBytes(srcString);
//将原本存储ISO-8859-1的字节数组当成GB2312转换成目标编码(关键步骤)
byte[] dstBytes = Encoding.Convert(GB2312Encoding, dstEncode, srcBytes);
char[] dstChars = new char[dstEncode.GetCharCount(dstBytes, 0, dstBytes.Length)];
dstEncode.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0);//利用char数组存储字符
sResult = new string(dstChars);
return sResult;
}
/// <summary>
/// 判断一个word是否为GB2312编码的汉字
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
private static bool IsGBCode(string word)
{
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word);
if (bytes.Length <= 1) // if there is only one byte, it is ASCII code or other code
{
return false;
}
else
{
byte byte1 = bytes[0];
byte byte2 = bytes[1];
if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254) //判断是否是GB2312
{
return true;
}
else
{
return false;
}
}
}
}
}
......@@ -2,6 +2,8 @@
增加相机本身获取图片的代码,后续扫码都直接从相机中获取图片,然后扫码
20190731
RC29西安三台料仓的二维码使用halcon无法识别,增加zxing的识别方式。
......@@ -83,8 +83,9 @@ namespace CodeTest
// }
// return result;
//}
public static bool DecodeQRCode(Bitmap bmp, out string text)
public static string DecodeQRCode(Bitmap bmp)
{
string text = "";
DecodingOptions option = new DecodingOptions();
//option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE, BarcodeFormat.All_1D };
......@@ -97,13 +98,49 @@ namespace CodeTest
if (rs == null)
{
text = "";
return false;
}
else
{
text = rs.Text;
return true;
text = ConvertISO88591ToEncoding(rs.ToString(), Encoding.Default);
}
return text;
}
//privatre void TestDecodeISO88591(string RssUrl)
//{
// string sResult = "";
// System.IO.Stream ResponseStream = null;
// HttpWebResponse hwrp = null;
// System.IO.StreamReader oStreamReader = null;
// Encoding UrlEncoding;
// System.Net.HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(RssUrl);
// hwr.Method = "GET";
// hwrp = (HttpWebResponse)hwr.GetResponse();
// UrlEncoding = Encoding.GetEncoding(hwrp.CharacterSet);
// ResponseStream = hwrp.GetResponseStream();
// oStreamReader = new System.IO.StreamReader(ResponseStream, UrlEncoding);
// sResult = oStreamReader.ReadToEnd();
// if (hwrp.CharacterSet == "ISO-8859-1") //如果编码为ISO-8859-1才转换
// {
// sResult = ConvertISO88591ToEncoding(sResult, Encoding.Default);
// }
// hwrp.Close();
// //处理RSS返回的数据
// //.......
//}
//转换
private static string ConvertISO88591ToEncoding(string srcString, Encoding dstEncode)
{
String sResult;
Encoding ISO88591Encoding = Encoding.GetEncoding("ISO-8859-1");
Encoding GB2312Encoding = Encoding.GetEncoding("GB2312"); //这个地方很特殊,必须利用GB2312编码
byte[] srcBytes = ISO88591Encoding.GetBytes(srcString);
//将原本存储ISO-8859-1的字节数组当成GB2312转换成目标编码(关键步骤)
byte[] dstBytes = Encoding.Convert(GB2312Encoding, dstEncode, srcBytes);
char[] dstChars = new char[dstEncode.GetCharCount(dstBytes, 0, dstBytes.Length)];
dstEncode.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0);//利用char数组存储字符
sResult = new string(dstChars);
return sResult;
}
......
......@@ -19,16 +19,16 @@ namespace CodeTest
static void Main()
{
// CodeCreater.CreateCode(10);
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//HDCodeLearnHelper.LoadConfig(ConfigAppSettings.GetValue(Setting_Init.CameraName), ConfigAppSettings.GetValue(Setting_Init.CodeType));
//Application.Run(new FrmCodeDecode());
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
HDCodeLearnHelper.LoadConfig(ConfigAppSettings.GetValue(Setting_Init.CameraName), ConfigAppSettings.GetValue(Setting_Init.CodeType));
Application.Run(new FrmCodeDecode());
//Application.Run(new FrmTest());
string filename = @"C:/Users/WORK/Desktop/西安二维码/IMG_20190730_165612.jpg";
Bitmap img = (Bitmap)Image.FromFile(filename).Clone();
string text;
CodeCreater.DecodeQRCode(img, out text);
//string filename = @"C:/Users/WORK/Desktop/西安二维码/IMG_20190730_165612.jpg";
//Bitmap img = (Bitmap)Image.FromFile(filename).Clone();
//string text= CodeCreater.DecodeQRCode(img);
//Console.WriteLine(text);
}
}
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!