public class MainActivity extends AppCompatActivity {
private ProcessWithQueue mprocessWithQueue;
private LinkedBlockingQueue<byte []> mframeQueue;
private NDKUtils ndk = new NDKUtils();
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mframeQueue = new LinkedBlockingQueue<>();
mprocessWithQueue = new ProcessWithQueue(mframeQueue);
...
}
public class ProcessWithQueue extends Thread {
private LinkedBlockingQueue<byte[]> mQueue;
public ProcessWithQueue(LinkedBlockingQueue<byte[]> frameQueue) {
mQueue = frameQueue;
start();
}
@Override
public void run() {
while (true) {
byte[] frameData = null;
try {
frameData = mQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(frameData != null){
processFrame(frameData);
}
}
}
private void processFrame(byte[] tmpBytes) {
//call JNI...
//...
int[] res = ndk.handleImg(pix,w,h)
//...
}
}
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
...
//int [] res = ndk.handleImg(pix,w,h)
...
}
}
}
...... JNI:
JNIEXPORT jintArray JNICALL Java_com_magicing_eigenndk_NDKUtils_handleImg(
JNIEnv *env, jclass obj, jintArray buf, int w, int h) {
jint *cbuf;
cbuf = env->GetIntArrayElements(buf, JNI_FALSE );
if (cbuf == NULL) {
return 0;
}
Mat srcMat(h, w, CV_8UC4, (unsigned char *) cbuf);
Mat dstMat1;
.....
jintArray rst = env->NewIntArray(rst_size+2);
env->SetIntArrayRegion(rst,0,rst_size+2,temp);
env->ReleaseIntArrayElements(buf, cbuf, 0);
return rst;
}
我创建了一个新线程来处理来自camera2
的预览图像,而handle函数来自JNI。在致电JNI时,它崩溃了。
如你所见,如果我从主线程调用它运行良好。但这会使页面不流畅。