Commit 4d3a56ac 刘韬

优化扫码

1 个父辈 8d9109e8
......@@ -252,7 +252,7 @@ namespace CodeLibrary
{
if (HDCodeLearnHelper.DefaultBitmap != null) {
List<CodeInfo> codeList = eyemDecode.Decoder(ref HDCodeLearnHelper.DefaultBitmap);
List<CodeInfo> codeList = EyemDecode.Decoder(ref HDCodeLearnHelper.DefaultBitmap);
HDCodeLearnHelper.DefaultBitmap.Dispose();
ShowCode(codeList);
return;
......@@ -303,7 +303,7 @@ namespace CodeLibrary
//for (int i = 0; i < 1000; i++)
//{
Bitmap b = (Bitmap)pictureBox1.Image.Clone();
codeList = eyemDecode.Decoder(ref b);
codeList = EyemDecode.Decoder(ref b);
b.Dispose();
Thread.Sleep(10);
//}
......@@ -656,13 +656,14 @@ namespace CodeLibrary
foreach (string s in files)
{
Bitmap bmp = new Bitmap(s);
HObject hoImage;
HDCodeHelper.Bitmap2HObjectBpp24(bmp, out hoImage);
//HObject hoImage;
//HDCodeHelper.Bitmap2HObjectBpp24(bmp, out hoImage);
bool hasDetect = false;
try
{
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, "Data Matrix ECC 200", GetCodeParamFilePath("Data Matrix ECC 200"), 0, 2500);
......@@ -670,13 +671,22 @@ namespace CodeLibrary
{
Console.WriteLine($"{t},{c.CodeType},{c.CodeStr}");
}
if (tlci.Count() == 0)
Console.WriteLine($"{t},No Detect.");
else
hasDetect = true;
}
catch { }
finally
{
hoImage.Dispose();
//hoImage.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();
}
......
......@@ -11,71 +11,83 @@ using System.Threading.Tasks;
namespace CodeLibrary
{
public unsafe class eyemDecode
public unsafe class EyemDecode
{
public static object DecoderLocker = new object();
public static List<CodeInfo> Decoder(ref Bitmap bmap)
/// <summary>
/// 识别图像二维码
/// </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>();
//lock (DecoderLocker)
{
try
try
{
//创建图像引用
EyemImage eyemImage;
BitmapData bitmapData = null;
if (file == null)
{
Rectangle rect = new Rectangle(0, 0, bmap.Width, bmap.Height);
EyemImage eyemImage = new EyemImage();
BitmapData bitmapData = bmap.LockBits(rect, ImageLockMode.ReadOnly, bmap.PixelFormat);
bitmapData = bmap.LockBits(rect, ImageLockMode.ReadOnly, bmap.PixelFormat);
eyemImage = new EyemImage();
eyemImage.ucpImage = bitmapData.Scan0;
eyemImage.iWidth = rect.Width;
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();
eyemRect.iXs = 0;
eyemRect.iYs = 0;
eyemRect.iWidth = eyemImage.iWidth;
eyemRect.iHeight = eyemImage.iHeight;
//string strDecodeResult = "";
string codeType = "DataMatrix";//QRCode
codeType = "QRCode";
int ipNum;
EyemBarCode* tpResults;
DataCodeHandle hObject = null;
try
{
int result = eyemDetectAndDecode(eyemImage, eyemRect, "", codeType, out hObject, out tpResults, out ipNum, false, 11, 5, 128, 215);
HDLogUtil.info($"eyemDetectAndDecode:{result},ipNum:{ipNum}");
if (result != 0 || ipNum == 0)
return codelist;
int ipNum;
EyemBarCode* tpResults;
DataCodeHandle hObject = null;
try
for (int i = 0; i < ipNum; i++)
{
//int result = eyemDetectAndDecode(eyemImage, eyemRect, "", codeType, ref strDecodeResult, false, 11, 3, 128, 215);
int result = eyemDetectAndDecode(eyemImage, eyemRect, "", codeType, out hObject, out tpResults, out ipNum, false, 11, 5, 128, 215);
HDLogUtil.info($"eyemDetectAndDecode:{result},ipNum:{ipNum}");
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();
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);
}
}
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);
return codelist;
}
//释放解码句柄
public class DataCodeHandle : SafeHandleZeroOrMinusOneIsInvalid
private class DataCodeHandle : SafeHandleZeroOrMinusOneIsInvalid
{
public DataCodeHandle() : base(true) { }
protected override bool ReleaseHandle()
......@@ -83,11 +95,8 @@ namespace CodeLibrary
return eyemDetectAndDecodeFree(handle);
}
}
[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);
[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);
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);
/// <summary>
/// 读取图像
......@@ -97,14 +106,14 @@ namespace CodeLibrary
/// <param name="ucpImage"></param>
/// <returns></returns>
[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>
/// <param name="ipImage"></param>
[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)]
......@@ -134,14 +143,6 @@ namespace CodeLibrary
public IntPtr hType; // 码类型
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
LogUtil.debug(deviceName + " 【" + cameraName + "】取图片完成,开始扫码");
string r = "";
List<CodeInfo> tlci = eyemDecode.Decoder(ref bmp);
List<CodeInfo> tlci = EyemDecode.Decoder(ref bmp);
foreach (CodeInfo code in tlci) {
LogUtil.info(deviceName + " 【" + cameraName + "】[eyemDecode]" + code.CodeType + "(X: " + code.X + ",Y: " + code.Y + ") " + code.CodeStr);
......@@ -218,16 +218,16 @@ namespace OnlineStore.DeviceLibrary
}
finally
{
LogUtil.info("回收");
//LogUtil.info("回收");
if (ho_Image != null)
{
HalconDotNet.HOperatorSet.ClearObj(ho_Image);
LogUtil.info("回收ho_Image");
//LogUtil.info("回收ho_Image");
}
if (bmp != null)
{
bmp.Dispose();
LogUtil.info("回收bmp");
//LogUtil.info("回收bmp");
}
}
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!