2017-10-10 18 views
0

私はCamera2Videoの例に従っており、また、画面上のプレビューのSurfaceTextureに属するSurface上で何らかの画像処理を行うためにいくつかのJNIコードを実装しました。これは正常に動作しますが、録音ボタンを押すと、CameraCaptureSession.StateCallback()からonConfigureFailed()を取得すると記録に失敗します。Android Camera2 JNI画像処理 - サーフェスがJNIからロック解除されていませんか?

私の主なJNI関数はここにある -

extern "C" 
jstring 
Java_com_example_android_camera2video_Camera2VideoFragment_someImageProcessingJNI(
     JNIEnv *env, 
     jobject, /* this */ 
     jint srcWidth, 
     jint srcHeight, 
     jobject y_srcBuffer, 
     jobject u_srcBuffer, 
     jobject v_srcBuffer, 
     jobject dstSurface) { 

    // Get pointers to the 3 planes (YUV) of the source (Image from ImageReader). 

    uint8_t *srcLumaPtr = reinterpret_cast<uint8_t *>(env->GetDirectBufferAddress(y_srcBuffer)); 
    if (srcLumaPtr == NULL) { 
     return NULL; 
    } 

    uint8_t *srcUPtr = reinterpret_cast<uint8_t *>(env->GetDirectBufferAddress(u_srcBuffer)); 
    if (srcUPtr == NULL) { 
     return NULL; 
    } 

    uint8_t *srcVPtr = reinterpret_cast<uint8_t *>(env->GetDirectBufferAddress(v_srcBuffer)); 
    if (srcVPtr == NULL) { 
     return NULL; 
    } 

    // Destination setup. 
    // YUV - NV21 

    int dstWidth; 
    int dstHeight; 

    // Get a ptr to the destination window - don't forget to release it. 
    ANativeWindow *dstWin = ANativeWindow_fromSurface(env, dstSurface); 

    // Acquire a reference on the given ANativeWindow object. This prevents the object from being 
    // deleted until the reference is removed. 
    ANativeWindow_acquire(dstWin); 

    // Swapping due to the rotation. 
    dstWidth = srcHeight; 
    dstHeight = srcWidth; 

    // Change the format and size of the window buffers. 
    int32_t setBufferGeometryRet = ANativeWindow_setBuffersGeometry(dstWin, dstWidth, dstHeight, 
                    0 /*format unchanged*/); 

    // Struct that represents a windows buffer. 
    ANativeWindow_Buffer dstBuf; 

    // Lock the window's next drawing surface for writing. 
    if (int32_t err = ANativeWindow_lock(dstWin, &dstBuf, NULL)) { 
     ANativeWindow_release(dstWin); 
     return NULL; 
    } 

    // uint8_t* dstLumaPtr = reinterpret_cast<uint8_t*>(dstBuf.bits); 
    // If we want to write as 32-bit. 
    uint32_t *dstLumaPtr = reinterpret_cast<uint32_t *>(dstBuf.bits); 
    uint32_t *dstLumaPtr_orig = dstLumaPtr; 

    // Buffer 
    int32_t ANativeWindow_Buffer_Width = dstBuf.width; 
    int32_t ANativeWindow_Buffer_Height = dstBuf.height; 
    int32_t ANativeWindow_Buffer_Stride = dstBuf.stride; 

    // Window 
    int32_t dstNativeWinWidth = ANativeWindow_getWidth(dstWin); 
    int32_t dstNativeWinHeight = ANativeWindow_getHeight(dstWin); 
    int32_t dstNativeWinFormat = ANativeWindow_getFormat(dstWin); 

    // Read from the YUV source which needs the 90 deg clockwise rotation. 
    for (int srcCol = 0; srcCol < srcWidth; srcCol++) { 
     for (int srcRow = srcHeight - 1; srcRow >= 0; srcRow--) { 

      // packRGBA() just converts YUV to RGB. 
      *dstLumaPtr = packRGBA(srcLumaPtr[srcRow * srcWidth + srcCol], 
            srcUPtr[((srcRow/2) * srcWidth) + (srcCol - (srcCol % 2))], 
            srcVPtr[((srcRow/2) * srcWidth) + (srcCol - (srcCol % 2))]); 
      dstLumaPtr++; 

      // We cannot simple write to destination pixels sequentially because of the 
      // stride. Stride is the actual memory buffer width, while the image width is only 
      // the wdith of valid pixels. 
      // If we reach the end of a source row, we need to advance our destination 
      // pointer to skip the padding cells. 
      if (srcRow == 0) 
       dstLumaPtr += (ANativeWindow_Buffer_Stride - ANativeWindow_Buffer_Width); 

     } 
    } 

    // ---------------------------------------------------------------------- 
    // Some image processing done here... 
    // -------------------------------------------------------------------- 

    ANativeWindow_unlockAndPost(dstWin); 
    ANativeWindow_release(dstWin); 

    return env->NewStringUTF("Return from JNI"); 
} 

今私は、JNI関数に録音ボタンの動作を呼び出しを削除する場合。これは、私がこのJNIコード内でSurfaceを適切に解放しているとは信じさせていますが、これを解決する方法がわかりません。

事前にアドバイスをいただき、ありがとうございます。


編集 - ログに次のメッセージが表示されます。

10-11 10:15:41.749 8736から8736/com.example.android.camera2video E/BufferQueueProducer:[SurfaceTexture-0-8736-0]接続:既に接続(CUR = 2 REQ = 4)

答えて

0

さて、プレビュー画面がまだ使用されていることがわかりました。これは、onStartRecording()内のJava側で別のプレビューサーフェス変数の追加を取り除いて修正しました。

関連する問題