Commit 1181d725 张士柳

1 个父辈 deb5735c
......@@ -4,68 +4,68 @@ using System.Runtime.InteropServices;
namespace eyemLib_Sharp
{
public unsafe class EyemSamples
{
#region 通用
//public unsafe class EyemSamples
//{
// #region 通用
// Win32 memory copy function
[DllImport("ntdll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern byte* memcpy(byte* dst, byte* src, int count);
//读取图像
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemImageRead(string filename, int iFalgs, out EyemImage ucpImage);
//保存图像
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemImageWrite(string filename, EyemImage ipImage);
//释放图像资源
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern void eyemImageFree(IntPtr ipImage);
// // Win32 memory copy function
// [DllImport("ntdll.dll", CallingConvention = CallingConvention.Cdecl)]
// private static extern byte* memcpy(byte* dst, byte* src, int count);
// //读取图像
// [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
// private static extern int eyemImageRead(string filename, int iFalgs, out EyemImage ucpImage);
// //保存图像
// [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
// private static extern int eyemImageWrite(string filename, EyemImage ipImage);
// //释放图像资源
// [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
// private static extern void eyemImageFree(IntPtr ipImage);
#endregion
// #endregion
//读码程序
[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 dToleErr = 0.5, double dMinorStep = 1.0);
//释放工具
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern bool eyemDetectAndDecodeFree(IntPtr hObject);
// //读码程序
// [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 dToleErr = 0.5, double dMinorStep = 1.0);
// //释放工具
// [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
// private static extern bool eyemDetectAndDecodeFree(IntPtr hObject);
public static void eyemReadImageTool(string fileName)
{
EyemImage ucpImage;
int flag = eyemImageRead(fileName, -1, out ucpImage);
if (flag != 0)
{
Console.WriteLine("读图失败!");
return;
}
EyemRect tpRoi = new EyemRect();
tpRoi.iXs = tpRoi.iYs = 0;
tpRoi.iWidth = ucpImage.iWidth;
tpRoi.iHeight = ucpImage.iHeight;
// public static void eyemReadImageTool(string fileName)
// {
// EyemImage ucpImage;
// int flag = eyemImageRead(fileName, -1, out ucpImage);
// if (flag != 0)
// {
// Console.WriteLine("读图失败!");
// return;
// }
// EyemRect tpRoi = new EyemRect();
// tpRoi.iXs = tpRoi.iYs = 0;
// tpRoi.iWidth = ucpImage.iWidth;
// tpRoi.iHeight = ucpImage.iHeight;
string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];
int ipNum; EyemBarCode* tpResults;
DataCodeHandle hObject;
eyemDetectAndDecode(ucpImage, tpRoi, file.Replace(".png", ""), "QRCode", out hObject, out tpResults, out ipNum, false, 11, 5, 128, 215);
for (int i = 0; i < ipNum; i++)
{
Console.WriteLine("类型:" + Marshal.PtrToStringAnsi(tpResults[i].hType) + ";坐标" + "[" + tpResults[i].iCenterX.ToString() + "," + tpResults[i].iCenterY.ToString() + "]" + ";内容:" + Marshal.PtrToStringAnsi(tpResults[i].hText) + "");
}
//释放
hObject.Dispose();
//释放图像
eyemImageFree(ucpImage.ucpImage);
}
// string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];
// int ipNum; EyemBarCode* tpResults;
// DataCodeHandle hObject;
// eyemDetectAndDecode(ucpImage, tpRoi, file.Replace(".png", ""), "QRCode", out hObject, out tpResults, out ipNum, false, 11, 5, 128, 215);
// for (int i = 0; i < ipNum; i++)
// {
// Console.WriteLine("类型:" + Marshal.PtrToStringAnsi(tpResults[i].hType) + ";坐标" + "[" + tpResults[i].iCenterX.ToString() + "," + tpResults[i].iCenterY.ToString() + "]" + ";内容:" + Marshal.PtrToStringAnsi(tpResults[i].hText) + "");
// }
// //释放
// hObject.Dispose();
// //释放图像
// eyemImageFree(ucpImage.ucpImage);
// }
//释放解码句柄
public class DataCodeHandle : SafeHandleZeroOrMinusOneIsInvalid
{
public DataCodeHandle() : base(true) { }
protected override bool ReleaseHandle()
{
return eyemDetectAndDecodeFree(handle);
}
}
}
// //释放解码句柄
// public class DataCodeHandle : SafeHandleZeroOrMinusOneIsInvalid
// {
// public DataCodeHandle() : base(true) { }
// protected override bool ReleaseHandle()
// {
// return eyemDetectAndDecodeFree(handle);
// }
// }
//}
}
......@@ -17,12 +17,11 @@ namespace eyemLib_Sharp
// {
// EyemLib.eyemReadImageTool(fn);
// });
// Console.Clear();
// Console.Write("请按任意键继续。。。");
//}
foreach (var fileName in fileNames)
{
EyemLib.eyemReadImageTool(fileName);
//EyemLib.eyemReadImageToolTest(fileName);
}
Console.Write("请按任意键继续。。。");
Console.ReadKey();
......
......@@ -5,37 +5,143 @@ int eyemImageRead(const char *filename, int iFlag, EyemImage *tpImage)
cv::Mat src = cv::imread(filename, iFlag);
if (src.empty())
return FUNC_IMAGE_NOT_EXIST;
//仅支持灰度图像
if (src.channels() != 1)
cv::cvtColor(src, src, cv::COLOR_BGR2GRAY);
//
tpImage->iWidth = src.cols;
tpImage->iHeight = src.rows;
tpImage->iDepth = src.depth();
//仅支持8位与16位图
//图像信息
int _X = src.cols, _Y = src.rows, _Depth = src.depth(), _Channels = src.channels();
//内存尺寸
int _Size = _X*_Y*_Channels;
switch (src.depth())
{
case CV_8U:
tpImage->vpImage = (unsigned char *)malloc(src.cols*src.rows * sizeof(unsigned char));
_Size *= sizeof(unsigned char);
tpImage->vpImage = (unsigned char *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, src.cols*src.rows * sizeof(unsigned char));
memset(tpImage->vpImage, 0, _Size);
break;
case CV_8S:
_Size *= sizeof(signed char);
tpImage->vpImage = (signed char *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_16U:
tpImage->vpImage = (unsigned short *)malloc(src.cols*src.rows * sizeof(unsigned short));
_Size *= sizeof(unsigned short);
tpImage->vpImage = (unsigned short *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, src.cols*src.rows * sizeof(unsigned short));
memset(tpImage->vpImage, 0, _Size);
break;
default:
case CV_16S:
_Size *= sizeof(signed short);
tpImage->vpImage = (signed short *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_32S:
_Size *= sizeof(signed int);
tpImage->vpImage = (signed int *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_32F:
_Size *= sizeof(float);
tpImage->vpImage = (float *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_64F:
_Size *= sizeof(double);
tpImage->vpImage = (double *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
default:
//no support format
return FUNC_CANNOT_USE;
}
//图像信息
tpImage->iWidth = _X; tpImage->iHeight = _Y; tpImage->iDepth = _Depth; tpImage->iChannels = _Channels;
//拷贝数据
int _Size = src.cols*src.rows*((src.depth() >> 1) + 1);
memcpy(tpImage->vpImage, src.data, _Size);
return FUNC_OK;
}
int eyemImageMalloc(int iWidth, int iHeight, int iChannels, int ccType, EyemImage *tpImage)
{
//内存尺寸
int _Size = iWidth*iHeight*iChannels;
switch (ccType)
{
case CV_8U:
tpImage->iDepth = 0;
_Size *= sizeof(unsigned char);
tpImage->vpImage = (unsigned char *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_8S:
tpImage->iDepth = 1;
_Size *= sizeof(signed char);
tpImage->vpImage = (signed char *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_16U:
tpImage->iDepth = 2;
_Size *= sizeof(unsigned short);
tpImage->vpImage = (unsigned short *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_16S:
tpImage->iDepth = 3;
_Size *= sizeof(signed short);
tpImage->vpImage = (signed short *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_32S:
tpImage->iDepth = 4;
_Size *= sizeof(signed int);
tpImage->vpImage = (signed int *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_32F:
tpImage->iDepth = 5;
_Size *= sizeof(float);
tpImage->vpImage = (float *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_64F:
tpImage->iDepth = 6;
_Size *= sizeof(double);
tpImage->vpImage = (double *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
default:
//no support format
return FUNC_CANNOT_USE;
}
//图像信息
tpImage->iWidth = iWidth; tpImage->iHeight = iHeight; tpImage->iChannels = iChannels;
return FUNC_OK;
}
int eyemImageReadRaw(const char *filename, int iWidth, int iHeight, int iDepth, EyemImage *tpImage)
{
if (std::strlen(filename) == 0)
......@@ -282,10 +388,14 @@ static bool checkSize(cv::Mat &srcPrev, cv::Mat &mask, int &partSize)
int eyemCountObject(EyemImage tpImage, const char *fileName, double dOffset, int iMinArea, int iMaxArea, int iWinSize, LPSTR *lpszNumObj, EyemImage *tpDstImg)
{
cv::Mat src = cv::Mat(tpImage.iHeight, tpImage.iWidth, tpImage.iDepth, tpImage.vpImage);
cv::Mat src = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage);
if (src.empty()) {
return FUNC_IMAGE_NOT_EXIST;
}
//转单通道
if (src.channels() != 1)
cv::cvtColor(src, src, cv::COLOR_BGR2GRAY);
cv::Mat src8U;
//环鸿&佳世达
src = src(cv::Range(200, src.cols - 70), cv::Range(200, src.rows - 10)).clone();
......@@ -1259,10 +1369,14 @@ int eyemCountObject(EyemImage tpImage, const char *fileName, double dOffset, int
int eyemCountObjectIrregularParts(EyemImage tpImage, const char *fileName, double dOffset, const char *ccSubType, int iMaxArea, int iWinSize, LPSTR *lpszNumObj, EyemImage *tpDstImg)
{
cv::Mat src = cv::Mat(tpImage.iHeight, tpImage.iWidth, tpImage.iDepth, tpImage.vpImage);
cv::Mat src = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage);
if (src.empty()) {
return FUNC_IMAGE_NOT_EXIST;
}
//转单通道
if (src.channels() != 1)
cv::cvtColor(src, src, cv::COLOR_BGR2GRAY);
cv::Mat src8U;
//环鸿&佳世达
src = src(cv::Range(200, src.cols - 70), cv::Range(200, src.rows - 10)).clone();
......@@ -2799,5 +2913,4 @@ void eyemImageFree(void *ucpImage)
{
// must be free
free(ucpImage);
ucpImage = NULL;
}
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!