Ask Your Question
0

Convert C++ IplImage.ImageData to jbyteArray

asked 2013-09-18 13:06:46 -0600

Exspecto gravatar image

updated 2013-09-18 13:09:13 -0600

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;

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-09-18 13:13:29 -0600

berak gravatar image

i think, your len is wrong:

len = sizeof(rgb_img->imageData);

since rgb_img->imageData is a pointer to uchar, sizeof(rgb_img->imageData) will give 4 on a 32 bit system, and 8 on 64 bit one. definitely not, what you wanted, right ?

the desired len is probably:

width * height * nChannels

here

edit flag offensive delete link more

Comments

Thanks for pointing that out. Would "rgb_img->imageSize" be the better option? Documentation states :

imageSize: Image data size in bytes. For interleaved data, this equals image->height * image->widthStep

E

Exspecto gravatar imageExspecto ( 2013-09-18 13:49:39 -0600 )edit

sounds good, too !

berak gravatar imageberak ( 2013-09-18 13:54:56 -0600 )edit

The fix suggested resolves the initial problem. Now I wonder if the converted IplImage->imageData to a jbyteArray is something that Java code can correctly convert into an image. I performed the following on the converted jbyteArray to display the image into an applet:

//NOTE buffImg is of class BufferedImage

// Get byte byte[] image = null; usb.maple_capture_image(ImageType.RGB.getValue(), image);

if (image != null){ // Received byte array from camera. Let's display in applet. We need to convert byte array ByteArrayInputStream in = new ByteArrayInputStream(image); buffImg = ImageIO.read(in);

if (buffImg != null) {
 graph = buffImg.createGraphics();
 repaint();
}
else{
    System.out.println("Buffer is NULL");
}

}

Exspecto gravatar imageExspecto ( 2013-09-18 14:15:50 -0600 )edit
BufferedImage buffImg = new BufferedImage(width,height, BufferedImage.TYPE_3BYTE_BGR); // type right ?
image.getRaster().setDataElements(0, 0, width,height, image );
berak gravatar imageberak ( 2013-09-18 14:21:09 -0600 )edit

Berak,

This worked like a charm!

Thank your for your help!

Exspecto gravatar imageExspecto ( 2013-09-18 14:36:20 -0600 )edit

pleasure !

berak gravatar imageberak ( 2013-09-18 14:38:29 -0600 )edit

Question Tools

Stats

Asked: 2013-09-18 13:06:46 -0600

Seen: 2,170 times

Last updated: Sep 18 '13