vendredi 24 octobre 2014

Cannot capture webcam using dll created by C++ in a C# application


Vote count:

0




I am going to create a C# application to capture webcam using a dll created by C++. This dll allow me to access OpenCV library. At the moment, I use Visual Studio 2013 Expression and OpenCV 2.49 version. I have already succeeded in capturing Webcam in C++ program. When I build this into dll and read this dll from C# code, an error occurred. The error content is:



An unhandled exception of type 'System.AccessViolationException' occurred in Webcam.exe

Additional Information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


The dll code I built in C++ likes this.



#include <iostream>
#include <Windows.h>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\core\core.hpp>

typedef struct _IplImageCs
{
int width;
int height;
int imageSize;
}
IplImageCs;

typedef IplImageCs* pIplImageCs;

extern "C"
{
_declspec(dllexport) int CaptureFromCamera(int num, pIplImageCs imgCs);
_declspec(dllexport) void QueryFrame(LPBYTE imageData);
_declspec(dllexport) void ReleaseCaptureCamera(void);
}

IplImage* img;
CvCapture* capture;
pIplImageCs imgCs;

_declspec(dllexport) int CaptureFromCamera(int num, pIplImageCs imgCs)
{
//capture = cvCaptureFromCAM(num);
capture = cvCreateCameraCapture(num);
if (capture == NULL)
return -1;
img = cvQueryFrame(capture);
imgCs->width = img->width;
imgCs->height = img->height;
imgCs->imageSize = img->imageSize;
return 0;
}

_declspec(dllexport) void QueryFrame(LPBYTE imageData)
{
if (capture == NULL)
return;
img = cvQueryFrame(capture);
memcpy(imageData, img->imageData, img->imageSize);
}

_declspec(dllexport) void ReleaseCaptureCamera(void)
{
cvReleaseCapture(&capture);
}


In the C#, I wrote this.



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Webcam
{
public partial class Form1 : Form
{
private bool bContinue;
private bool bCompleted;
private BackgroundWorker bw;
private Bitmap bmp;

[StructLayout(LayoutKind.Sequential)]
private struct IplImage
{
public int width;
public int height;
public int imageSize;
public IplImage(int init)
{
width = init;
height = init;
imageSize = init;
}
}
IplImage img;

[DllImport("Show Camera.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int CaptureFromCamera(int num, ref IplImage img);
[DllImport("Show Camera.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void QueryFrame(Byte[] imageData);
[DllImport("Show Camera.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void ReleaseCaptureCamera();

public Form1()
{
InitializeComponent();
panel2.AutoScroll = true;
bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(Processing);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedProcessing);
bCompleted = true;
img = new IplImage(0);
}

private void ShowBt_Click(object sender, EventArgs e)
{
try
{
CaptureFromCamera(0, ref img);
//if (CaptureFromCamera(0, ref img) < 0)
// return;
int width = img.width;
int height = img.height;
pb.Size = new Size(width, height);
this.ClientSize = new Size(panel1.Width + panel2.Width, panel2.Height);
bContinue = true;
bCompleted = false;
bw.RunWorkerAsync();
ShowBt.Enabled = false;
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}

private void Processing(object sender,DoWorkEventArgs e)
{
try
{
if (CaptureFromCamera(0, ref img) < 0)
return;
this.Cursor = Cursors.WaitCursor;

Byte[] imageData = new Byte[img.imageSize];

while (bContinue)
{
QueryFrame(imageData);
bmp = new Bitmap(img.width, img.height, PixelFormat.Format24bppRgb);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr ptr = bmpData.Scan0;
Marshal.Copy(imageData, 0, ptr, img.imageSize);
bmp.UnlockBits(bmpData);
pb.Image = bmp;
bmp.Dispose();
}
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}

private void CompletedProcessing(object sender, RunWorkerCompletedEventArgs e)
{
bCompleted = true;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
bContinue = false;
while (bCompleted != true)
Application.DoEvents();
}

private void QuitBt_Click(object sender, EventArgs e)
{
Close();
}
}
}


I don't know how to correct the code to make it work correcty. Could anyone help me to fix it? Thank you very much for reading my problem and help me. Have a nice weekend, all!



asked 2 mins ago







Cannot capture webcam using dll created by C++ in a C# application

Aucun commentaire:

Enregistrer un commentaire