6

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,

2
  • 1
    why do you want so, you are trying to convert byte[] to unsigned char array, its range is within 0-255 Commented Nov 22, 2013 at 5:13
  • @Arju I (have) to convert it because most of the time, we cannot just use Java data types directly in C. For example, we have to convert java.lang.String to char * before we can effectively use it in C. Am I missing something? Commented Nov 22, 2013 at 19:32

1 Answer 1

7

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);
}
Sign up to request clarification or add additional context in comments.

8 Comments

could you please give me an example to make it more clear for me? Should I do the casting inside onPictureTaken()? If yes, how to do it while uint8_t* is not supported in Java. Thanks
@zharf I found your answer here: stackoverflow.com/a/16668081/3020258 which is related to my question except that I have a C code and yours is C++. Could you please help me what are the changes that I should make to make it suitable for my code?
Oh, you need complete code for the wrapper… See the updated answer as soon as I find a full-sized keyboard to type it.
@user3020258: I have updated the answer with complete code for the wrapper.
Even I didn't exactly follow your solution,I accepted it since it was an excellent start point and hint for me. Thank you for ur help and fast response! I only used the third code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.