Ask Your Question

Passing Bitmap to JNI

asked 2012-11-23 01:43:19 -0600

Haris gravatar image

updated 2012-11-23 01:47:06 -0600

Andrey Pavlenko gravatar image

Hi all How can I pass bitmap image to JNI part for some image manipulation. Is it possible, or do I need to convert to byte array.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-11-23 01:50:20 -0600

Andrey Pavlenko gravatar image

Look at the <NDK dir>/samples/bitmap-plasma sample.

edit flag offensive delete link more


Hi thanks for replay. I checked the sample, I think that code directly using the bitmap. I need to use my bitmap as Mat for doing some image processing algorithms.

Haris gravatar imageHaris ( 2012-11-23 04:09:33 -0600 )edit

Hi I just convert my bitmap to byte array and passed to the JNI but getting distorted image at the JNI code. My code look like

Haris gravatar imageHaris ( 2012-11-23 23:22:36 -0600 )edit

answered 2013-04-30 12:45:44 -0600

riothamus gravatar image

I've solved this by ensuring a buffered image is of type BufferedImage.TYPE_3BYTE_BGR), converting to a byte array, and then copying into the aligned memory in JNI. Code samples below.

Scala part

(Conversion to java should be easy)

// converts an image to BufferedImage.TYPE_3BYTE_BGR
def convertType(img: BufferedImage): BufferedImage = {
    val convertedImg = new BufferedImage(img.getWidth, img.getHeight, BufferedImage.TYPE_3BYTE_BGR)
    convertedImg.getGraphics.drawImage(img, 0, 0, null)

JNI Part

IplImage* rgbJbyteToIpl(const int width, const int height, const jbyte* const img) {
    int j, channels = 3;
    IplImage * rv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
    // IplImage data is aligned for SIMD instructions.
    // Copy each row individually.
    for (j = 0; j < height; j++) {
        memcpy((void*) &(rv->imageData[(rv->widthStep) * j]), (void*) &(img[j * width * channels]), width * channels);
    return 0;
edit flag offensive delete link more


in you JNI part, i didn't understand why you're returning 0? Thank you very much

ahmedkhanfir gravatar imageahmedkhanfir ( 2014-12-08 12:25:31 -0600 )edit

Question Tools


Asked: 2012-11-23 01:43:19 -0600

Seen: 3,289 times

Last updated: Apr 30 '13