Recently done a project, the other company only provided a C + + DLL, but not blocked, C # can not dllimport. So you can only write C + + to seal it.
Each other's DLL only receives yuv420 picture format, so in C + + with OpenCV to convert. Originally wanted C # to write, but always feel trouble.
"C #" code
public void Do (Bitmap BP)
{
Bitmap outimg = new Bitmap (BP. WIDTH,BP. Height);
System.Drawing.Imaging.BitmapData bmpdata = BP. LockBits (New System.Drawing.Rectangle (0, 0, BP). Width, BP. Height), System.Drawing.Imaging.ImageLockMode.ReadWrite,
Bp. PixelFormat);
System.Drawing.Imaging.BitmapData bmpData1 = outimg. LockBits (New System.Drawing.Rectangle (0, 0, outimg. Width, Outimg. Height), System.Drawing.Imaging.ImageLockMode.ReadWrite,
Outimg. PixelFormat);
Cfunction (Bmpdata.scan0, BP. Width, BP. Height, 3, 0,bmpdata1.scan0);
Img. Unlockbits (Bmpdata);
Outimg.unlockbits (BMPDATA1);
}
"C + +" code
extern "C" __declspec (dllexport) void WINAPI cfunction (unsigned char* img,int width,int height,int ch,int format,unsigned char* outimg)
{
Mat Frame (Size (width, height), CV_8UC (CH), IMG);
Mat dest;
Cvtcolor (frame, dest, format = = 0?) cv_bgr2yuv_i420:cv_rgb2yuv_i420);
Third-party DLLs (DEST);
Mat RGB;
Cvtcolor (dest, RGB, format = = 0?) cv_yuv2bgr_i420:cv_yuv2rgb_i420);
memcpy ((void*) outimg, (void*) dest.data, Rgb.cols * rgb.rows * rgb.channels ());
}
This basically implements the C + + and C # Direct image docking. But this approach will card C # program, I tried Backgroundworker,task, a lot of methods, but C # is stuck. The specific reason is not very clear, so I think this part of the processing in C + + with threads to do,
Notify C # When you're done, because I need to generate four result images in a third-party DLL. So the structure is defined.
"C #"
[StructLayout (LayoutKind.Sequential)]
public struct Imageresult
{
public INTPTR first;
Public IntPtr Second;
public INTPTR third;
Public IntPtr Forth;
}
"C + +"
struct Imgeresult
{
void * FIRSTIMG;
void* secondimg;
void* thirdimg;
void* fouthimg;
};
This is followed by a delegate defined in C # to the C + + callback.
"C #"
public delegate void Cscallback (Imageresult ir);
public static Cscallback callback;
callback = Cscallbackfunction;
Setcallback (callback);
public static void Cscallbackfunction (Imageresult tick)
{
Working with data returned by C + +
}
Calling C + + thread functions
public void Do (Bitmap BP)
{
Bitmap outimg = new Bitmap (BP. WIDTH,BP. Height);
System.Drawing.Imaging.BitmapData bmpdata = BP. LockBits (New System.Drawing.Rectangle (0, 0, BP). Width, BP. Height), System.Drawing.Imaging.ImageLockMode.ReadWrite,
Bp. PixelFormat);
Doimage (Bmpdata.scan0, BP. Width, BP. Height, 3, 0,bmpdata1.scan0);
Img. Unlockbits (Bmpdata);
Outimg.unlockbits (BMPDATA1);
}
"C + +"
typedef void (__stdcall *cppcallback) (Imgeresult ir);
Cppcallback Mycallback;
extern "C" __declspec (dllexport) void WINAPI Setcallback (Cppcallback callback)
{
Mycallback = callback;
}
void Operateimage (unsigned char* img,int width,int height,int ch,int format)
{
Mat Frame (Size (width, height), CV_8UC (CH), IMG);
Mat dest;
Cvtcolor (frame, dest, format = = 0?) cv_bgr2yuv_i420:cv_rgb2yuv_i420);
Mat second;
Cvtcolor (frame, second, format = = 0?) cv_bgr2yuv_i420:cv_rgb2yuv_i420);
Mat third;
Cvtcolor (frame, third, format = = 0?) cv_bgr2yuv_i420:cv_rgb2yuv_i420);
Mat forth;
Cvtcolor (frame, forth, format = = 0?) cv_bgr2yuv_i420:cv_rgb2yuv_i420);
Mat Nextfirst;
Mat Nextsecond;
Mat Nextthird;
Mat Nexttforth;
Third-party library processing pictures
Cvtcolor (dest, nextfirst, format = = 0?) cv_yuv2bgr_i420:cv_yuv2rgb_i420);
Cvtcolor (second, nextsecond, format = = 0?) cv_yuv2bgr_i420:cv_yuv2rgb_i420);
Cvtcolor (third, nextthird, format = = 0?) cv_yuv2bgr_i420:cv_yuv2rgb_i420);
Cvtcolor (Forth, nexttforth, format = = 0?) cv_yuv2bgr_i420:cv_yuv2rgb_i420);
Releasebellefaceengine ();
ir. Firstimg = (void *) malloc (nextfirst.cols * nextfirst.rows * nextfirst.channels ());
ir. Secondimg = (void *) malloc (nextsecond.cols * nextsecond.rows * nextsecond.channels ());
ir. Thirdimg = (void *) malloc (nextthird.cols * nextthird.rows * nextthird.channels ());
ir. Fouthimg = (void *) malloc (nexttforth.cols * nexttforth.rows * nexttforth.channels ());
memcpy (IR. Firstimg, (void*) nextfirst.data, Nextfirst.cols * nextfirst.rows * nextfirst.channels ());
memcpy (IR. Secondimg, (void*) nextsecond.data, Nextsecond.cols * nextsecond.rows * nextsecond.channels ());
memcpy (IR. Thirdimg, (void*) nextthird.data, Nextthird.cols * nextthird.rows * nextthird.channels ());
memcpy (IR. Fouthimg, (void*) nexttforth.data, Nexttforth.cols * nexttforth.rows * nexttforth.channels ());
Mycallback (IR);
}
extern "C" __declspec (dllexport) void WINAPI doimage (unsigned char* img,int width,int height,int ch,int format)
{
Std::thread th1 (Operateimage, img, width, height,ch,format);
Th1.detach ();
}
This will not get stuck in a C # program.
Because I first use C + +, is also a side check, while writing.
C + + Write some tips.
(1) Use OpenCV to be aware that the link library is debug or release.
(2) to the pure system to install REDIST, my target machine is 64 bits, the development environment is 2013, so to be installed vs2013redist.
(3) Finally pay attention to put Msvcp120.dll,msvcp120d.dll,msvcr120.dll,msvcr120d.dll,vccorlib120.dll,vccorlib120d.dll to C:\Windows\ In the SysWOW64 directory, the 32-bit target machine is placed below the Win32.
If there is a better way, please tell the younger brother.
Some things that C # interacts with C + +