JAVA 和 native 共享内存

作者: NiceDream | 来源:发表于2017-06-02 15:34 被阅读53次

    java层

    private ByteBuffer _recBuffer;
     
     try{
        _recBuffer = ByteBuffer.allocateDirect(204800); // Max 10 ms @ 48
        } catch(Exception e) {
          Log.d("UVCCamera","can not get _capBuffer");
        }
    

    native 层

    jclass cls = env->GetObjectClass(thiz);
        if(cls == NULL){
            LOGE("cls == NULL");
        } else {
    
        }
        jfieldID fidRecBuffer = env->GetFieldID( cls, "_recBuffer",  "Ljava/nio/ByteBuffer;");
        if(fidRecBuffer == NULL){
            LOGE("fidRecBuffer == NULL");
        } else {
    
        }
        jobject javaRecBufferLocal =  env->GetObjectField(thiz, fidRecBuffer);
        if(javaPlayBufferLocal == NULL){
            LOGE("javaPlayBufferLocal == NULL");
        } else {
    
        }
        _javaRecBuffer = env->NewGlobalRef(javaRecBufferLocal);
        if(_javaRecBuffer == NULL){
            LOGE("_javaRecBuffer == NULL");
        } else {
            _javaDirectRecBuffer =env->GetDirectBufferAddress(_javaRecBuffer);
        }
    
    
    
    void* _javaDirectRecBuffer; // Direct buffer pointer to rec buffer
    jobject _javaRecBuffer;
    

    可以对_javaDirectRecBuffer进行操作,再java 层会同时更新数据

    相关文章

      网友评论

        本文标题:JAVA 和 native 共享内存

        本文链接:https://www.haomeiwen.com/subject/jbjufxtx.html