Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Convert C++ IplImage.ImageData to jbyteArray

Hello,

I using a c++ dll provided by a customer that connects to a USB-video camera. The image captured by the camera uses OpenCV contructs such as IplImage. Since I need to create a Java applet to display the image provide by this .dll, I've create a JNI file that implements equivalent methods to the C++ code (see below). The problem with my C++ implementation is that I can't convert IplImage->imageData to a jbyteArray (it's always "null"). I'm not sure if I'm going about it the right way. I've used the JavaCV project but it's an overkill for my simple Java Applet. I would prefer to convert IplImage->imageData to some native Java structure instead. I can't figure out what is wrong with my C++ implementation. Suggestions?

MY JAVA MapleJNI Class:

public class MapleJNI {

 static{ 
     System.loadLibrary("MapleJNI.dll");

 } 

 public static native byte maple_connect(); 
 public static native void maple_disconnect(); 
 public static native byte maple_set_capture_mode(byte mode); 
 public static native byte maple_capture_image(byte image_type, byte[] img_buf); 
 public static native void maple_crop_image(byte[] img_buf); 
 [ ... ]

C++ - Code ".cpp" File (JNI Implementation)

JNIEXPORT jbyte JNICALL Java_maple_MapleJNI_maple_1capture_1image (JNIEnv *env, jclass, jbyte mode, jbyteArray image){

    UCHAR result = maple_capture_image((UCHAR) mode, (UCHAR *) rgb_img->imageData);

    jsize len = 0;

    if (result == STATUS_NO_ERROR)
    {
        // Crop top/bot rows of image
        // (set to black) to avoid jitter
        maple_crop_image((UCHAR *) rgb_img->imageData);

        len = sizeof(rgb_img->imageData);

        image = env->NewByteArray(len);
                    // Copy "rgb_image" to java native jbyteArray "image"
        env->SetByteArrayRegion(image, 0, len, (jbyte*) rgb_img->imageData);
    }
    else{
        maple_disconnect();
    }

    return result;

}

Convert C++ IplImage.ImageData to jbyteArray

Hello,

I using a c++ dll provided by a customer that connects to a USB-video camera. The image captured by the camera uses OpenCV contructs such as IplImage. Since I need to create a Java applet to display the image provide by this .dll, I've create a JNI file that implements equivalent methods to the C++ code (see below). The problem with my C++ implementation is that I can't convert IplImage->imageData to a jbyteArray (it's always "null"). I'm not sure if I'm going about it the right way. I've used the JavaCV project but it's an overkill for my simple Java Applet. I would prefer to convert IplImage->imageData to some native Java structure instead. I can't figure out what is wrong with my C++ implementation. Suggestions?

MY JAVA MapleJNI Class:

public class MapleJNI {

 static{ 
     System.loadLibrary("MapleJNI.dll");

 } 

 public static native byte maple_connect(); 
 public static native void maple_disconnect(); 
 public static native byte maple_set_capture_mode(byte mode); 
 public static native byte maple_capture_image(byte image_type, byte[] img_buf); 
 public static native void maple_crop_image(byte[] img_buf); 
 [ ... ]

C++ - Code ".cpp" File (JNI Implementation)

JNIEXPORT jbyte JNICALL Java_maple_MapleJNI_maple_1capture_1image (JNIEnv *env, jclass, jbyte mode, jbyteArray image){

    UCHAR result = maple_capture_image((UCHAR) mode, (UCHAR *) rgb_img->imageData);
            // NOTE:  rgb_img is a global variable (for now) and I've proven that is
            //        does contain the captured image from the video camera

    jsize len = 0;

    if (result == STATUS_NO_ERROR)
    {
        // Crop top/bot rows of image
        // (set to black) to avoid jitter
        maple_crop_image((UCHAR *) rgb_img->imageData);

        len = sizeof(rgb_img->imageData);

        image = env->NewByteArray(len);
                    // Copy "rgb_image" to java native jbyteArray "image"
        env->SetByteArrayRegion(image, 0, len, (jbyte*) rgb_img->imageData);
    }
    else{
        maple_disconnect();
    }

    return result;

}