Commit df6b8e9d LN

hoImage改为out参数方式

1 个父辈 63305355
......@@ -85,6 +85,10 @@ namespace CodeLibrary
NoIdLog(id, defaultStr);
}
}
if (strCurLanguage == null)
{
strCurLanguage = "";
}
return strCurLanguage;
}
public static string GetString(string id, string defaultStr, params object[] param)
......
......@@ -52,6 +52,7 @@
this.label4 = new System.Windows.Forms.Label();
this.btnScanTest = new System.Windows.Forms.Button();
this.chbZxing = new System.Windows.Forms.CheckBox();
this.label5 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
......@@ -313,6 +314,7 @@
this.btnScanTest.TabIndex = 30;
this.btnScanTest.Text = "扫码测试";
this.btnScanTest.UseVisualStyleBackColor = true;
this.btnScanTest.Visible = false;
this.btnScanTest.Click += new System.EventHandler(this.btnScanTest_Click);
//
// chbZxing
......@@ -326,11 +328,20 @@
this.chbZxing.Text = "ZXing";
this.chbZxing.UseVisualStyleBackColor = true;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(1083, 57);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(0, 12);
this.label5.TabIndex = 32;
//
// FrmCodeDecode
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1375, 729);
this.Controls.Add(this.label5);
this.Controls.Add(this.chbZxing);
this.Controls.Add(this.btnScanTest);
this.Controls.Add(this.txtParamPath);
......@@ -392,6 +403,7 @@
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button btnScanTest;
private System.Windows.Forms.CheckBox chbZxing;
private System.Windows.Forms.Label label5;
}
}
......@@ -14,6 +14,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace CodeLibrary
{
......@@ -64,6 +65,12 @@ namespace CodeLibrary
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
if (HDCodeLearnHelper.DefaultImage != null)
{
HDCodeLearnHelper.DefaultImage.Dispose();
HDCodeLearnHelper.DefaultImage = null;
}
}
catch (Exception ex)
{
......@@ -126,7 +133,8 @@ namespace CodeLibrary
Image img = (Image)Image.FromFile(filename).Clone();
pictureBox1.Image = img;
Bitmap bitmap = new Bitmap(pictureBox1.Image);
HObject hoimg = HDCodeHelper.Bitmap2HObjectBpp24(bitmap);
HObject hoimg;
HDCodeHelper.Bitmap2HObjectBpp24(bitmap,out hoimg);
HDCodeLearnHelper.DefaultImage = hoimg;
ShowImage(hoimg);
}
......@@ -210,7 +218,7 @@ namespace CodeLibrary
int dWidth = (int)width.D;
int dHeight = (int)height.D;
this.hWindowControl1.HalconWindow.SetPart(0, 0, dHeight, dWidth);
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
}
private void btnLearn_Click(object sender, EventArgs e)
......@@ -265,7 +273,7 @@ namespace CodeLibrary
codeList = HDCodeHelper.DecodeCode(HDCodeLearnHelper.DefaultImage, count, codeParamPath, cmbCodeType.Text);
}
ShowCode(codeList);
txtResult.Text += "\r\n elapsed time:" + stopwatch.Elapsed.ToString();
txtResult.Text += "\r\n elapsed time:" + stopwatch.Elapsed.ToString();
}
}
private void zxingDecode(Bitmap map, string type)
......@@ -296,11 +304,12 @@ namespace CodeLibrary
MessageBox.Show(selCamera);
return null;
}
HObject map = Camera._cam.CaptureOnImage(camerName);
if (map == null)
HObject map;
bool result= Camera._cam.CaptureOnImage(camerName,out map);
if (result&&map == null)
{
Camera._cam.Close(camerName);
map = Camera._cam.CaptureOnImage(camerName);
result = Camera._cam.CaptureOnImage(camerName, out map);
}
return map;
}
......@@ -424,7 +433,8 @@ namespace CodeLibrary
stopwatch.Restart();
testCount++;
Bitmap map = new Bitmap(pictureBox1.Image);
HObject ho_image = HDCodeHelper.Bitmap2HObjectBpp24(map);
HObject ho_image;
HDCodeHelper.Bitmap2HObjectBpp24(map,out ho_image);
txtResult.Text += "\r\n elapsed time:" + stopwatch.Elapsed.ToString();
hWindowControl1.HalconWindow.SetPart(0, 0, map.Height, map.Width);
......@@ -460,45 +470,149 @@ namespace CodeLibrary
{
Camera._cam.CloseAll();
}
private void btnScanTest_Click(object sender, EventArgs e)
{
string camerName = cmbCamera.Text;
if (cmbCamera.SelectedIndex < 0)
{
this.Enabled = false;
this.Cursor = Cursors.WaitCursor;
Task.Factory.StartNew(delegate
{
MessageBox.Show(selCamera);
return;
}
HDLogUtil.info("获取图片完成,开始转换图片,并扫码");
HObject ho_Image = Camera._cam.CaptureOnImage(camerName);
HDLogUtil.info(" 相机【" + camerName + "】转换图片完成,开始扫码");
List<CodeInfo> cc = new List<CodeInfo>();
string str = "";
List<string> codeTypeList = new List<string>(HDCodeLearnHelper.codeTypeList);
string r = "";
int count = cmbCount.SelectedIndex;
foreach (string codeType in codeTypeList)
//ScanTest();
ScanDemo(1000);
});
}
private void ScanTest()
{
for (int index = 1; index <= 200; index++)
{
//判断是否是一维码
if (codeType.ToLower().Equals("barcode"))
{
cc = HDCodeHelper.DecodeBarCode(ho_Image);
}
else
try
{
string codeParamPath = HDCodeHelper.GetCodeParamFilePath(codeType);
cc = HDCodeHelper.DecodeCode(ho_Image, count, codeParamPath, codeType);
label5.Text = "扫码测试_" + index;
int name = (index % 3) + 1;
string filename = @"F:\test\" + name + ".hobj";
if (!File.Exists(filename))
{
continue;
}
ClearPicImg();
if (filename.EndsWith(".hobj"))
{
HObject hObject = null;
HOperatorSet.GenEmptyObj(out hObject);
hObject.ReadObject(filename);
if (hObject != null)
{
HDCodeLearnHelper.DefaultImage = hObject;
ShowImage(hObject);
}
else
{
MessageBox.Show("读取文件失败:" + filename);
}
}
else
{
//读取图片内容
Image img = (Image)Image.FromFile(filename).Clone();
pictureBox1.Image = img;
Bitmap bitmap = new Bitmap(pictureBox1.Image);
HObject hoimg;
HDCodeHelper.Bitmap2HObjectBpp24(bitmap, out hoimg);
HDCodeLearnHelper.DefaultImage = hoimg;
ShowImage(hoimg);
}
btnDCode_Click(null, null);
Thread.Sleep(100);
}
foreach (CodeInfo c in cc)
catch (Exception ex)
{
r += (c.CodeStr) + "##";
HDLogUtil.error("ScanTest[" + index + "]出错:" + ex.ToString());
}
}
MessageBox.Show(r);
ho_Image.Dispose();
this.Enabled = true;
this.Cursor = Cursors.Default;
}
private void ScanDemo(int whileCount = 1)
{
string camerName = cmbCamera.Text;
if (cmbCamera.SelectedIndex < 0)
{
//MessageBox.Show(selCamera);
txtResult.Text = selCamera;
return;
}
int count = cmbCount.SelectedIndex;
List<string> codeTypeList = new List<string>(HDCodeLearnHelper.codeTypeList);
Task task = null;
for (int index = 1; index <= whileCount; index++)
{
task = Task.Factory.StartNew(delegate {
label5.Text = "扫码测试_" + index;
HObject ho_Image = null;
try
{
bool result = Camera._cam.CaptureOnImage(camerName, out ho_Image);
if (result && ho_Image != null)
{
//ClearPicImg();
// ShowImage(ho_Image);
HDLogUtil.info(" 相机【" + camerName + "】获取图片完成,开始扫码");
List<CodeInfo> cc = new List<CodeInfo>();
string r = "ScanDemo[" + index + "]\r\n";
foreach (string codeType in codeTypeList)
{
//判断是否是一维码
if (codeType.ToLower().Equals("barcode"))
{
cc = HDCodeHelper.DecodeBarCode(ho_Image);
}
else
{
string codeParamPath = HDCodeHelper.GetCodeParamFilePath(codeType);
cc = HDCodeHelper.DecodeCode(ho_Image, count, codeParamPath, codeType);
}
foreach (CodeInfo c in cc)
{
r += (c.CodeStr) + "##";
}
}
//MessageBox.Show(r);
txtResult.Text = r;
}
}
catch (Exception ex)
{
HDLogUtil.error("ScanDemo[" + index + "]出错:" + ex.ToString());
}
finally
{
if (ho_Image != null)
{
ho_Image.Dispose();
ho_Image = null;
}
}
});
task.Wait();
//GC.Collect();
//Thread.Sleep(100);
task.Dispose();
Thread.Sleep(5);
}
this.Enabled = true;
this.Cursor = Cursors.Default;
}
private void btnCopyN_Click(object sender, EventArgs e)
{
string text = cmbCamera.Text;
......
......@@ -38,7 +38,7 @@ namespace CodeLibrary
MessageBox.Show(selCamera);
return null;
}
ho_image = Camera._cam.CaptureOnImage(camerName);
Camera._cam.CaptureOnImage(camerName,out ho_image);
return ho_image;
}
private void btnOpen_Click(object sender, EventArgs e)
......@@ -86,7 +86,8 @@ namespace CodeLibrary
return;
}
Bitmap bitmap = new Bitmap( pictureBox1.Image);
HObject Image = HDCodeHelper.Bitmap2HObjectBpp24(bitmap);
HObject Image;
HDCodeHelper.Bitmap2HObjectBpp24(bitmap,out Image);
HDCodeLearnHelper.DefaultImage = Image;
ShowImage(Image);
}
......
......@@ -42,19 +42,20 @@ namespace CodeLibrary
}
return DecodeCode(ho_Image, codeCount, codeParamPath, paramType);
}
/// <summary>
/// 根据图片解析二维码
/// </summary>
/// <param name="map">图片对象</param>
/// <param name="codeCount">二维码数量</param>
/// <param name="codeParamPath">二维码参数路径,""表示不使用参数</param>
/// <param name="paramType">二维码类型,不传类型默认Data Matrix ECC 200</param>
/// <returns>解析到的二维码</returns>
public static List<CodeInfo> DecodeCode(Bitmap map, int codeCount, string codeParamPath, params string[] paramType)
{
HObject ho_image = Bitmap2HObjectBpp24(map);
return DecodeCode(ho_image, codeCount, codeParamPath, paramType);
}
///// <summary>
///// 根据图片解析二维码
///// </summary>
///// <param name="map">图片对象</param>
///// <param name="codeCount">二维码数量</param>
///// <param name="codeParamPath">二维码参数路径,""表示不使用参数</param>
///// <param name="paramType">二维码类型,不传类型默认Data Matrix ECC 200</param>
///// <returns>解析到的二维码</returns>
//public static List<CodeInfo> DecodeCode(Bitmap map, int codeCount, string codeParamPath, params string[] paramType)
//{
// HObject ho_image;
// Bitmap2HObjectBpp24(map,out ho_image);
// return DecodeCode(ho_image, codeCount, codeParamPath, paramType);
//}
/// <summary>
/// 根据图片解析二维码
/// </summary>
......@@ -143,13 +144,41 @@ namespace CodeLibrary
}
}
HDLogUtil.debug(" DecodeCode[" + symbolType + "][" + hv_model_path + "][" + codeCount + "] 结束,返回数量:"+codeList.Count);
if (hv_Area != null)
{
hv_Area.UnPinTuple();
}
if (hv_Row1 != null)
{
hv_Row1.UnPinTuple();
}
if (hv_Column != null)
{
hv_Column.UnPinTuple();
}
if (hv_PointOrder != null)
{
hv_PointOrder.UnPinTuple();
}
if (hv_ResultHandles != null)
{
hv_ResultHandles.UnPinTuple();
}
if (hv_DecodedDataStrings != null)
{
hv_DecodedDataStrings.UnPinTuple();
}
if (hv_DataCodeHandle != null)
{
hv_DataCodeHandle.UnPinTuple();
}
if (ho_SymbolXLDs != null)
{
ho_SymbolXLDs.Dispose();
ho_SymbolXLDs = null;
}
return codeList;
}
catch (Exception ex)
......@@ -199,9 +228,9 @@ namespace CodeLibrary
/// <summary>
/// 将BitMap转换为HObject对象
/// </summary>
public static HObject Bitmap2HObjectBpp24(Bitmap bmp)
public static bool Bitmap2HObjectBpp24(Bitmap bmp,out HObject ho_Image)
{
HObject ho_Image = null;
ho_Image = null;
try
{
HOperatorSet.GenEmptyObj(out ho_Image);
......@@ -212,13 +241,14 @@ namespace CodeLibrary
HOperatorSet.GenImageInterleaved(out ho_Image, srcBmpData.Scan0, "bgrx", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
bmp.UnlockBits(srcBmpData);
return true;
}
catch (Exception ex)
{
HDLogUtil.error("将BitMap转换为HObject对象出错:" + ex.ToString());
ho_Image = null;
}
return ho_Image;
return false ;
}
internal static string GetCodeParamFilePath(string codeType)
......@@ -244,11 +274,12 @@ namespace CodeLibrary
HOperatorSet.ReadImage(out ho_Image, filePath);
return DecodeBarCode(ho_Image);
}
public static List<CodeInfo> DecodeBarCode(Bitmap bitmap)
{
HObject ho_image = Bitmap2HObjectBpp24(bitmap);
return DecodeBarCode(ho_image);
}
//public static List<CodeInfo> DecodeBarCode(Bitmap bitmap)
//{
// HObject ho_image;
// Bitmap2HObjectBpp24(bitmap,out ho_image);
// return DecodeBarCode(ho_image);
//}
public static List<CodeInfo> DecodeBarCode(HObject ho_Image)
{
......@@ -308,7 +339,33 @@ namespace CodeLibrary
}
}
HOperatorSet.ClearBarCodeModel(hv_BarCodeHandle);
if (hv_Area != null)
{
hv_Area.UnPinTuple();
}
if (hv_Row1 != null)
{
hv_Row1.UnPinTuple();
}
if (hv_Column != null)
{
hv_Column.UnPinTuple();
}
if (hv_Orientation != null)
{
hv_Orientation.UnPinTuple();
}
if (ho_GrayImage != null)
{
ho_GrayImage.Dispose();
ho_GrayImage = null;
}
if (ho_SymbolRegions != null)
{
ho_SymbolRegions.Dispose();
ho_SymbolRegions = null;
}
return codeList;
}
catch (Exception ex)
......
......@@ -251,6 +251,7 @@ namespace CodeLibrary
{
CodeResultAdd(hv_DecodedDataStrings, codeType, "识别");
}
ho_Image.Dispose();
}
if (findCode.Count >= codeCount || stopWatch.ElapsedMilliseconds > timeOutMs)
{
......@@ -263,8 +264,41 @@ namespace CodeLibrary
HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
CloseCamera(cameraName);
ho_SymbolXLDs.Dispose();
ho_SymbolXLDs.Dispose();
StopLearn();
if (hv_code_type != null)
{
hv_code_type.UnPinTuple();
}
if (hv_model_path != null)
{
hv_model_path.UnPinTuple();
}
if (hv_train_first != null)
{
hv_train_first.UnPinTuple();
}
if (hv_ResultHandles != null)
{
hv_ResultHandles.UnPinTuple();
}
if (hv_DataCodeHandle != null)
{
hv_DataCodeHandle.UnPinTuple();
}
if (hv_DecodedDataStrings != null)
{
hv_DecodedDataStrings.UnPinTuple();
}
if (hv_GenParamNames != null)
{
hv_GenParamNames.UnPinTuple();
}
if (hv_ModelBeforeTraining != null)
{
hv_ModelBeforeTraining.UnPinTuple();
}
return true;
}
catch (Exception ex)
......
......@@ -228,13 +228,14 @@ namespace CodeLibrary
}
return null;
}
public override HObject CaptureOnImage(string name)
public override bool CaptureOnImage(string name,out HObject hoImage)
{
hoImage = null;
int index = cameraName.FindIndex(s => s == name);
if (index == -1)
{
_errInfo = name + "Not find";
return null;
return false ;
}
if (cameraCurr[index] != null)
{
......@@ -257,7 +258,7 @@ namespace CodeLibrary
if (!grabResult.IsValid || !grabResult.GrabSucceeded)
{
_errInfo = grabResult.ErrorDescription;
return null;
return false ;
}
//相机像素数据
......@@ -266,26 +267,26 @@ namespace CodeLibrary
GCHandle hand = GCHandle.Alloc(buffer, GCHandleType.Pinned);
//获取像素数据的指针
IntPtr pr = hand.AddrOfPinnedObject();
HalconDotNet.HObject image;
//HalconDotNet.HObject image;
//转成灰度图HOjbect
HalconDotNet.HOperatorSet.GenImage1(out image, new HalconDotNet.HTuple("byte"), grabResult.Width, grabResult.Height, pr);
HalconDotNet.HOperatorSet.GenImage1(out hoImage, new HalconDotNet.HTuple("byte"), grabResult.Width, grabResult.Height, pr);
//释放内存
if (hand.IsAllocated) hand.Free();
_errInfo = "OK";
return image;
return true ;
}
}
catch (Exception ex)
{
_errInfo = ex.Message;
return null;
return false ;
}
finally
{
// cameraCurr[index].MV_CC_StopGrabbing_NET();
}
return null;
return false ;
}
}
......
......@@ -112,7 +112,7 @@ namespace CodeLibrary
// public abstract bool GrabContinuous(int index);
// public abstract void GrabStop(int index);
public abstract Bitmap GrabOneImage(string name);
public abstract HObject CaptureOnImage(string name);
public abstract bool CaptureOnImage(string name,out HObject hoImage);
}
}
......@@ -110,6 +110,7 @@ namespace CodeLibrary
if (rtn != MyCamera.MV_OK)
{
_errInfo = "Load failed";
HDLogUtil.error(" Load camera error : " + _errInfo);
return false;
}
......@@ -123,12 +124,14 @@ namespace CodeLibrary
IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
s = "GigE:" + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")";
//Marshal.FreeHGlobal(buffer);
}
else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
{
IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stUsb3VInfo, 0);
MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_USB3_DEVICE_INFO));
s = "USB:" + usbInfo.chModelName + " (" + usbInfo.chSerialNumber + ")";
//Marshal.FreeHGlobal(buffer);
}
cameraName.Add(s);
}
......@@ -155,6 +158,7 @@ namespace CodeLibrary
if (index < 0 || index >= _count)
{
_errInfo = "Not find";
HDLogUtil.error("open camera " + index + " error : " + _errInfo);
return false;
}
if (cameraCurr[index] != null) Close(index);
......@@ -171,6 +175,7 @@ namespace CodeLibrary
{
cameraCurr[index].MV_CC_DestroyDevice_NET();
_errInfo = "Can not open";
HDLogUtil.error("open camera " + index + " error : " + _errInfo);
return false;
}
......@@ -189,6 +194,7 @@ namespace CodeLibrary
_isOpen[index] = true;
_errInfo = "OK";
HDLogUtil.info("open camera " + index + _errInfo);
return true;
}
catch (Exception ex)
......@@ -216,6 +222,7 @@ namespace CodeLibrary
if (n == -1)
{
_errInfo = name + " Not find";
HDLogUtil.error("GrabOneImage ["+name+"] error : " + _errInfo);
return null;
}
if (cameraCurr[n] != null)
......@@ -335,19 +342,22 @@ namespace CodeLibrary
return _image;
}
}
public override HObject CaptureOnImage(string name)
public override bool CaptureOnImage(string name,out HObject hoImage)
{
hoImage = null;
int index = cameraName.FindIndex(s => s == name);
if (index == -1)
{
_errInfo = name + "Not find";
return null;
HDLogUtil.error("CaptureOnImage [" + name + "] error : " + _errInfo);
return false ;
}
if (cameraCurr[index] != null)
{
}
else
{
HDLogUtil.info("CaptureOnImage [" + name + "] 先打开相机 ");
Open(name);
}
......@@ -358,7 +368,8 @@ namespace CodeLibrary
if (rtn != MyCamera.MV_OK)
{
_errInfo = "Can not grab one : " + rtn;
return null;
HDLogUtil.error("CaptureOnImage [" + name + "] error : " + _errInfo);
return false ;
}
int nRet = cameraCurr[index].MV_CC_GetImageBuffer_NET(ref FrameInfo, 1000);
......@@ -368,8 +379,8 @@ namespace CodeLibrary
HDLogUtil.debug("Get Image Buffer:" + "Width[" + Convert.ToString(FrameInfo.stFrameInfo.nWidth) + "] , Height[" + Convert.ToString(FrameInfo.stFrameInfo.nHeight)
+ "] , FrameNum[" + Convert.ToString(FrameInfo.stFrameInfo.nFrameNum) + "]");
HObject ho_Imagetemp;
HOperatorSet.GenImage1(out ho_Imagetemp, "byte", FrameInfo.stFrameInfo.nWidth, FrameInfo.stFrameInfo.nHeight, FrameInfo.pBufAddr);
//HObject ho_Imagetemp;
HOperatorSet.GenImage1(out hoImage, "byte", FrameInfo.stFrameInfo.nWidth, FrameInfo.stFrameInfo.nHeight, FrameInfo.pBufAddr);
if (FrameInfo.pBufAddr != IntPtr.Zero)
{
......@@ -379,7 +390,8 @@ namespace CodeLibrary
HDLogUtil.error(" [" + name + "] Free Image Buffer fail:" + nRet);
}
}
return ho_Imagetemp;
//return ho_Imagetemp;
return true;
}
else
{
......@@ -389,7 +401,7 @@ namespace CodeLibrary
catch (Exception ex)
{
_errInfo = ex.Message;
return null;
return false ;
}
finally
{
......@@ -403,7 +415,7 @@ namespace CodeLibrary
HDLogUtil.error(" [" + name + "] MV_CC_FreeImageBuffer_NET 出错:" + ex.ToString());
}
}
return null;
return false ;
}
}
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!