Commit 7ab790ee LN

hik获取图片修改

1 个父辈 7ce5a30d
...@@ -57,7 +57,6 @@ ...@@ -57,7 +57,6 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="camera\Basler.cs" />
<Compile Include="camera\Common.cs" /> <Compile Include="camera\Common.cs" />
<Compile Include="camera\HIK.cs" /> <Compile Include="camera\HIK.cs" />
<Compile Include="FrmBase.cs"> <Compile Include="FrmBase.cs">
......
...@@ -101,7 +101,7 @@ namespace CodeLibrary ...@@ -101,7 +101,7 @@ namespace CodeLibrary
{ {
HOperatorSet.ReadDataCode2dModel(hv_model_path, out hv_DataCodeHandle); HOperatorSet.ReadDataCode2dModel(hv_model_path, out hv_DataCodeHandle);
} }
// HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "timeout", 3000);
ho_SymbolXLDs.Dispose(); ho_SymbolXLDs.Dispose();
if (codeCount <= 0) if (codeCount <= 0)
{ {
...@@ -198,7 +198,7 @@ namespace CodeLibrary ...@@ -198,7 +198,7 @@ namespace CodeLibrary
ho_Image.Dispose(); ho_Image.Dispose();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat); BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
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);
......
...@@ -121,7 +121,7 @@ namespace CodeLibrary ...@@ -121,7 +121,7 @@ namespace CodeLibrary
// } // }
//} //}
public override bool GrabOne(int index) public override Bitmap GrabOne(int index)
{ {
if (cameraCurr[index] != null) if (cameraCurr[index] != null)
{ {
...@@ -136,9 +136,9 @@ namespace CodeLibrary ...@@ -136,9 +136,9 @@ namespace CodeLibrary
if (!grabResult.IsValid) if (!grabResult.IsValid)
{ {
_errInfo = grabResult.ErrorDescription; _errInfo = grabResult.ErrorDescription;
return false; return null;
} }
_image = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format24bppRgb); Bitmap _image = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format24bppRgb);
BitmapData bmpData = _image.LockBits(new Rectangle(0, 0, grabResult.Width, grabResult.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); BitmapData bmpData = _image.LockBits(new Rectangle(0, 0, grabResult.Width, grabResult.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr ptrBmp = bmpData.Scan0; IntPtr ptrBmp = bmpData.Scan0;
int picSize = bmpData.Stride * grabResult.Height; int picSize = bmpData.Stride * grabResult.Height;
...@@ -151,21 +151,21 @@ namespace CodeLibrary ...@@ -151,21 +151,21 @@ namespace CodeLibrary
_image.UnlockBits(bmpData); _image.UnlockBits(bmpData);
_errInfo = "OK"; _errInfo = "OK";
return true; return _image;
} }
catch (Exception ex) catch (Exception ex)
{ {
_errInfo = ex.Message; _errInfo = ex.Message;
return false; return null;
} }
}return false; }return null;
} }
public override bool GrabOne(string name) public override Bitmap GrabOne(string name)
{ {
int idx = Array.FindIndex(_name, s => s == name); int idx = Array.FindIndex(_name, s => s == name);
if (idx == -1) if (idx == -1)
return false; return null;
else else
return GrabOne(idx); return GrabOne(idx);
} }
...@@ -271,7 +271,7 @@ namespace CodeLibrary ...@@ -271,7 +271,7 @@ namespace CodeLibrary
IGrabResult grabResult = e.GrabResult; IGrabResult grabResult = e.GrabResult;
if (!grabResult.IsValid) return; if (!grabResult.IsValid) return;
_image= new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format24bppRgb); Bitmap _image= new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format24bppRgb);
BitmapData bmpData = _image.LockBits(new Rectangle(0, 0, grabResult.Width, grabResult.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); BitmapData bmpData = _image.LockBits(new Rectangle(0, 0, grabResult.Width, grabResult.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr ptrBmp = bmpData.Scan0; IntPtr ptrBmp = bmpData.Scan0;
int picSize = bmpData.Stride * grabResult.Height; int picSize = bmpData.Stride * grabResult.Height;
...@@ -305,8 +305,8 @@ namespace CodeLibrary ...@@ -305,8 +305,8 @@ namespace CodeLibrary
} }
if (cameraCurr[n] != null) if (cameraCurr[n] != null)
{ {
GrabOne(name); return GrabOne(name);
return Image;
} }
return null; return null;
} }
......
...@@ -13,12 +13,12 @@ namespace CodeLibrary ...@@ -13,12 +13,12 @@ namespace CodeLibrary
public class Camera : ClsCamera public class Camera : ClsCamera
{ {
public static ClsCamera _cam; public static ClsCamera _cam;
private static CameraType _type; private static CameraType _type;
/// <summary> /// <summary>
/// 连续采集事件 /// 连续采集事件
/// </summary> /// </summary>
public static event Continuous Continuous_Event; public static event Continuous Continuous_Event;
/// <summary> /// <summary>
...@@ -30,14 +30,14 @@ namespace CodeLibrary ...@@ -30,14 +30,14 @@ namespace CodeLibrary
/// <summary> /// <summary>
/// 相机类型 /// 相机类型
/// </summary> /// </summary>
public static CameraType Type public static CameraType Type
{ {
set set
{ {
if (_cam != null) if (_cam != null)
{ {
return ; return;
// _cam.CloseAll(); // _cam.CloseAll();
} }
_type = value; _type = value;
switch (_type) switch (_type)
...@@ -46,10 +46,10 @@ namespace CodeLibrary ...@@ -46,10 +46,10 @@ namespace CodeLibrary
_cam = new HIK(); _cam = new HIK();
((HIK)_cam).Continuous_Event += Camera_Continuous_Event; ((HIK)_cam).Continuous_Event += Camera_Continuous_Event;
break; break;
case CameraType.Basler: //case CameraType.Basler:
_cam = new Basler(); // _cam = new Basler();
((Basler)_cam).Continuous_Event += Camera_Continuous_Event; // ((Basler)_cam).Continuous_Event += Camera_Continuous_Event;
break; // break;
} }
} }
get get
...@@ -78,25 +78,25 @@ namespace CodeLibrary ...@@ -78,25 +78,25 @@ namespace CodeLibrary
/// </summary> /// </summary>
public override string[] Name => _cam.Name; public override string[] Name => _cam.Name;
/// <summary> ///// <summary>
/// 图像宽度 ///// 图像宽度
/// </summary> ///// </summary>
public override int[] Width => _cam.Width; //public override int[] Width => _cam.Width;
/// <summary> ///// <summary>
/// 图像高度 ///// 图像高度
/// </summary> ///// </summary>
public override int[] Height => _cam.Height; //public override int[] Height => _cam.Height;
/// <summary> ///// <summary>
/// 图像缓存 ///// 图像缓存
/// </summary> ///// </summary>
public override byte[] Buffer => _cam.Buffer; //public override byte[] Buffer => _cam.Buffer;
/// <summary> /// <summary>
/// 图像 /// 图像
/// </summary> /// </summary>
public override Bitmap Image => _cam.Image; //public override Bitmap Image => _cam.Image;
...@@ -150,14 +150,14 @@ namespace CodeLibrary ...@@ -150,14 +150,14 @@ namespace CodeLibrary
//{ //{
// return _cam.GrabContinuous( index); // return _cam.GrabContinuous( index);
//} //}
/// <summary> /// <summary>
/// 抓取一次图像 /// 抓取一次图像
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public override bool GrabOne(int index) public override Bitmap GrabOne(int index)
{ {
return _cam.GrabOne( index); return _cam.GrabOne(index);
} }
///// <summary> ///// <summary>
...@@ -175,18 +175,18 @@ namespace CodeLibrary ...@@ -175,18 +175,18 @@ namespace CodeLibrary
/// </summary> /// </summary>
/// <param name="name"></param> /// <param name="name"></param>
/// <returns></returns> /// <returns></returns>
public override bool GrabOne(string name) public override Bitmap GrabOne(string name)
{ {
return _cam.GrabOne(name); return _cam.GrabOne(name);
} }
/// <summary> ///// <summary>
/// 停止连续抓取 ///// 停止连续抓取
/// </summary> ///// </summary>
public override void GrabStop(int index) //public override void GrabStop(int index)
{ //{
_cam.GrabStop( index); // _cam.GrabStop( index);
} //}
/// <summary> /// <summary>
/// 关闭所有相机 /// 关闭所有相机
...@@ -201,16 +201,16 @@ namespace CodeLibrary ...@@ -201,16 +201,16 @@ namespace CodeLibrary
/// </summary> /// </summary>
public override void Close(int index) public override void Close(int index)
{ {
_cam.Close( index); _cam.Close(index);
} }
public override void Close(string name) public override void Close(string name)
{ {
_cam.Close(name); _cam.Close(name);
} }
private static void Camera_Continuous_Event() private static void Camera_Continuous_Event()
{ {
Continuous_Event?.Invoke(); Continuous_Event?.Invoke();
} }
...@@ -237,10 +237,10 @@ namespace CodeLibrary ...@@ -237,10 +237,10 @@ namespace CodeLibrary
} }
public abstract class ClsCamera public abstract class ClsCamera
{ {
/// <summary> /// <summary>
/// 所有相机的名称 /// 所有相机的名称
/// </summary> /// </summary>
protected List<string> cameraName; protected List<string> cameraName;
protected string _errInfo; protected string _errInfo;
protected bool[] _isOpen; protected bool[] _isOpen;
...@@ -248,9 +248,9 @@ namespace CodeLibrary ...@@ -248,9 +248,9 @@ namespace CodeLibrary
protected string[] _name; protected string[] _name;
protected int[] _width; protected int[] _width;
protected int[] _height; protected int[] _height;
protected byte[] _buffer; //protected byte[] _buffer;
protected Bitmap _image; // protected Bitmap _image;
// protected int _index; // protected int _index;
public delegate void Continuous(); public delegate void Continuous();
...@@ -261,10 +261,10 @@ namespace CodeLibrary ...@@ -261,10 +261,10 @@ namespace CodeLibrary
public virtual bool[] IsOpen => _isOpen; public virtual bool[] IsOpen => _isOpen;
public virtual int Count => _count; public virtual int Count => _count;
public virtual string[] Name => _name; public virtual string[] Name => _name;
public virtual int[] Width => _width; //public virtual int[] Width => _width;
public virtual int[] Height => _height; //public virtual int[] Height => _height;
public virtual byte[] Buffer => _buffer; //public virtual byte[] Buffer => _buffer;
public virtual Bitmap Image => _image; // public virtual Bitmap Image => _image;
public abstract bool Load(); public abstract bool Load();
public abstract bool OpenAll(); public abstract bool OpenAll();
...@@ -274,10 +274,10 @@ namespace CodeLibrary ...@@ -274,10 +274,10 @@ namespace CodeLibrary
public abstract void Close(int index); public abstract void Close(int index);
public abstract void Close(string name); public abstract void Close(string name);
// public abstract bool GrabOne(); // public abstract bool GrabOne();
public abstract bool GrabOne(int index); public abstract Bitmap GrabOne(int index);
public abstract bool GrabOne(string name); public abstract Bitmap GrabOne(string name);
// 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);
} }
......
...@@ -36,7 +36,7 @@ namespace CodeLibrary ...@@ -36,7 +36,7 @@ namespace CodeLibrary
/// 连续抓图事件 /// 连续抓图事件
/// </summary> /// </summary>
public event Continuous Continuous_Event; public event Continuous Continuous_Event;
public override void Close(int index) public override void Close(int index)
{ {
if (cameraCurr[index] != null) if (cameraCurr[index] != null)
...@@ -53,7 +53,7 @@ namespace CodeLibrary ...@@ -53,7 +53,7 @@ namespace CodeLibrary
if (index == -1) if (index == -1)
{ {
_errInfo = "Not find"; _errInfo = "Not find";
return ; return;
} }
if (cameraCurr[index] != null) if (cameraCurr[index] != null)
{ {
...@@ -132,12 +132,12 @@ namespace CodeLibrary ...@@ -132,12 +132,12 @@ namespace CodeLibrary
// } // }
//} //}
public override bool GrabOne(int index) public override Bitmap GrabOne(int index)
{ {
if (cameraCurr[index] == null) if (cameraCurr[index] == null)
{ {
_errInfo = "Camera null"; _errInfo = "Camera null";
return false; return null;
} }
try try
...@@ -146,16 +146,16 @@ namespace CodeLibrary ...@@ -146,16 +146,16 @@ namespace CodeLibrary
if (rtn != MyCamera.MV_OK) if (rtn != MyCamera.MV_OK)
{ {
_errInfo = "Can not grab one"; _errInfo = "Can not grab one";
return false; return null;
} }
ImageCapture(index); return ImageCapture(index);
_errInfo = "OK"; _errInfo = "OK";
return true; // return true;
} }
catch (Exception ex) catch (Exception ex)
{ {
_errInfo = ex.Message; _errInfo = ex.Message;
return false; return null;
} }
finally finally
{ {
...@@ -163,25 +163,15 @@ namespace CodeLibrary ...@@ -163,25 +163,15 @@ namespace CodeLibrary
} }
} }
public override bool GrabOne(string name) public override Bitmap GrabOne(string name)
{ {
int idx = Array.FindIndex(_name, s => s == name); int idx = Array.FindIndex(_name, s => s == name);
if (idx == -1) if (idx == -1)
return false; return null;
else else
return GrabOne(idx); return GrabOne(idx);
} }
public override void GrabStop(int index)
{
if (tGrab != null)
tGrab.Abort();
tGrab = null;
continuous = false;
if (cameraCurr == null) return;
int rtn = cameraCurr[index].MV_CC_StopGrabbing_NET();
if (rtn != MyCamera.MV_OK) return;
}
public override bool Load() public override bool Load()
{ {
...@@ -233,7 +223,7 @@ namespace CodeLibrary ...@@ -233,7 +223,7 @@ namespace CodeLibrary
public override bool Open(int index) public override bool Open(int index)
{ {
// _index = index; // _index = index;
if (index < 0 || index >= _count) if (index < 0 || index >= _count)
{ {
_errInfo = "Not find"; _errInfo = "Not find";
...@@ -286,7 +276,7 @@ namespace CodeLibrary ...@@ -286,7 +276,7 @@ namespace CodeLibrary
int n = cameraName.FindIndex(s => s == name); int n = cameraName.FindIndex(s => s == name);
if (n == -1) if (n == -1)
{ {
_errInfo =name+ "Not find"; _errInfo = name + "Not find";
return false; return false;
} }
else else
...@@ -307,8 +297,8 @@ namespace CodeLibrary ...@@ -307,8 +297,8 @@ namespace CodeLibrary
{ {
Open(name); Open(name);
} }
GrabOne(name); return GrabOne(name);
return Image;
} }
public override bool OpenAll() public override bool OpenAll()
{ {
...@@ -321,25 +311,13 @@ namespace CodeLibrary ...@@ -321,25 +311,13 @@ namespace CodeLibrary
return rtn; return rtn;
} }
private Bitmap ImageCapture(int index)
//private void FunGrab(int index)
//{
// while (true)
// {
// ImageCapture(index);
// System.Threading.Thread.Sleep(50);
// Continuous_Event?.Invoke();
// System.Threading.Thread.Sleep(100);
// }
//}
private void ImageCapture(int index)
{ {
MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE(); MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
int rtn = cameraCurr[index].MV_CC_GetIntValue_NET("PayloadSize", ref stParam); int rtn = cameraCurr[index].MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
if (rtn != MyCamera.MV_OK) return; if (rtn != MyCamera.MV_OK) return null;
uint dataSize = stParam.nCurValue; uint dataSize = stParam.nCurValue;
byte[] dataArr = new byte[dataSize]; byte[] dataArr = new byte[dataSize];
uint buffSize = dataSize * 3 + 2048; uint buffSize = dataSize * 3 + 2048;
...@@ -347,7 +325,7 @@ namespace CodeLibrary ...@@ -347,7 +325,7 @@ namespace CodeLibrary
IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement(dataArr, 0); IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement(dataArr, 0);
MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX(); MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
rtn = cameraCurr[index].MV_CC_GetOneFrameTimeout_NET(pData, dataSize, ref stFrameInfo, 100000); rtn = cameraCurr[index].MV_CC_GetOneFrameTimeout_NET(pData, dataSize, ref stFrameInfo, 100000);
if (rtn != MyCamera.MV_OK) return; if (rtn != MyCamera.MV_OK) return null;
MyCamera.MvGvspPixelType enDstPixelType = stFrameInfo.enPixelType; MyCamera.MvGvspPixelType enDstPixelType = stFrameInfo.enPixelType;
switch (stFrameInfo.enPixelType) switch (stFrameInfo.enPixelType)
...@@ -395,28 +373,21 @@ namespace CodeLibrary ...@@ -395,28 +373,21 @@ namespace CodeLibrary
stConverPixelParam.pDstBuffer = pImage; stConverPixelParam.pDstBuffer = pImage;
stConverPixelParam.nDstBufferSize = buffSize; stConverPixelParam.nDstBufferSize = buffSize;
rtn = cameraCurr[index].MV_CC_ConvertPixelType_NET(ref stConverPixelParam); rtn = cameraCurr[index].MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
if (rtn != MyCamera.MV_OK) return; if (rtn != MyCamera.MV_OK) return null;
if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8) if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
{ {
_image = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, pImage); Bitmap _image = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, pImage);
ColorPalette cp = Image.Palette; ColorPalette cp = _image.Palette;
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++)
cp.Entries[i] = Color.FromArgb(i, i, i); cp.Entries[i] = Color.FromArgb(i, i, i);
Image.Palette = cp; _image.Palette = cp;
int picSize = Image.Width * Image.Height;
_buffer = new byte[picSize];
Array.Copy(buffArr, Buffer, picSize);
//int picSize = _image.Width * _image.Height;
//Rectangle rect = new Rectangle(0, 0, Image.Width, Image.Height); //byte[] _buffer = new byte[picSize];
//BitmapData bmpData = Image.LockBits(rect, ImageLockMode.ReadWrite, Image.PixelFormat); //Array.Copy(buffArr, _buffer, picSize);
//IntPtr iPtr = bmpData.Scan0;
//int picSize = Image.Width * Image.Height; return _image;
//Buffer = new byte[picSize];
//Marshal.Copy(iPtr, Buffer, 0, picSize);
//Image.UnlockBits(bmpData);
} }
else else
{ {
...@@ -429,15 +400,16 @@ namespace CodeLibrary ...@@ -429,15 +400,16 @@ namespace CodeLibrary
buffArr[i * stFrameInfo.nWidth * 3 + j * 3 + 2] = chRed; buffArr[i * stFrameInfo.nWidth * 3 + j * 3 + 2] = chRed;
} }
} }
_image = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pImage); Bitmap _image = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pImage);
int picSize = Image.Width * Image.Height * 3; //int picSize = _image.Width * _image.Height * 3;
_buffer = new byte[picSize]; //byte[] _buffer = new byte[picSize];
Array.Copy(buffArr, Buffer, picSize); //Array.Copy(buffArr, _buffer, picSize);
return _image;
} }
} }
} }
} }
\ No newline at end of file \ No newline at end of file
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!