Commit df6b8e9d LN

hoImage改为out参数方式

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