#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
struct Data
{
int data[3];
};
Mat img = imread("Demo.jpg");
extern "C" __declspec(dllexport) void GetSize(Data *pdata)
{
int h = img.rows;
int w = img.cols;
int c = img.channels();
pdata->data[0] = h;
pdata->data[1] = w;
pdata->data[2] = c;
}
extern "C" __declspec(dllexport) unsigned char* GetImage(int height, int width, int channels)
{
size_t size = height * width * channels;
unsigned char* data = new unsigned char[size];
memcpy(data, img.data, size);
return data;
}
using System;
using OpenCvSharp;
using System.Runtime.InteropServices;
namespace Test
{
struct Data
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] data;
};
class Test
{
[DllImport("Opendll.dll", EntryPoint = "GetSize", CallingConvention = CallingConvention.Cdecl)]
public extern static void Size(ref Data data);
[DllImport("Opendll.dll", EntryPoint = "GetImage", CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr Show(int h, int w, int c);
}
class Program
{
static void Main(string[] args)
{
Data sd = new Data();
Test.Size(ref sd);
byte[] imagedata = new byte[sd.data[0] * sd.data[1] * sd.data[2]];
IntPtr imgptr = Test.Show(sd.data[0], sd.data[1], sd.data[2]);
Marshal.Copy(imgptr, imagedata, 0, imagedata.Length);
Mat img = new Mat(sd.data[0], sd.data[1], MatType.CV_8UC3);
Marshal.Copy(imagedata, 0, img.Data, imagedata.Length);
Cv2.ImWrite("demo.jpg", img);
}
}
}