简体   繁体   English

如何将Android中的byte []转换为C中的uint8_T数组?

[英]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. 我打算通过拍照手机(Android)拍照,然后通过JNI将其传递给C功能。 The C function is generated by MATLAB Coder. C函数由MATLAB Coder生成。

Here is the header of the generated C function: 这是生成的C函数的标题:

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? 问题:如何将byte []转换为uint8_T数组? I found how to convert byte[] to jbyte * .. but I don't know how to deal with uint8_T? 我发现如何将byte []转换为jbyte * ..但我不知道如何处理uint8_T?

I know only Java but not C. 我只知道Java但不知道C.

Regards, 问候,

Java does not have unsigned integer types, but the camera does not really care. Java没有无符号整数类型,但相机并不真正关心。 You can safely cast the byte array that arrives from onPictureTaken() callback to uint8_t* . 您可以安全地将从onPictureTaken()回调到达的字节数组onPictureTaken()uint8_t*

Sidenote: most likely, the picture will arrive as JPEG stream. 旁注:很可能,图片将以JPEG流形式到达。

Update: Example of implementing onPictureTaken() in C . 更新:C中实现onPictureTaken()示例。

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 : 这是文件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 : 这是C文件,它是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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM