Android OpenCV使用内存地址而不是GetByteArrayElements

时间:2019-04-28 14:37:59

标签: android performance opencv java-native-interface opencv4android

我正在开发一个使用OpenCV的Android应用。 在最新的4.x OpenCV Android示例中,本机c ++函数从Java接收Mat内存地址。

问题是,在我的情况下,我只有在byte []中拥有框架,并且在Java端将其从frame byte []转换为Mat,从而增加了很多处理时间。

然后,我想传递byte [],获取它的内存地址,并在c ++端转换为Mat。

这就是我今天所拥有的:

            public void process(Frame frame) {

                byte[] data = frame.getData();
                int rotation = frame.getRotation();
                long time = frame.getTime();
                Size size = frame.getSize();
                int format = frame.getFormat();

                int[] bgra = mBGRA;

                ProcessFrameGBA(size.getWidth(), size.getHeight(), data, bgra);

C ++

JNIEXPORT void JNICALL Java_com_smartnsens_opencvapp_MainActivity_ProcessFrame(
        JNIEnv *env,
        jobject /* this */,
        jint width,
        jint height,
        jbyteArray yuv /* raw data */,
        jintArray bgra /* return frame*/) {

    // Get native access to the given Java arrays.
    jbyte *_yuv = env->GetByteArrayElements(yuv, 0);
    jint *_bgra = env->GetIntArrayElements(bgra, 0);

    // Prepare a cv::Mat that points to the YUV420sp data.
    Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv); // Wrapper around the _yuv data.

    // Prepare a cv::Mat that points to the BGRA output data.
    Mat mbgra(height, width, CV_8UC4, (uchar *) _bgra);

    // Convert the color format from the camera's
    // NV21 "YUV420sp" format to an Android BGRA color image.
    cvtColor(myuv, mbgra, CV_YUV420sp2BGRA);

    // Release the native lock we placed on the Java arrays.
    env->ReleaseIntArrayElements(bgra, _bgra, 0);
    env->ReleaseByteArrayElements(yuv, _yuv, 0);
}

我想要这样的东西:

JNIEXPORT void JNICALL Java_com_smartnsens_opencvapp_MainActivity_ProcessFrame(
        JNIEnv *env,
        jobject /* this */,
        jint width,
        jint height,
        jlong yuvAddr /* raw data */,
        jlong bgraAddr /* return frame*/) {

    // (Pseudo code below - I'm not sure if it's correct)
    jbyte *_yuv = *yuv;
    jint *_bgra = *bgra;

    // 
    // and change the code below to have the final frame in the mbgra
    //

    // Prepare a cv::Mat that points to the YUV420sp data.
    Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv); // Wrapper around the _yuv data.

    // Prepare a cv::Mat that points to the BGRA output data.
    Mat mbgra(height, width, CV_8UC4, (uchar *) _bgra);

    // Convert the color format from the camera's
    // NV21 "YUV420sp" format to an Android BGRA color image.
    cvtColor(myuv, mbgra, CV_YUV420sp2BGRA);

}

我该怎么做?

最好的问候。 克莱森里奥斯(Kleyson Rios)。

0 个答案:

没有答案