Commit 437486c7 LN

增加zxing的识别方式

1 个父辈 2b470fb9
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CodeLibrary</RootNamespace> <RootNamespace>CodeLibrary</RootNamespace>
<AssemblyName>CodeLibrary</AssemblyName> <AssemblyName>CodeLibrary</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
...@@ -30,6 +31,7 @@ ...@@ -30,6 +31,7 @@
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Basler.Pylon, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e389355f398382ab, processorArchitecture=x86"> <Reference Include="Basler.Pylon, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e389355f398382ab, processorArchitecture=x86">
...@@ -55,6 +57,9 @@ ...@@ -55,6 +57,9 @@
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="zxing">
<HintPath>..\dll\zxing.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="FrmBase.cs"> <Compile Include="FrmBase.cs">
...@@ -83,6 +88,7 @@ ...@@ -83,6 +88,7 @@
<Compile Include="ImageHelper.cs" /> <Compile Include="ImageHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CodeResourceControl.cs" /> <Compile Include="CodeResourceControl.cs" />
<Compile Include="ZXingCodeHelper.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="FrmBase.resx"> <EmbeddedResource Include="FrmBase.resx">
......
...@@ -18,7 +18,7 @@ using System.IO; ...@@ -18,7 +18,7 @@ using System.IO;
namespace CodeLibrary namespace CodeLibrary
{ {
public partial class FrmCodeDecode : FrmBase public partial class FrmCodeDecode : FrmBase
{ {
private Stopwatch stopwatch = new Stopwatch(); private Stopwatch stopwatch = new Stopwatch();
private List<string> baslerNameList = new List<string>(); private List<string> baslerNameList = new List<string>();
...@@ -53,7 +53,7 @@ namespace CodeLibrary ...@@ -53,7 +53,7 @@ namespace CodeLibrary
{ {
cmbCodeType.Items.Add("QR Code"); cmbCodeType.Items.Add("QR Code");
cmbCodeType.SelectedIndex = 0; cmbCodeType.SelectedIndex = 0;
} }
} }
private void LoadCamera() private void LoadCamera()
{ {
...@@ -98,12 +98,12 @@ namespace CodeLibrary ...@@ -98,12 +98,12 @@ namespace CodeLibrary
Image img = (Image)Image.FromFile(filename).Clone(); Image img = (Image)Image.FromFile(filename).Clone();
pictureBox1.Image = img; pictureBox1.Image = img;
} }
private void btnErZhi_Click(object sender, EventArgs e) private void btnErZhi_Click(object sender, EventArgs e)
{ {
if (pictureBox1.Image == null || txtPath.Text.Equals("")) if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{ {
MessageBox.Show(selImage,title,MessageBoxButtons.OK,MessageBoxIcon.Error); MessageBox.Show(selImage, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
Bitmap map = new Bitmap(pictureBox1.Image); Bitmap map = new Bitmap(pictureBox1.Image);
...@@ -114,15 +114,15 @@ namespace CodeLibrary ...@@ -114,15 +114,15 @@ namespace CodeLibrary
{ {
if (pictureBox1.Image == null || txtPath.Text.Equals("")) if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{ {
MessageBox.Show(selImage ,title, MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(selImage, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
Bitmap map = new Bitmap(pictureBox1.Image); Bitmap map = new Bitmap(pictureBox1.Image);
Bitmap newMap = ImageHelper.ToGray(map); Bitmap newMap = ImageHelper.ToGray(map);
pictureBox1.Image = newMap; pictureBox1.Image = newMap;
} }
private void ShowCode(List<CodeInfo> list) private void ShowCode(List<CodeInfo> list)
{ {
if (list.Count > 0) if (list.Count > 0)
...@@ -130,7 +130,7 @@ namespace CodeLibrary ...@@ -130,7 +130,7 @@ namespace CodeLibrary
txtResult.Text += "\r\n code list:"; txtResult.Text += "\r\n code list:";
foreach (CodeInfo code in 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 else
...@@ -140,13 +140,13 @@ namespace CodeLibrary ...@@ -140,13 +140,13 @@ namespace CodeLibrary
} }
private void btnbarCode_Click(object sender, EventArgs e) 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; return;
} }
int count = cmbCount.SelectedIndex ; int count = cmbCount.SelectedIndex;
txtResult.Text = ""; txtResult.Text = "";
stopwatch.Restart(); stopwatch.Restart();
HDCodeHelper.HalconWindow = this.hWindowControl1.HalconWindow; HDCodeHelper.HalconWindow = this.hWindowControl1.HalconWindow;
...@@ -176,10 +176,10 @@ namespace CodeLibrary ...@@ -176,10 +176,10 @@ namespace CodeLibrary
{ {
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; return;
} }
int count = cmbCount.SelectedIndex ; int count = cmbCount.SelectedIndex;
txtResult.Text = ""; txtResult.Text = "";
stopwatch.Restart(); stopwatch.Restart();
...@@ -205,9 +205,21 @@ namespace CodeLibrary ...@@ -205,9 +205,21 @@ namespace CodeLibrary
{ {
codeList = HDCodeHelper.DecodeCode(ho_image, count, codeParamPath, cmbCodeType.Text); codeList = HDCodeHelper.DecodeCode(ho_image, count, codeParamPath, cmbCodeType.Text);
} }
ShowCode(codeList);
txtResult.Text += "\r\n elapsed time:" + stopwatch.Elapsed.ToString(); 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) 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 @@ ...@@ -2,6 +2,8 @@
增加相机本身获取图片的代码,后续扫码都直接从相机中获取图片,然后扫码 增加相机本身获取图片的代码,后续扫码都直接从相机中获取图片,然后扫码
20190731
RC29西安三台料仓的二维码使用halcon无法识别,增加zxing的识别方式。
...@@ -83,30 +83,67 @@ namespace CodeTest ...@@ -83,30 +83,67 @@ namespace CodeTest
// } // }
// return result; // return result;
//} //}
public static bool DecodeQRCode(Bitmap bmp, out string text) public static string DecodeQRCode(Bitmap bmp)
{ {
string text = "";
DecodingOptions option = new DecodingOptions(); DecodingOptions option = new DecodingOptions();
//option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE, BarcodeFormat.All_1D }; //option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE, BarcodeFormat.All_1D };
option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE }; option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE };
BarcodeReader br = new BarcodeReader(); BarcodeReader br = new BarcodeReader();
br.Options = option; br.Options = option;
Result rs = br.Decode(bmp); Result rs = br.Decode(bmp);
if (rs == null) if (rs == null)
{ {
text = ""; text = "";
return false;
} }
else else
{ {
text = rs.Text; text = ConvertISO88591ToEncoding(rs.ToString(), Encoding.Default);
return true;
} }
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;
} }
//} //}
///// <summary> ///// <summary>
......
...@@ -19,16 +19,16 @@ namespace CodeTest ...@@ -19,16 +19,16 @@ namespace CodeTest
static void Main() static void Main()
{ {
// CodeCreater.CreateCode(10); // CodeCreater.CreateCode(10);
//Application.EnableVisualStyles(); Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
//HDCodeLearnHelper.LoadConfig(ConfigAppSettings.GetValue(Setting_Init.CameraName), ConfigAppSettings.GetValue(Setting_Init.CodeType)); HDCodeLearnHelper.LoadConfig(ConfigAppSettings.GetValue(Setting_Init.CameraName), ConfigAppSettings.GetValue(Setting_Init.CodeType));
//Application.Run(new FrmCodeDecode()); Application.Run(new FrmCodeDecode());
//Application.Run(new FrmTest()); //Application.Run(new FrmTest());
string filename = @"C:/Users/WORK/Desktop/西安二维码/IMG_20190730_165612.jpg"; //string filename = @"C:/Users/WORK/Desktop/西安二维码/IMG_20190730_165612.jpg";
Bitmap img = (Bitmap)Image.FromFile(filename).Clone(); //Bitmap img = (Bitmap)Image.FromFile(filename).Clone();
string text; //string text= CodeCreater.DecodeQRCode(img);
CodeCreater.DecodeQRCode(img, out text); //Console.WriteLine(text);
} }
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!