Hi all. I am trying to simply wrap the facedetect C code to Java. I actually succeed in running the C code alone and build the dll file successfully. But when I am wrapping the C code to Java, The EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00009548, pid=2276, tid=2564 error just occurs. I debugged the program line by line, it suggested that cvLoadImage and cvHaarDetectObjects cannot be accepted by the JVM. Could anyone tell what's wrong with the original C code? How can I modify it? Is it because of the null pointer?
Here is my C code.
// OpenCV Sample Application: facedetect.c
// Include header files
#include <jni.h>
#include <stdio.h>
#include "faceinter.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
// Main function, defines the entry point for the program.
JNIEXPORT void JNICALL Java_lireface_Main_test
(JNIEnv *env, jobject obj)
{
CvMemStorage* storage = 0;
CvHaarClassifierCascade* cascade = 0;
CvSeq* faces;
// Structure for getting video from camera or avi
CvCapture* capture = 0;
const char* cascade_name ;
// Input file name for avi or image file.
// const char* input_name;
cascade_name =
"e:/OpenCV/data/haarcascades/haarcascade_frontalface_alt.xml";
// input_name = argc > 2 ? argv[2] : 0;
// Load the HaarClassifierCascade
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
// Check whether the cascade has loaded successfully. Else report and error and quit
// Allocate the memory storage
storage = cvCreateMemStorage(0);
char* a="e:/lena.jpg";
// Create a new named window with title: result
cvNamedWindow( "result", 1 );
// Assume the image to be lena.jpg, or the input_name specified
const char* filename = a;
// Load the image from that filename
IplImage* image = cvLoadImage(filename, 1);
// IplImage* image1 = cvCreateImage( cvSize(image->width, image->height), 8, 3 );
// image1->imageData=image->imageData;
// If Image is loaded succesfully, then:
if( image ) {
int scale = 1;
CvRect rect;
// Create a new image based on the input image
// IplImage* temp = cvCreateImage( cvSize(image->width/scale,image->height/scale), 8, 3 );
// Create two points to represent the face locations
CvPoint pt1, pt2;
int i;
// Clear the memory storage which was used before
cvClearMemStorage( storage );
// Find whether the cascade is loaded, to find the faces. If yes, then:
if( cascade ) {
// There can be more than one face in an image. So create a growable sequence of faces.
// Detect the objects and store them in the sequence
faces = cvHaarDetectObjects( image, cascade, storage,
1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
cvSize(40, 40) );
// Loop the number of faces found.
for( i = 0; i < (faces ? faces->total : 0); i++ ) {
// Create a new rectangle for drawing the face
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
// Find the dimensions of the face,and scale it if necessary
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
// Draw the rectangle in the input image
cvRectangle( image, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0 );
rect = r[0];
}
}
// cvResetImageROI(img);
// vRect cvGetImageROI(img);
// if(!rect)
// printf("alibaba \n");
// Show the image in the window named "result"
// cvShowImage( "result", image );
// Release the temp image created.
// cvReleaseImage( &temp );
// cvSaveImage("e:/NetBeansProjects/Lire_2901/alibaba.jpg", image);
// cvSetImageROI(image1, rect);
// cvSaveImage("e:/NetBeansProjects/Lire_2901/alimama.jpg", image1);
// IplImage* temp = cvCreateImage( cvSize(image->width/scale, image->height/scale), image->depth, image->nChannels );
cvShowImage("result", image);
// Wait for user input
cvWaitKey(0);
// Release the image memory
cvReleaseImage( &image );
cvDestroyWindow("result");
// cvReleaseImage( &temp );
// cvSetImageROI(image1, rect);
}
int x=faces->total;
if(x!=0){
return 1;
}else{
return 0;
}
}
And the Java code is also listed here:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lireface;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.awt.image.WritableRaster;
public class Main {
static{
System.load("e:\\NetBeansProjects\\face\\dist\\face.dll");
}
public Main() {
}
public static void main(String[] args) throws IOException {
new Main().test();
}
public static native void test();
}
I am looking forward to your help. Thanks.