Commit 66950e28 刘韬

导入eyen二维码识别算法

1 个父辈 daf0d3bc
......@@ -65,6 +65,7 @@
<Compile Include="camera\Basler.cs" />
<Compile Include="camera\Common.cs" />
<Compile Include="camera\HIK.cs" />
<Compile Include="eyemDecode.cs" />
<Compile Include="FrmBase.cs">
<SubType>Form</SubType>
</Compile>
......
......@@ -320,11 +320,11 @@
//
this.chbZxing.AutoSize = true;
this.chbZxing.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.chbZxing.Location = new System.Drawing.Point(820, 16);
this.chbZxing.Location = new System.Drawing.Point(792, 16);
this.chbZxing.Name = "chbZxing";
this.chbZxing.Size = new System.Drawing.Size(60, 21);
this.chbZxing.Size = new System.Drawing.Size(103, 21);
this.chbZxing.TabIndex = 31;
this.chbZxing.Text = "ZXing";
this.chbZxing.Text = "EyemDecode";
this.chbZxing.UseVisualStyleBackColor = true;
//
// label5
......
......@@ -15,6 +15,7 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;
namespace CodeLibrary
{
......@@ -43,7 +44,11 @@ namespace CodeLibrary
}
cmbCount.SelectedIndex = 0;
cmbCount.SelectedIndex = 0;
//LoadCamera();
try
{
LoadCamera();
}
catch { }
cmbCodeType.DataSource = HDCodeLearnHelper.codeTypeList;
if (HDCodeLearnHelper.codeTypeList.Count > 0)
......@@ -82,6 +87,8 @@ namespace CodeLibrary
cmbCamera.Items.Clear();
Camera.Type = CameraType.HIK;
Camera._cam.Load();
if (Camera._cam.Name == null)
return;
foreach (string str in Camera._cam.Name)
{
cmbCamera.Items.Add(str);
......@@ -119,6 +126,11 @@ namespace CodeLibrary
if (hObject != null)
{
HDCodeLearnHelper.DefaultImage = hObject;
HDCodeHelper.HObject2Bpp8(hObject, out HDCodeLearnHelper.DefaultBitmap);
//pictureBox1.Image = (Bitmap)bitmap.Clone();
//HDCodeLearnHelper.DefaultBitmap = (Bitmap)bitmap.Clone();
//bitmap.Dispose();
ShowImage(hObject);
}
else
......@@ -128,11 +140,13 @@ namespace CodeLibrary
}
else
{
if (HDCodeLearnHelper.DefaultBitmap != null)
HDCodeLearnHelper.DefaultBitmap.Dispose();
//读取图片内容
Image img = (Image)Image.FromFile(filename).Clone();
pictureBox1.Image = img;
Bitmap bitmap = new Bitmap(pictureBox1.Image);
Bitmap bitmap = new Bitmap(filename);
HDCodeLearnHelper.DefaultBitmap = bitmap;
HObject hoimg;
HDCodeHelper.Bitmap2HObjectBpp24(bitmap,out hoimg);
HDCodeLearnHelper.DefaultImage = hoimg;
......@@ -250,11 +264,16 @@ namespace CodeLibrary
{
if (pictureBox1.Image == null)
{
MessageBox.Show(selImage, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
//MessageBox.Show(selImage, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
//return;
}
if (HDCodeLearnHelper.DefaultBitmap != null)
{
Bitmap b = (Bitmap)HDCodeLearnHelper.DefaultBitmap.Clone();
List<CodeInfo> codeList = EyemDecode.Decoder(ref b);
ShowCode(codeList);
return;
}
Bitmap map = new Bitmap(pictureBox1.Image);
zxingDecode(map, cmbCodeType.Text);
}
else
{
......@@ -275,6 +294,8 @@ namespace CodeLibrary
ShowCode(codeList);
txtResult.Text += "\r\n elapsed time:" + stopwatch.Elapsed.ToString();
}
}
private void zxingDecode(Bitmap map, string type)
{
......@@ -288,13 +309,15 @@ namespace CodeLibrary
txtResult.Text += "\r\n \r\n elapsed time:" + stopwatch.Elapsed.ToString();
}
private void btnClearLog_Click(object sender, EventArgs e)
{
HDLogUtil.ClearLog();
txtResult.Text = "";
}
private HObject GetCameraBitmap()
private HObject GetCameraBitmap(out Bitmap bitmap)
{
bitmap = null;
try
{
int index = cmbCamera.SelectedIndex;
......@@ -305,11 +328,11 @@ namespace CodeLibrary
return null;
}
HObject map;
map = Camera._cam.CaptureOnImage(camerName );
if ( map == null)
map = Camera._cam.CaptureOnImage(camerName, out bitmap);
if (map == null)
{
Camera._cam.Close(camerName);
map = Camera._cam.CaptureOnImage(camerName );
map = Camera._cam.CaptureOnImage(camerName, out bitmap);
}
return map;
}
......@@ -325,7 +348,7 @@ namespace CodeLibrary
{
if (!chbZxing.Checked)
{
HObject bitmap = GetCameraBitmap();
HObject bitmap = GetCameraBitmap(out Bitmap b);
if (bitmap != null)
{
HDLogUtil.info("从相机【" + cmbCamera.Text + "】获取到一张图片");
......@@ -333,6 +356,7 @@ namespace CodeLibrary
//pictureBox1.Image = (Image)bitmap.Clone();
//HObject hoImage = HDCodeHelper.Bitmap2HObjectBpp24(bitmap);
HDCodeLearnHelper.DefaultImage = bitmap;
HDCodeLearnHelper.DefaultBitmap = b;
ShowImage(bitmap);
}
}
......@@ -365,6 +389,8 @@ namespace CodeLibrary
private void btnExit_Click(object sender, EventArgs e)
{
if (HDCodeLearnHelper.DefaultBitmap != null)
HDCodeLearnHelper.DefaultBitmap.Dispose();
this.Close();
}
......
......@@ -5,6 +5,7 @@ using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
......@@ -243,8 +244,11 @@ namespace CodeLibrary
ho_Image.Dispose();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
HOperatorSet.GenImageInterleaved(out ho_Image, srcBmpData.Scan0, "bgrx", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
if (bmp.PixelFormat == PixelFormat.Format8bppIndexed)
HOperatorSet.GenImage1(out ho_Image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
else
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;
......@@ -256,7 +260,42 @@ namespace CodeLibrary
}
return false ;
}
public static void HObject2Bpp8(HObject image, out Bitmap res)
{
HTuple hpoint, type, width, height;
const int Alpha = 255;
long[] ptr = new long[2];
HOperatorSet.GetImagePointer1(image, out hpoint, out type, out width, out height);
res = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
ColorPalette pal = res.Palette;
for (int i = 0; i <= 255; i++)
{
pal.Entries[i] = Color.FromArgb(Alpha, i, i, i);
}
res.Palette = pal;
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = res.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
ptr[0] = (long)bitmapData.Scan0;
ptr[1] = hpoint.L;
if (width % 4 == 0)
CopyMemory((IntPtr)ptr[0], (IntPtr)ptr[1], width * height * PixelSize);
else
{
for (int i = 0; i < height - 1; i++)
{
ptr[1] += width;
CopyMemory((IntPtr)ptr[0], (IntPtr)ptr[1], width * PixelSize);
ptr[0] += bitmapData.Stride;
}
}
res.UnlockBits(bitmapData);
}
[DllImport("Kernel32.dll")]
private static extern void CopyMemory(IntPtr dest, IntPtr source, int size);
internal static string GetCodeParamFilePath(string codeType)
{
if (CodeParamPath.Equals(""))
......
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
......@@ -17,6 +18,7 @@ namespace CodeLibrary
/// 图片对象,学习时如果使用图片学习,需要先设置此属性
/// </summary>
public static HObject DefaultImage = null;
public static Bitmap DefaultBitmap = null;
/// <summary>
/// 学习后是否自动测试,默认是
/// </summary>
......
......@@ -228,9 +228,14 @@ namespace CodeLibrary
}
return null;
}
public override HObject CaptureOnImage(string name )
public override HObject CaptureOnImage(string name)
{
return CaptureOnImage(name, out _);
}
public override HObject CaptureOnImage(string name,out Bitmap bmp)
{
HObject hoImage = null;
bmp = null;
int index = cameraName.FindIndex(s => s == name);
if (index == -1)
{
......
......@@ -113,6 +113,7 @@ namespace CodeLibrary
// public abstract void GrabStop(int index);
public abstract Bitmap GrabOneImage(string name);
public abstract HObject CaptureOnImage(string name );
public abstract HObject CaptureOnImage(string name,out Bitmap bmp);
}
}
......@@ -48,6 +48,8 @@ namespace CodeLibrary
}
public override void CloseAll()
{
if (cameraCurr == null)
return;
for (int i = 0; i < cameraCurr.Length; i++)
{
if (cameraCurr[i] != null)
......@@ -222,7 +224,7 @@ namespace CodeLibrary
if (n == -1)
{
_errInfo = name + " Not find";
HDLogUtil.error("GrabOneImage ["+name+"] error : " + _errInfo);
HDLogUtil.error("GrabOneImage [" + name + "] error : " + _errInfo);
return null;
}
if (cameraCurr[n] != null)
......@@ -342,9 +344,17 @@ namespace CodeLibrary
return _image;
}
}
public override HObject CaptureOnImage(string name )
public override HObject CaptureOnImage(string name)
{
var r = CaptureOnImage(name, out Bitmap bmp);
bmp.Dispose();
return r;
}
public override HObject CaptureOnImage(string name, out Bitmap bmp)
{
HObject hoImage = null;
HObject hoImage = null;
bmp = null;
int index = cameraName.FindIndex(s => s == name);
if (index == -1)
{
......@@ -381,7 +391,60 @@ namespace CodeLibrary
//HObject ho_Imagetemp;
HOperatorSet.GenImage1(out hoImage, "byte", FrameInfo.stFrameInfo.nWidth, FrameInfo.stFrameInfo.nHeight, FrameInfo.pBufAddr);
try
{
HDLogUtil.info(FrameInfo.stFrameInfo.enPixelType.ToString());
bmp = new Bitmap(FrameInfo.stFrameInfo.nWidth, FrameInfo.stFrameInfo.nHeight, FrameInfo.stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, FrameInfo.pBufAddr);
ColorPalette pal = bmp.Palette;
for (int i = 0; i <= 255; i++)
{
pal.Entries[i] = Color.FromArgb(i, i, i);
}
bmp.Palette = pal;
/*
var width = FrameInfo.stFrameInfo.nWidth;
var height = FrameInfo.stFrameInfo.nHeight;
const int Alpha = 255;
IntPtr[] ptr = new IntPtr[2];
bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
ColorPalette pal = bmp.Palette;
for (int i = 0; i <= 255; i++)
{
pal.Entries[i] = Color.FromArgb(Alpha, i, i, i);
}
bmp.Palette = pal;
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = bmp.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
ptr[0] = bitmapData.Scan0;
ptr[1] = FrameInfo.pBufAddr;
//Marshal.Copy()
if (width % 4 == 0)
{
HDLogUtil.info("CopyMemory1");
CopyMemory((IntPtr)ptr[0], (IntPtr)ptr[1], width * height * PixelSize);
}
else
{
HDLogUtil.info("CopyMemory2");
for (int i = 0; i < height - 1; i++)
{
ptr[1] += width;
CopyMemory((IntPtr)ptr[0], (IntPtr)ptr[1], width * PixelSize);
ptr[0] += bitmapData.Stride;
}
}
bmp.UnlockBits(bitmapData);
HDLogUtil.debug("UnlockBits");
//bmp.Save(@"D:\image\"+DateTime.Now.Ticks.ToString() + "test.bmp");
*/
}
catch (Exception ex)
{
HDLogUtil.error(ex.ToString());
}
if (FrameInfo.pBufAddr != IntPtr.Zero)
{
nRet = cameraCurr[index].MV_CC_FreeImageBuffer_NET(ref FrameInfo);
......@@ -396,6 +459,7 @@ namespace CodeLibrary
else
{
HDLogUtil.error(" [" + name + "] MV_CC_GetImageBuffer_NET No data: " + nRet);
}
}
catch (Exception ex)
......@@ -417,6 +481,16 @@ namespace CodeLibrary
}
return hoImage;
}
}
[DllImport("Kernel32.dll")]
private static extern void CopyMemory(IntPtr dest, IntPtr source, int size);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern void OutputDebugString(string message);
HalconDotNet.HalconAPI.HClearProcCallBack callback = __OnFreeCallBack;
private static void __OnFreeCallBack(IntPtr pImg)
{
Marshal.FreeHGlobal(pImg);
}
}
}
\ No newline at end of file
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace CodeLibrary
{
public unsafe class EyemDecode
{
/// <summary>
/// 识别图像二维码
/// </summary>
/// <param name="bmap">需要手动释放这个bitmap</param>
/// <param name="file">如果传文件的话,bmp传null</param>
/// <returns></returns>
public static List<CodeInfo> Decoder(ref Bitmap bmap, string file = null)
{
List<CodeInfo> codelist = new List<CodeInfo>();
try
{
//创建图像引用
EyemImage eyemImage;
BitmapData bitmapData = null;
if (file == null)
{
Rectangle rect = new Rectangle(0, 0, bmap.Width, bmap.Height);
bitmapData = bmap.LockBits(rect, ImageLockMode.ReadOnly, bmap.PixelFormat);
eyemImage = new EyemImage();
eyemImage.ucpImage = bitmapData.Scan0;
eyemImage.iWidth = rect.Width;
eyemImage.iHeight = rect.Height;
eyemImage.iChannel = 0;
}
else
{
eyemImageRead(file, 3, out eyemImage);
}
//创建扫描区域
EyemRect eyemRect = new EyemRect();
eyemRect.iXs = 0;
eyemRect.iYs = 0;
eyemRect.iWidth = eyemImage.iWidth;
eyemRect.iHeight = eyemImage.iHeight;
string codeType = "DataMatrix";//QRCode
codeType = "QRCode";
int ipNum;
EyemBarCode* tpResults;
DataCodeHandle hObject = null;
try
{
int result = eyemDetectAndDecode(eyemImage, eyemRect, "", codeType, out hObject, out tpResults, out ipNum, false, 11, 5, 128, 215);
HDLogUtil.info($"eyemDetectAndDecode:{result},ipNum:{ipNum}");
if (result != 0 || ipNum == 0)
return codelist;
for (int i = 0; i < ipNum; i++)
{
CodeInfo ci = new CodeInfo(Marshal.PtrToStringAnsi(tpResults[i].hText), tpResults[i].iCenterX, tpResults[i].iCenterY);
ci.CodeType = Marshal.PtrToStringAnsi(tpResults[i].hType);
codelist.Add(ci);
}
}
finally
{
if (hObject != null)
hObject.Dispose();
if (bitmapData != null)
bmap.UnlockBits(bitmapData);
if (file != null)
eyemImageFree(eyemImage.ucpImage);
//bmap.Dispose();
}
}
catch (Exception ex)
{
HDLogUtil.error(ex.ToString());
}
//eyemImageFree(eyemImage.ucpImage);
return codelist;
}
//释放解码句柄
private class DataCodeHandle : SafeHandleZeroOrMinusOneIsInvalid
{
public DataCodeHandle() : base(true) { }
protected override bool ReleaseHandle()
{
return eyemDetectAndDecodeFree(handle);
}
}
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemDetectAndDecode(EyemImage tpImage, EyemRect tpRoi, string fileName, string strCodeType, out DataCodeHandle hObject, out EyemBarCode* tpResults, out int ipNum, bool bUseNiBlack, int iBlockSize, int iRangeC, int iSymbolMin, int iSymbolMax, double dScaleUpAndDown = 0.5, double dToleErr = 0.5, double dMinorStep = 1.0);
/// <summary>
/// 读取图像
/// </summary>
/// <param name="filename"></param>
/// <param name="iFalgs"></param>
/// <param name="ucpImage"></param>
/// <returns></returns>
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemImageRead(string filename, int iFalgs, out EyemImage ucpImage);
/// <summary>
/// 释放图像资源
/// </summary>
/// <param name="ipImage"></param>
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern void eyemImageFree(IntPtr ipImage);
//释放工具
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern bool eyemDetectAndDecodeFree(IntPtr hObject);
[StructLayout(LayoutKind.Sequential)]
public struct EyemImage
{
public IntPtr ucpImage; // 地址
public int iWidth; // 图像内存 x 方向大小
public int iHeight; // 图像内存 y 方向大小
public int iChannel; // 图像通道数
}
[StructLayout(LayoutKind.Sequential)]
public struct EyemRect
{
public int iXs; // 起始点(左上角) x 坐标
public int iYs; // 起始点(左上角) y 坐标
public int iWidth; // x 方向大小(宽度)
public int iHeight; // y 方向大小(高度)
}
[StructLayout(LayoutKind.Sequential)]
public struct EyemBarCode
{
public int iCenterX; // y坐标
public int iCenterY; // y坐标
public IntPtr hType; // 码类型
public IntPtr hText; // 码内容
}
}
}
......@@ -358,7 +358,7 @@ namespace DeviceLib
if (_serialPort.IsOpen)
{
if (Monitor.TryEnter(lockObj, Overtime + 80))
if (Monitor.TryEnter(lockObj, Overtime + 120))
{
//Monitor.Enter(lockObj);
try
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!