Search code examples
androidc++opencvjava-native-interfaceiplimage

Set IplImage from object Bitmap and get result from IplImage to be a jIntArray


I am working on android / JNI / Opencv. I need to read a jObject set to be a Bitmap, send it to be an IplImage * inside a function then the output will be converted back to jintArray. This is my code:

    JNIEXPORT void JNICALL Java_com_cs_here_there_nProcessOnBitmap(JNIEnv* env,
jobject thiz, jint width, jint height, jbyteArray jb, jintArray rgbaData, jobject dinfo, jobject srcimg)
{
// convert img
    AndroidBitmapInfo bInfo;
    char *bPixs;
    int bRet;
    if ((bRet = AndroidBitmap_getInfo(env, srcimg, &bInfo)) < 0) {
        dmz_debug_log("AndroidBitmap_getInfo failed(src)! error = %d", bRet);
        return;
    }else{
        dmz_debug_log("bRet = %d", bRet);
    }
    if (bInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
        dmz_debug_log("Bitmap(src) format is not RGBA_8888!");
        return;
    }else{
        dmz_debug_log("ANDROID_BITMAP_FORMAT = %i", bInfo.format);
    }
    if ((bRet = AndroidBitmap_lockPixels(env, srcimg, (void**)&bPixs)) < 0) {
        dmz_debug_log("AndroidBitmap_lockPixels() failed(src)! error = %d", bRet);
        return;
    }
    IplImage* bimg = cvCreateImage(cvSize(bInfo.width,bInfo.height), IPL_DEPTH_8U, 4);
    memcpy(bimg->imageData, bPixs, bimg->imageSize);
    AndroidBitmap_unlockPixels(env, srcimg);
//Resize channels
IplImage* img = cvCreateImage(cvSize(bInfo.width,bInfo.height), IPL_DEPTH_8U, 3);
cvCvtColor(bimg, img, CV_RGBA2BGR);
IplImage * pImage = ImageProcessor ( img );
int len = pImage->width * pImage->height;
env->SetIntArrayRegion(rgbaData,0,len,(jint*) pImage->imageData);
cvReleaseImage(&pImage);
dmz_debug_log("[nProcessOnBitmap()] END");
}

I am trying to convert srcimg to plImage* bimg, and then to resize this image to be a 3 channels (img). Then to send this img to ImageProcessor(). The output should be then converted to jintArray to be read by Java.

Bitmap bmp_4 = Bitmap.createBitmap(detectedBitmap_width, detectedBitmap_height, Bitmap.Config.ARGB_8888);

Sadly, I am not getting the expected result. Do you have any idea? Please help. In advance thanks.


Solution

  • I solved it by reviewing the JPG file creation in the Java file. the JPG creation also I used is based on ARGB so I needed to send an ARGB format to the Java code. Good Luck.