Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert byte[] in Android to uint8_T array in C?

I'm planning to take a picture by camera phone (Android) then pass it to C function through JNI. The C function is generated by MATLAB Coder.

Here is the header of the generated C function:

real_T detection(const **uint8_T** OriginalImage[28755648])

Here is the data type of the image:

 @Override
    public void onPictureTaken(**byte[] data**, Camera camera) {.....}

Question: How to convert byte[] to uint8_T array? I found how to convert byte[] to jbyte *.. but I don't know how to deal with uint8_T?

I know only Java but not C.

Regards,

like image 717
user3020258 Avatar asked Nov 22 '13 04:11

user3020258


1 Answers

Java does not have unsigned integer types, but the camera does not really care. You can safely cast the byte array that arrives from onPictureTaken() callback to uint8_t*.

Sidenote: most likely, the picture will arrive as JPEG stream.

Update: Example of implementing onPictureTaken() in C.

Here is what you have somewhere in your activity:

mCamera = Camera.open();
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
...
mCamera.takePicture(null, null, new android.hardware.Camera.NativePictureCallback);

Here is the file src/android/hardware/Camera/NativePictureCallback.java:

package android.hardware.Camera;
class NativePictureCallback: implements PictureCallback {
  static { 
    System.loadLibrary("NativeCamera"); 
  } 
  public void native onPictureTaken(byte[] data, Camera camera);
}

And here is the C file that is part of libNativeCamera.so:

include <jni.h>
include <tmwtypes.h>

real_T detection(const uint8_T* OriginalImage);

JNIEXPORT void JNICALL
Java_android_hardware_Camera_NativePictureCallback_onPictureTaken(
    JNIEnv* env, jobject thiz, jbytearray data, jobject camera) {
  jbyte* dataPtr = (*env)->GetByteArrayElements(env, data, NULL);
  real_T res = detection((const uint8_T*)dataPtr);
  (*env)->ReleaseByteArrayElements(env, data, dataPtr, JNI_ABORT);
}
like image 130
Alex Cohn Avatar answered Sep 19 '22 04:09

Alex Cohn