Commit 7ab790ee LN

hik获取图片修改

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