1

I should rotate every yuv image buffer, which i receive from camera, on 90 degrees counterclockwise. I found this post where using a java. This code works fine. But I've tried to do a native method, because I wanted to do method which has the same logic but works faster.

JNIEXPORT jbyteArray JNICALL Java_com_ndk_example_utils_NativeUtils_rotateFrameBackward
      (JNIEnv *env, jobject obj, jbyteArray arr, jint w, jint h){

      jint arrSize = w*h*3/2;
      jbyte *data,*yuv;

      data = (*env)->GetByteArrayElements(env, arr, JNI_FALSE);

      yuv = (*env)->GetByteArrayElements(env, arr, JNI_FALSE);

      int x,y,i = 0;
      for(x = 0; x < w; x++){
        for(y = h-1;y >= 0;y--){
          yuv[i] = data[y*w+x];
          i++;
        }
      }

      i = arrSize - 1;
      for(x = w-1;x > 0;x=x-2)
      {
        for(y = 0;y < h/2;y++)
        {
          yuv[i] = data[(w*h)+(y*w)+x];
          i--;
          yuv[i] = data[(w*h)+(y*w)+(x-1)];
          i--;
        }
      }
      (*env)->ReleaseByteArrayElements(env, arr, yuv, JNI_ABORT);
      yuv = 0;
      data = 0;
      return arr;
    }

When i launched this method on my htc 816(v5.1) it works fine, but when I launched the app on Samsung S3(v4.3) and Lenovo P-70(v4.4.2), the app is crashes. And in Android monitor tab in Android Studio, i saw that memory usage is always increasing until my app is crashes. In my htc i don't have problems with it. Any ideas?

Community
  • 1
  • 1
AinisSK
  • 306
  • 1
  • 10

1 Answers1

2

You do a double GetByteArrayElements for arr using data and yuv, then only release yuv. You also don't check if a copy was made using the last parameter, you just give in JNI_FALSE. You shouldn't do that, you should use a boolean parameter to receive the value, not try to tell the system whether to copy.

You should therefore release both pointers in the end.

Also if this code works, it means that copies are in fact made since you are reading and writing from the same memory area and that would cause corruption of the image.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74