Inside my "public void onPreviewFrame(byte[] data, Camera camera) { .... }" I want to take the bytearray "data" and pass it to the JNI and apply some OpenCV filters so that the preview changes, without returning it. What is the best way to do this?
Currently I only pass the bytearray like this:
JNIEXPORT jint JNICALL Java_example_jnitest_Lib_filterfunc
(JNIEnv * je, jclass jc, jbyteArray byteData){
try {
jbyte* _b_data= je->GetByteArrayElements(byteData, 0);
int height = base;
int width = base2;
Mat mdata(height, width, CV_8UC4, (unsigned char *)_b_data);
Mat myMat = imdecode(mdata,1);
je->ReleaseByteArrayElements(byteData, _b_data, 0);
return 1;
} catch(const exception& ex){
return 0;
}
}
In the Java Code:
public static native int filterfunc(byte[] byteData);
Note: Currently the return Value is one so I hope turning the bytearray into a Mat Obejct is working. But this won't change the Preview because I don't change it back to an bytearray.
Unfortunately that's not possible. The byte array that is passed to
onPreviewFrame()
is just a copy of the preview frame, and any changes that you make to it will not be shown in the preview. You can test this for yourself by modifying the byte array in Java inside theonPreviewFrame()
function as a test, you won't see any effect.If you want to change the preview frame data using OpenCV and see the results in a preview window then you will need to upload the processed frame to an OpenGL texture and then render it to a GLSurfaceView, using a fragment shader to convert the NV21 data to RGB, or some other approach. Simply changing the byte array won't work.
See these questions for more information:
PreviewCallback onPreviewFrame does not change data
onPreviewFrame doesn't change the data