str14821
str14821

Reputation: 243

Need an advice: Processing color image from camera; Android; OpenCV; NDK; C++

I want to make some preprocessing on preview of an image that was captured from the android device camera.

I can describe the carcase of my application in this way:

1.) Java part.

    // Getting preview from camera.
    public void onPreviewFrame(byte[] arg0, Camera arg1) {
    if (imageFormat == ImageFormat.NV21) {
            frameData = arg0; // private byte[] frameData = null;
        }
    }
    // ...
    // After some code - call native function.
    ImageProcessing(width, height, frameData, output); // private int[] output= null;
    // Setting output to bitmap, etc...
    MyCameraPreview.setImageBitmap(bitmap); // Dislplay, etc...

2.) C++ part. ImageProcessing

extern "C"
jboolean huge_prefix_ImageProcessing(
            JNIEnv* env,
            jobject thiz,
            jint width, 
            jint height,
            jbyteArray frameData,
            jintArray output)
    {
        jbyte* pFrameData = env->GetByteArrayElements(frameData, 0);
        jint* pOutput = env->GetIntArrayElements(output, 0);

        Mat gray(height, width, CV_8UC1, (unsigned char *)pFrameData);
        // Some processing and writing gray to result.
        // ...
        return true;
    }

Everything works perfectly for grayscale images. But now I need to perform processing of RGB image. Can somebody give me an advice on doing it in a right way? I've made ​​several attempts:

Upvotes: 0

Views: 1517

Answers (1)

don_q
don_q

Reputation: 415

Have you tried something like this ? ( called from your JNI wrapper )

void convertYUV( int width, int height, jbyteArray yuvArray ) {

     // Get the data from JEnv. 
    signed char *data = JNIEnvInfo::getInstance()->getJNIEnv()->GetByteArrayElements(yuvArray, 0);

     // Convert to Mat object.
     Mat imgbuf(Size(width,height), CV_8UC4, (unsigned char*) data);
     Mat img = imdecode(imgbuf, CV_LOAD_IMAGE_COLOR);

    //
    // Release the JNI data pointer.
    JNIEnvInfo::getInstance()->getJNIEnv()->ReleaseByteArrayElements(yuvArray, (jbyte*) yuvArray, 0);

    // ... do stuff with the Mat ..

}



Mat convertRGB(int width , int height , jintArray rgb8888)
{

    //
    int *rgb;
    int i;

    //
    // Get the data from JEnv. 
    int *data = JNIEnvInfo::getInstance()->getJNIEnv()->GetIntArrayElements(rgb8888, 0);

   //
   // Copy the data.
    for(i = 0; i < width * height; i++ ) {
        rgb[i] = data[i];
   }

   //
   // Convert to mat object.
   Mat imgbuf(Size(width,height), CV_8UC3, rgb);
   Mat img = imdecode(imgbuf, CV_LOAD_IMAGE_COLOR);

   //
   // Release the JNI data pointer.
   JNIEnvInfo::getInstance()->getJNIEnv()->ReleaseIntArrayElements(rgb8888, (jint*) rgb8888, 0);

   return img;
}

Upvotes: 2

Related Questions