Commit 4d3a56ac 刘韬

优化扫码

1 个父辈 8d9109e8
...@@ -252,7 +252,7 @@ namespace CodeLibrary ...@@ -252,7 +252,7 @@ namespace CodeLibrary
{ {
if (HDCodeLearnHelper.DefaultBitmap != null) { if (HDCodeLearnHelper.DefaultBitmap != null) {
List<CodeInfo> codeList = eyemDecode.Decoder(ref HDCodeLearnHelper.DefaultBitmap); List<CodeInfo> codeList = EyemDecode.Decoder(ref HDCodeLearnHelper.DefaultBitmap);
HDCodeLearnHelper.DefaultBitmap.Dispose(); HDCodeLearnHelper.DefaultBitmap.Dispose();
ShowCode(codeList); ShowCode(codeList);
return; return;
...@@ -303,7 +303,7 @@ namespace CodeLibrary ...@@ -303,7 +303,7 @@ namespace CodeLibrary
//for (int i = 0; i < 1000; i++) //for (int i = 0; i < 1000; i++)
//{ //{
Bitmap b = (Bitmap)pictureBox1.Image.Clone(); Bitmap b = (Bitmap)pictureBox1.Image.Clone();
codeList = eyemDecode.Decoder(ref b); codeList = EyemDecode.Decoder(ref b);
b.Dispose(); b.Dispose();
Thread.Sleep(10); Thread.Sleep(10);
//} //}
...@@ -656,13 +656,14 @@ namespace CodeLibrary ...@@ -656,13 +656,14 @@ namespace CodeLibrary
foreach (string s in files) foreach (string s in files)
{ {
Bitmap bmp = new Bitmap(s); Bitmap bmp = new Bitmap(s);
HObject hoImage; //HObject hoImage;
HDCodeHelper.Bitmap2HObjectBpp24(bmp, out hoImage); //HDCodeHelper.Bitmap2HObjectBpp24(bmp, out hoImage);
bool hasDetect = false;
try try
{ {
t++; t++;
List<CodeInfo> tlci = eyemDecode.Decoder(ref bmp); List<CodeInfo> tlci = EyemDecode.Decoder(ref bmp, null);
//HDCodeHelper.DecodeCode(hoImage, "QR Code", GetCodeParamFilePath("QR Code"), 0, 2500); //HDCodeHelper.DecodeCode(hoImage, "QR Code", GetCodeParamFilePath("QR Code"), 0, 2500);
//HDCodeHelper.DecodeCode(hoImage, "Data Matrix ECC 200", GetCodeParamFilePath("Data Matrix ECC 200"), 0, 2500); //HDCodeHelper.DecodeCode(hoImage, "Data Matrix ECC 200", GetCodeParamFilePath("Data Matrix ECC 200"), 0, 2500);
...@@ -670,13 +671,22 @@ namespace CodeLibrary ...@@ -670,13 +671,22 @@ namespace CodeLibrary
{ {
Console.WriteLine($"{t},{c.CodeType},{c.CodeStr}"); Console.WriteLine($"{t},{c.CodeType},{c.CodeStr}");
} }
if (tlci.Count() == 0)
Console.WriteLine($"{t},No Detect.");
else
hasDetect = true;
} }
catch { } catch { }
finally finally
{ {
hoImage.Dispose(); //hoImage.Dispose();
bmp.Dispose(); bmp.Dispose();
} }
var ss = Path.ChangeExtension(s, null);
ss = Path.ChangeExtension(ss, null);
ss = Path.ChangeExtension(ss, hasDetect?".bmp":".false.bmp");
File.Move(s, ss);
} }
//GC.Collect(); //GC.Collect();
} }
......
...@@ -11,71 +11,83 @@ using System.Threading.Tasks; ...@@ -11,71 +11,83 @@ using System.Threading.Tasks;
namespace CodeLibrary namespace CodeLibrary
{ {
public unsafe class eyemDecode public unsafe class EyemDecode
{ {
public static object DecoderLocker = new object(); /// <summary>
public static List<CodeInfo> Decoder(ref Bitmap bmap) /// 识别图像二维码
/// </summary>
/// <param name="bmap">需要手动释放这个bitmap</param>
/// <returns></returns>
public static List<CodeInfo> Decoder(ref Bitmap bmap,string file=null)
{ {
List<CodeInfo> codelist = new List<CodeInfo>(); List<CodeInfo> codelist = new List<CodeInfo>();
//lock (DecoderLocker)
{ try
try {
//创建图像引用
EyemImage eyemImage;
BitmapData bitmapData = null;
if (file == null)
{ {
Rectangle rect = new Rectangle(0, 0, bmap.Width, bmap.Height); Rectangle rect = new Rectangle(0, 0, bmap.Width, bmap.Height);
EyemImage eyemImage = new EyemImage(); bitmapData = bmap.LockBits(rect, ImageLockMode.ReadOnly, bmap.PixelFormat);
BitmapData bitmapData = bmap.LockBits(rect, ImageLockMode.ReadOnly, bmap.PixelFormat); eyemImage = new EyemImage();
eyemImage.ucpImage = bitmapData.Scan0; eyemImage.ucpImage = bitmapData.Scan0;
eyemImage.iWidth = rect.Width; eyemImage.iWidth = rect.Width;
eyemImage.iHeight = rect.Height; eyemImage.iHeight = rect.Height;
eyemImage.iChannel = 0;// Bitmap.GetPixelFormatSize(bitmapData.PixelFormat); eyemImage.iChannel = 0;
}
else {
eyemImageRead(file, 3, out eyemImage);
}
//创建扫描区域
EyemRect eyemRect = new EyemRect();
eyemRect.iXs = 0;
eyemRect.iYs = 0;
eyemRect.iWidth = eyemImage.iWidth;
eyemRect.iHeight = eyemImage.iHeight;
string codeType = "DataMatrix";//QRCode
codeType = "QRCode";
EyemRect eyemRect = new EyemRect(); int ipNum;
eyemRect.iXs = 0; EyemBarCode* tpResults;
eyemRect.iYs = 0; DataCodeHandle hObject = null;
eyemRect.iWidth = eyemImage.iWidth; try
eyemRect.iHeight = eyemImage.iHeight; {
//string strDecodeResult = ""; int result = eyemDetectAndDecode(eyemImage, eyemRect, "", codeType, out hObject, out tpResults, out ipNum, false, 11, 5, 128, 215);
string codeType = "DataMatrix";//QRCode HDLogUtil.info($"eyemDetectAndDecode:{result},ipNum:{ipNum}");
codeType = "QRCode"; if (result != 0 || ipNum == 0)
return codelist;
int ipNum; for (int i = 0; i < ipNum; i++)
EyemBarCode* tpResults;
DataCodeHandle hObject = null;
try
{ {
//int result = eyemDetectAndDecode(eyemImage, eyemRect, "", codeType, ref strDecodeResult, false, 11, 3, 128, 215); CodeInfo ci = new CodeInfo(Marshal.PtrToStringAnsi(tpResults[i].hText), tpResults[i].iCenterX, tpResults[i].iCenterY);
int result = eyemDetectAndDecode(eyemImage, eyemRect, "", codeType, out hObject, out tpResults, out ipNum, false, 11, 5, 128, 215); ci.CodeType = Marshal.PtrToStringAnsi(tpResults[i].hType);
HDLogUtil.info($"eyemDetectAndDecode:{result},ipNum:{ipNum}"); codelist.Add(ci);
if (result != 0 || ipNum == 0)
return codelist;
for (int i = 0; i < ipNum; i++)
{
CodeInfo ci = new CodeInfo(Marshal.PtrToStringAnsi(tpResults[i].hText), tpResults[i].iCenterX, tpResults[i].iCenterY);
ci.CodeType = Marshal.PtrToStringAnsi(tpResults[i].hType);
codelist.Add(ci);
//Console.WriteLine("类型:" + Marshal.PtrToStringAnsi(tpResults[i].hType) + ";坐标" + "[" + tpResults[i].iCenterX.ToString() + "," + tpResults[i].iCenterY.ToString() + "]" + ";内容:" + Marshal.PtrToStringAnsi(tpResults[i].hText) + "");
}
}
finally
{
if (hObject != null)
hObject.Dispose();
bmap.UnlockBits(bitmapData);
//bmap.Dispose();
} }
} }
catch (Exception ex) finally
{ {
HDLogUtil.error(ex.ToString()); if (hObject != null)
hObject.Dispose();
if (bitmapData!=null)
bmap.UnlockBits(bitmapData);
if (file != null)
eyemImageFree(eyemImage.ucpImage);
//bmap.Dispose();
} }
} }
catch (Exception ex)
{
HDLogUtil.error(ex.ToString());
}
//eyemImageFree(eyemImage.ucpImage); //eyemImageFree(eyemImage.ucpImage);
return codelist; return codelist;
} }
//释放解码句柄 //释放解码句柄
public class DataCodeHandle : SafeHandleZeroOrMinusOneIsInvalid private class DataCodeHandle : SafeHandleZeroOrMinusOneIsInvalid
{ {
public DataCodeHandle() : base(true) { } public DataCodeHandle() : base(true) { }
protected override bool ReleaseHandle() protected override bool ReleaseHandle()
...@@ -83,11 +95,8 @@ namespace CodeLibrary ...@@ -83,11 +95,8 @@ namespace CodeLibrary
return eyemDetectAndDecodeFree(handle); return eyemDetectAndDecodeFree(handle);
} }
} }
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemDetectAndDecode(EyemImage tpImage, EyemRect tpRoi, string fileName, string strCodeType, ref string strDecodeResult, bool bUseNiBlack, int iBlockSize, int iRangeC, int iSymbolMin, int iSymbolMax, double dToleErr = 0.5, double dMinorStep = 1.0); private static extern int eyemDetectAndDecode(EyemImage tpImage, EyemRect tpRoi, string fileName, string strCodeType, out DataCodeHandle hObject, out EyemBarCode* tpResults, out int ipNum, bool bUseNiBlack, int iBlockSize, int iRangeC, int iSymbolMin, int iSymbolMax, double dScaleUpAndDown=0.5, double dToleErr = 0.5, double dMinorStep = 1.0);
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemDetectAndDecode(EyemImage tpImage, EyemRect tpRoi, string fileName, string strCodeType, out DataCodeHandle hObject, out EyemBarCode* tpResults, out int ipNum, bool bUseNiBlack, int iBlockSize, int iRangeC, int iSymbolMin, int iSymbolMax, double dToleErr = 0.5, double dMinorStep = 1.0);
/// <summary> /// <summary>
/// 读取图像 /// 读取图像
...@@ -97,14 +106,14 @@ namespace CodeLibrary ...@@ -97,14 +106,14 @@ namespace CodeLibrary
/// <param name="ucpImage"></param> /// <param name="ucpImage"></param>
/// <returns></returns> /// <returns></returns>
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
internal static extern int eyemImageRead(string filename, int iFalgs, out EyemImage ucpImage); private static extern int eyemImageRead(string filename, int iFalgs, out EyemImage ucpImage);
/// <summary> /// <summary>
/// 释放图像资源 /// 释放图像资源
/// </summary> /// </summary>
/// <param name="ipImage"></param> /// <param name="ipImage"></param>
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
internal static extern void eyemImageFree(IntPtr ipImage); private static extern void eyemImageFree(IntPtr ipImage);
//释放工具 //释放工具
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
...@@ -134,14 +143,6 @@ namespace CodeLibrary ...@@ -134,14 +143,6 @@ namespace CodeLibrary
public IntPtr hType; // 码类型 public IntPtr hType; // 码类型
public IntPtr hText; // 码内容 public IntPtr hText; // 码内容
} }
[StructLayout(LayoutKind.Sequential)]
public struct EyemBarCode2
{
public int iCenterX; // y坐标
public int iCenterY; // y坐标
public string hType; // 码类型
public string hText; // 码内容
}
} }
} }
...@@ -141,7 +141,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -141,7 +141,7 @@ namespace OnlineStore.DeviceLibrary
LogUtil.debug(deviceName + " 【" + cameraName + "】取图片完成,开始扫码"); LogUtil.debug(deviceName + " 【" + cameraName + "】取图片完成,开始扫码");
string r = ""; string r = "";
List<CodeInfo> tlci = eyemDecode.Decoder(ref bmp); List<CodeInfo> tlci = EyemDecode.Decoder(ref bmp);
foreach (CodeInfo code in tlci) { foreach (CodeInfo code in tlci) {
LogUtil.info(deviceName + " 【" + cameraName + "】[eyemDecode]" + code.CodeType + "(X: " + code.X + ",Y: " + code.Y + ") " + code.CodeStr); LogUtil.info(deviceName + " 【" + cameraName + "】[eyemDecode]" + code.CodeType + "(X: " + code.X + ",Y: " + code.Y + ") " + code.CodeStr);
...@@ -218,16 +218,16 @@ namespace OnlineStore.DeviceLibrary ...@@ -218,16 +218,16 @@ namespace OnlineStore.DeviceLibrary
} }
finally finally
{ {
LogUtil.info("回收"); //LogUtil.info("回收");
if (ho_Image != null) if (ho_Image != null)
{ {
HalconDotNet.HOperatorSet.ClearObj(ho_Image); HalconDotNet.HOperatorSet.ClearObj(ho_Image);
LogUtil.info("回收ho_Image"); //LogUtil.info("回收ho_Image");
} }
if (bmp != null) if (bmp != null)
{ {
bmp.Dispose(); bmp.Dispose();
LogUtil.info("回收bmp"); //LogUtil.info("回收bmp");
} }
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!