6

Android 和 OpenCV 的新手。一直在尝试实现新书《Mastering OpenCV with Practical Computer Vision Projects》中的代码。该应用程序基本上使用 OpenCV 在相机预览上渲染卡通化图像。您可以触摸屏幕保存卡通化图像。

作者的源代码位于此处

我对 CartoonifierApp.java 文件做了一个小的修改(见下文),以便我可以使用OpenCV 管理器应用程序静态加载卡通化器库(原始代码引发 UnsatisfiedLinkError)。

我面临的问题是,当我将应用程序加载到我的 Galaxy Nexus (Android 4.1.1) 上时,我得到一个空白的黑屏。我的 LogCat 说:

E/BufferQueue(4744): [unnamed-4744-0] setBufferCount: SurfaceTexture 已被放弃!E/Cartoonifier::SurfaceView(4744): startPreview() 失败

我认为这是一个内存问题。我知道 cpp 代码可以正常工作,因为它在我的计算机上运行良好——尽管在相对较新的笔记本电脑(华硕 U46E)上渲染速度很慢。

我不知道如何解决这个问题。我发现的唯一有用的建议是这里。如果我在 CatoonifierVewBase.java 中替换我的 setPreview 方法

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
     mCamera.setPreviewTexture( new SurfaceTexture(10) );
 else
     mCamera.setPreviewDisplay(null);

 mCamera.setPreviewDisplay(mHolder);

然后会发生什么是相机正常工作,我可以通过触摸屏幕保存卡通化图像。请注意,这并不是我想要的结果,因为我想在相机预览中不断卡通化图像。相机只能工作,因为我没有写到表面视图(至少这是我的理解)。前面提到的网站中的答案有一些解决方法,但我不知道他在说什么。

顺便说一句,我已经尝试了所有示例 OpenCV4Android 应用程序,它们工作得很好。我也在使用 OpenCV 2.4.3 版。API 级别目标是 15。

完整的Logcat:

12-22 15:33:07.966:I/CartoonifierApp(5999):实例化新类 com.Cartoonifier.CartoonifierApp 12-22 15:33:07.966:I/CartoonifierApp(5999):调用 onCreate 12-22 15:33:07.966 : I/CartoonifierApp(5999): 试图加载 OpenCV 库 12-22 15:33:07.982: I/Cartoonifier::SurfaceView(5999): 实例化新类 com.Cartoonifier.CartoonifierView 12-22 15:33:07.990: I /CartoonifierApp(5999): onResume 12-22 15:33:07.990: I/Cartoonifier::SurfaceView(5999): openCamera 12-22 15:33:07.990: I/Cartoonifier::SurfaceView(5999): releaseCamera 12-22 15:33:08.099: D/OpenCVManager/Helper(5999): 服务连接创建 12-22 15:33:08.099: D/OpenCVManager/Helper(5999): 试图获取库路径 12-22 15:33:08.138: D/OpenCVManager/Helper(5999): 试图获取库列表 12-22 15:33:08.169: D/OpenCVManager/Helper(5999): 库列表:“” 12-22 15:33:08.169: D/OpenCVManager/Helper(5999): 第一次尝试加载库 12-22 15:33:08.169: D/OpenCVManager/Helper(5999): 试图初始化 OpenCV 库 12- 22 15:33:08.169: D/OpenCVManager/Helper(5999): 试图加载库 /data/data/org.opencv.engine/lib/libopencv_java.so 12-22 15:33:08.169:

D/dalvikvm(5999):试图加载 lib /data/data/org.opencv.engine/lib/libopencv_java.so 0x41936a40 12-22

15:33:08.193:D/dalvikvm(5999):添加共享库 /data/data/org.opencv.engine/lib/libopencv_java.so 0x41936a40 12-22

15:33:08.193:D/OpenCVManager/Helper(5999):OpenCV 库初始化没问题!

12-22 15:33:08.193:D/OpenCVManager/Helper(5999):首次尝试加载库是可以的 12-22 15:33:08.193:D/OpenCVManager/Helper(5999):初始化完成,状态为 0 12- 22 15:33:08.193:D/OpenCVManager/Helper(5999):与服务解除绑定 12-22 15:33:08.200:D/OpenCVManager/Helper(5999):使用回调调用 12-22 15:33:08.200:I /CartoonifierApp(5999): OpenCV 加载成功

12-22 15:33:08.200: D/dalvikvm(5999): 试图加载 lib /data/data/com.Cartoonifier/lib/libcartoonifier.so 0x41936a40 12-22

15:33:08.200:D/dalvikvm(5999):添加了共享库 /data/data/com.Cartoonifier/lib/libcartoonifier.so 0x41936a40 12-22

15:33:08.200:D/dalvikvm(5999):在 /data/data/com.Cartoonifier/lib/libcartoonifier.so 0x41936a40 中找不到 JNI_OnLoad,跳过初始化 12-22 15:33:08.200:D/OpenCVManager/Helper( 5999): 服务连接已创建 12-22 15:33:08.200: D/OpenCVManager/Helper(5999): 试图获取库路径 12-22

15:33:08.232:D/OpenCVManager/Helper(5999):试图获取库列表 12-22 15:33:08.271:D/OpenCVManager/Helper(5999):库列表:“”

12-22 15:33:08.271: D/OpenCVManager/Helper(5999): 第一次尝试加载库 12-22 15:33:08.271: D/OpenCVManager/Helper(5999): 试图

初始化 OpenCV 库 12-22 15:33:08.271:D/OpenCVManager/Helper(5999):尝试加载库 /data/data/org.opencv.engine/lib/libopencv_java.so 12-22 15:33:08.271: D/dalvikvm(5999):试图加载 lib /data/data/org.opencv.engine/lib/libopencv_java.so 0x41936a40 12-22

15:33:08.271:D/dalvikvm(5999):共享库'/data/data/org.opencv.engine/lib/libopencv_java.so'已经加载到相同的CL 0x41936a40 12-22 15:33:08.271:D/ OpenCVManager/Helper(5999):OpenCV 库初始化没问题!12-22 15:33:08.271: D/OpenCVManager/Helper(5999): 第一次尝试加载库是可以的

12-22 15:33:08.271:D/OpenCVManager/Helper(5999):初始化完成,状态为 0 12-22 15:33:08.271:D/OpenCVManager/Helper(5999):与服务解除绑定 12-22 15:33 :08.271: D/OpenCVManager/Helper(5999): 使用回调调用 12-22 15:33:08.271: I/CartoonifierApp(5999): OpenCV 加载成功 12-22 15:33:08.279: D/dalvikvm(5999):试图加载 lib /data/data/com.Cartoonifier/lib/libcartoonifier.so 0x41936a40 12-22 15:33:08.279: D/dalvikvm(5999): Shared lib '/data/data/com.Cartoonifier/lib/libcartoonifier .so' 已经在同一个 CL 0x41936a40 12-22 15:33:08.302 中加载:I/Cartoonifier::SurfaceView(5999):surfaceCreated 12-22 15:33:08.302:I/Cartoonifier::SurfaceView(5999):surfaceChanged( )。窗口尺寸:1196x670 12-22 15:33:08.302: I/Cartoonifier::SurfaceView(5999): setupCamera(1196x670) 12-22 15:33:08.302: I/Cartoonifier::

12-22 15:33:08.333:I/dalvikvm-heap(5999):将堆(碎片情况)增加到 11.902MB,分配 1382416 字节

12-22 15:33:08.357: D/dalvikvm(5999): GC_CONCURRENT freed 1K, 3% free 12156K/12423K, paused 12ms+1ms,总共24ms

12-22 15:33:08.357: D/dalvikvm(5999): WAIT_FOR_CONCURRENT_GC 阻塞 11ms

12-22 15:33:08.365: D/dalvikvm(5999): GC_FOR_ALLOC 释放 0K,3% 释放 12156K/12423K,暂停 9ms,总共 9ms

12-22 15:33:08.372:I/dalvikvm-heap(5999):将堆(碎片情况)增加到 13.219MB,分配 1382416 字节

12-22 15:33:08.388: D/dalvikvm(5999): GC_CONCURRENT freed 0K, 3% free 13506K/13831K, paused 11ms+1ms,总共21ms

12-22 15:33:08.388: D/dalvikvm(5999): WAIT_FOR_CONCURRENT_GC 阻塞 7ms

12-22 15:33:08.404: D/dalvikvm(5999): GC_FOR_ALLOC freed <1K, 3% free 13506K/13831K, 暂停 9ms, 共 10ms

12-22 15:33:08.411:I/dalvikvm-heap(5999):将堆(碎片情况)增加到 16.735MB,分配 3686416 字节

12-22 15:33:08.427: D/dalvikvm(5999): GC_CONCURRENT freed <1K, 3% free 17106K/17479K, paused 12ms+1ms,总共22ms

12-22 15:33:08.427: D/dalvikvm(5999): WAIT_FOR_CONCURRENT_GC 阻塞 10ms

12-22 15:33:08.443: D/dalvikvm(5999): GC_FOR_ALLOC freed <1K, 3% free 17106K/17479K, 暂停 10ms, 共 10ms

12-22 15:33:08.450:I/dalvikvm-heap(5999):将堆(碎片情况)增加到 20.250MB,分配 3686416 字节

12-22 15:33:08.466: D/dalvikvm(5999): GC_CONCURRENT freed 0K, 2% free 20706K/21127K, paused 12ms+2ms,总共22ms

12-22 15:33:08.466: D/dalvikvm(5999): WAIT_FOR_CONCURRENT_GC 阻塞 5ms

12-22 15:33:08.466: I/Cartoonifier::SurfaceView(5999): 开始预览

12-22 15:33:08.497: E/BufferQueue(5999): [unnamed-5999-0] setBufferCount: SurfaceTexture 已被放弃!

12-22 15:33:08.505: E/Cartoonifier::SurfaceView(5999): mCamera.startPreview() 失败

CartoonifierApp.java 的片段显示我的修改

private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");

                // Load native library after(!) OpenCV initialization
                System.loadLibrary("cartoonifier");
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);

    Log.i(TAG, "Trying to load OpenCV library");
    if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback))
    {
      Log.e(TAG, "Cannot connect to OpenCV Manager");
    }

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    mView = new CartoonifierView(this);
    setContentView(mView);

    // Call our "onTouch()" callback function whenever the user touches the screen.
    mView.setOnTouchListener(this);
}


@Override
protected void onPause() {
    Log.i(TAG, "onPause");
    super.onPause();
    mView.releaseCamera();
}

@Override
public void onResume()
{
    super.onResume();
    Log.i(TAG, "onResume");
    if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback))
    {
      Log.e(TAG, "Cannot connect to OpenCV Manager");
    }   

    if( !mView.openCamera() ) {
        AlertDialog ad = new AlertDialog.Builder(this).create();  
        ad.setCancelable(false); // This blocks the 'BACK' button  
        ad.setMessage("Fatal error: can't open camera!");  
        /*ad.setButton("OK", new DialogInterface.OnClickListener() {  
            public void onClick(DialogInterface dialog, int which) {  
                dialog.dismiss();                      
                finish();
            }  
        });  */
        ad.show();
    }
}
4

1 回答 1

9

前段时间在 OpenCV 中解决了这个问题。

不确定是应用程序还是操作系统错误。问题是调用Bitmap.createBitmap分离SurfaceTexture用于可视化的对象。

解决方法是修改基类的setupCameraView方法并更改

try {
    setPreview();
} catch (IOException e) {
    Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e);
}

/* Notify that the preview is about to be started and deliver preview size */
onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height);

/* Notify that the preview is about to be started and deliver preview size */
onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height);

try {
    setPreview();
} catch (IOException e) {
    Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e);
}

(行的顺序改变了)

于 2012-12-22T21:30:34.270 回答