Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: how to Update a SurfaceView from the ndk by updating the Surface buffer?

I am working on an image processing project. I have a SurfaceView where I want to show "images" form the jni side.

I followed this blog , the NDK ANativeWindow API instructions to get the pointer of the buffer and updated from the C side.

I got the code to run but my SurfaceView is not updating (not showing any image...). Also the callback surfaceChanged is not called when the buffer is updated.

Here is what I am doing:

JNI SIDE :

/*
  * Class:     com_example_myLib
  * Method:    renderImage
  * Signature: (JI)V
  */

JNIEXPORT void JNICALL com_example_myLib_renderImage
  (JNIEnv *mJNIEnv, jobject mjobject, jobject javaSurface) {

#ifdef DEBUG_I
    LOGI("renderImage attempt !");
#endif

// load an ipl image. code tested and works well with ImageView.
IplImage *iplimage = loadMyIplImage();

int iplImageWidth = iplimage->width;
int iplImageHeitgh = iplimage->height;

char * javalBmpPointer = malloc(iplimage->width * iplimage->height * 4);
int _javaBmpRowBytes = iplimage->width * 4;



 // code tested and works well with ImageView.
    copyIplImageToARGBPointer(iplimage, javalBmpPointer, _javaBmpRowBytes,
            iplimage->width, iplimage->height);
#ifdef DEBUG_I
    LOGI("ANativeWindow_fromSurface");
 #endif
ANativeWindow* window = ANativeWindow_fromSurface(env, javaSurface);

#ifdef DEBUG_I
    LOGI("Got window %p", window);
 #endif

 if(window != 0 ){
    ANativeWindow_Buffer buffer;
    if (ANativeWindow_lock(window, &buffer, NULL) == 0) {
        #ifdef DEBUG_I
            LOGI("GANativeWindow_lock %p", buffer);
         #endif
        memcpy(buffer.bits, javalBmpPointer,  iplimage->width* iplimage->height* 4); // ARGB_8888
        ANativeWindow_unlockAndPost(window);
    }

    ANativeWindow_release(window);

 }

}

java SIDE :

// every time that I want to reload the image:
   renderImage(mySurfaceView.getHolder().getSurface());

Thanks for your time and help!

like image 811
ahmed_khan_89 Avatar asked Oct 19 '25 14:10

ahmed_khan_89


1 Answers

One of the most common problems leading to a blank SurfaceView is setting a background for the View. The View contents are intended to be a transparent "hole", used only for layout. The Surface contents are on a separate layer, below the View layer, so they are only visible if the View remains transparent.

The View background should generally be disabled, and nothing drawn on the View, unless you want some sort of "mask" effect (like rounded corners).

like image 130
fadden Avatar answered Oct 22 '25 03:10

fadden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!