/// <summary> /// 初始化 /// </summary> /// <param name="width">图像宽</param> /// <param name="height">图像高</param> /// <returns></returns> public bool InitAlgorithm(Int32 width, Int32 height) { this.Width = 4000; this.Height = 3000; //初始化清晰度的算法 MvSSharpnessMeasureChannelParam smChannelParam = MvSSharpnessMeasureChannelParam.GetInstance(); smChannelParam.imgWidth = Width; smChannelParam.imgHeight = Height; int memSize = MVSHARP.sharpnessMeasureCalMemSize(ref smChannelParam); MvBarCodeGlobalVar.Log.InfoFormat("sharpnessMeasureCalMemSize :{0}", memSize.ToString()); m_handle = Marshal.AllocHGlobal(memSize); int result = MVSHARP.sharpnessMeasureInit(m_handle, ref smChannelParam); MvBarCodeGlobalVar.Log.InfoFormat("sharpnessMeasureInit return :{0}", result.ToString()); if (result > memSize) { MVSHARP.sharpnessMeasureExit(m_handle); //记下错误日志 //log.ErrorFormat("算法需要的内存{0}比最大支持的内存{1}大,此图片不进行清晰度", result, memSize); } return(true); }
/// <summary> /// 第一帧条码图像需要setconfig /// </summary> public void SetConfig() { MvSSharpnessMeasureConfigParam smConfigParam = MvSSharpnessMeasureConfigParam.GetInstance(); MVSHARP.sharpnessMeasureGetConfig(m_handle, ref smConfigParam); smConfigParam.roi.left = 0; smConfigParam.roi.top = 0; smConfigParam.roi.right = Width - 1; smConfigParam.roi.bottom = Height - 1; smConfigParam.thScore = 30; MVSHARP.sharpnessMeasureSetConfig(m_handle, ref smConfigParam); }
/// <summary> /// 主处理函数 /// </summary> /// <param name="bmp">传入图片</param> /// <param name="mcList">条码信息</param> /// <returns>传出图片</returns> public int Process(Bitmap bmp, string code, List <MvBarCode.MvCodeInfo> mcList, int flag) { MvSSharpnessMeasureProcessParam processParam = MvSSharpnessMeasureProcessParam.GetInstance(); MvSSharpnessMeasureProcessResult processResult = MvSSharpnessMeasureProcessResult.GetInstance(); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); var ptrImage = bmpData.Scan0; MvsCommon.MvSImage img = MvsCommon.MvSImage.GetInstance(); img.Height = bmpData.Height; img.Width = bmpData.Width; img.ImageData = ptrImage; img.DataType = Convert.ToInt32(MvsImgDataType.MVS_IMGDTP_U8); img.Type = Convert.ToInt32(MvsImgType.MVS_IMGTP_UITL_Y); processParam.pSrcImg = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MvsCommon.MvSImage))); Marshal.StructureToPtr(img, processParam.pSrcImg, false); processParam.nRegion = 1; processParam.bCmpFlag = flag; var ptrPoint = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MvsCommon.MvSPoint)) * 12); IntPtr ptrContent = IntPtr.Zero; ptrContent = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MvSCodeContent)) * 10); for (int j = 0; j < mcList.Count; j++) { for (int i = 0; i < mcList[j].Region.PtArray.Length; i++) { MvsCommon.MvSPoint pt; pt.x = mcList[j].Region.PtArray[i].X; pt.y = mcList[j].Region.PtArray[i].Y; Marshal.StructureToPtr(pt, ptrPoint + Marshal.SizeOf(typeof(MvsCommon.MvSPoint)) * i, false); } MvSCodeContent codeContent = MvSCodeContent.GetInstance(); Array.Copy(Encoding.Default.GetBytes(mcList[j].Code), codeContent.content, mcList[j].CodeLen); //codeContent.content = Encoding.Default.GetBytes(mcList[j].Code); codeContent.len = mcList[j].CodeLen; Marshal.StructureToPtr(codeContent, ptrContent + Marshal.SizeOf(typeof(MvSCodeContent)) * j, false); //Marshal.StructureToPtr(codeContent, ptrContent, false); } processParam.Pts = ptrPoint; processParam.CodeContent = ptrContent; try { int result = MVSHARP.sharpnessMeasureProcess(m_handle, ref processParam, ref processResult); } catch (Exception ex) { MvBarCodeGlobalVar.Log.Error(ex); } bmp.UnlockBits(bmpData); return(processResult.score); }
public void Dispose() { MVSHARP.sharpnessMeasureExit(m_handle); Marshal.FreeHGlobal(m_handle); }