Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
孙克
/
GeneralClassLibrary
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit 437486c7
由
LN
编写于
2019-07-31 09:37:13 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
增加zxing的识别方式
1 个父辈
2b470fb9
隐藏空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
172 行增加
和
33 行删除
CodeLibraryProject/CodeLibrary/CodeLibrary.csproj
CodeLibraryProject/CodeLibrary/FrmCodeDecode.cs
CodeLibraryProject/CodeLibrary/ZXingCodeHelper.cs
CodeLibraryProject/CodeLibrary/记录.txt
CodeLibraryProject/CodeTest/CodeCreater.cs
CodeLibraryProject/CodeTest/Program.cs
CodeLibraryProject/编译好的DLL/CodeLibrary.dll
CodeLibraryProject/编译好的DLL/zxing.dll
CodeLibraryProject/CodeLibrary/CodeLibrary.csproj
查看文件 @
437486c
...
...
@@ -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">
...
...
CodeLibraryProject/CodeLibrary/FrmCodeDecode.cs
查看文件 @
437486c
...
...
@@ -18,7 +18,7 @@ using System.IO;
namespace
CodeLibrary
{
public
partial
class
FrmCodeDecode
:
FrmBase
{
{
private
Stopwatch
stopwatch
=
new
Stopwatch
();
private
List
<
string
>
baslerNameList
=
new
List
<
string
>();
...
...
@@ -53,7 +53,7 @@ namespace CodeLibrary
{
cmbCodeType
.
Items
.
Add
(
"QR Code"
);
cmbCodeType
.
SelectedIndex
=
0
;
}
}
}
private
void
LoadCamera
()
{
...
...
@@ -98,12 +98,12 @@ namespace CodeLibrary
Image
img
=
(
Image
)
Image
.
FromFile
(
filename
).
Clone
();
pictureBox1
.
Image
=
img
;
}
private
void
btnErZhi_Click
(
object
sender
,
EventArgs
e
)
{
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,15 +114,15 @@ 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
);
Bitmap
newMap
=
ImageHelper
.
ToGray
(
map
);
pictureBox1
.
Image
=
newMap
;
}
private
void
ShowCode
(
List
<
CodeInfo
>
list
)
{
if
(
list
.
Count
>
0
)
...
...
@@ -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
...
...
@@ -140,13 +140,13 @@ 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,9 +205,21 @@ namespace CodeLibrary
{
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
)
...
...
CodeLibraryProject/CodeLibrary/ZXingCodeHelper.cs
0 → 100644
查看文件 @
437486c
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
;
}
}
}
}
}
CodeLibraryProject/CodeLibrary/记录.txt
查看文件 @
437486c
...
...
@@ -2,6 +2,8 @@
增加相机本身获取图片的代码,后续扫码都直接从相机中获取图片,然后扫码
20190731
RC29西安三台料仓的二维码使用halcon无法识别,增加zxing的识别方式。
CodeLibraryProject/CodeTest/CodeCreater.cs
查看文件 @
437486c
...
...
@@ -83,30 +83,67 @@ 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 };
option
.
PossibleFormats
=
new
List
<
BarcodeFormat
>()
{
BarcodeFormat
.
QR_CODE
};
BarcodeReader
br
=
new
BarcodeReader
();
br
.
Options
=
option
;
Result
rs
=
br
.
Decode
(
bmp
);
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
;
}
//}
///// <summary>
...
...
CodeLibraryProject/CodeTest/Program.cs
查看文件 @
437486c
...
...
@@ -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);
}
}
...
...
CodeLibraryProject/编译好的DLL/CodeLibrary.dll
查看文件 @
437486c
此文件类型无法预览
CodeLibraryProject/编译好的DLL/zxing.dll
0 → 100644
查看文件 @
437486c
此文件类型无法预览
编写
预览
支持
Markdown
格式
附加文件
你添加了
0
人
到此讨论。请谨慎行事。
Finish editing this message first!
Cancel
请
注册
或
登录
后发表评论