Ask Your Question

Use native OpenCV C++ camera in JNI call

asked 2013-03-05 10:51:31 -0500

stephanmg gravatar image

updated 2013-03-06 03:29:59 -0500

Dear OpenCV Users,

as the topic suggests I want to use the OpenCV C++ interface for VideoCapture and use this as a native JNI call in my Java Android project.

Nevertheless I recognized that this isn't possible for CONSOLE applications as reported by: 1) [!topic/android-opencv/SUpxCPvQPIE] and 2) []

Is there any way to make this work again?

All the best, Stephan

P.S.: My AndroidManifest.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    <uses-sdk android:minSdkVersion="2" android:targetSdkVersion="4"/>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-feature android:name="" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application android:label="@string/app_name"
        <activity android:name=".HelloJni"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

And my Java Code:

package com.example.hellojni;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HelloJni extends Activity

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)

        /* Create a TextView and set its content.
         * the text is retrieved by calling a native
         * function.

        // this works
        final Button button = new Button(getApplicationContext());
                button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        // Perform action on click
        TextView  tv = new TextView(this);
        tv.setText( stringFromJNI() );

    /* A native method that is implemented by the
     * 'hello-jni' native library, which is packaged
     * with this application.
    public native String  stringFromJNI();

    /* This is another native method declaration that is *not*
     * implemented by 'hello-jni'. This is simply to show that
     * you can declare as many native methods in your Java code
     * as you want, their implementation is searched in the
     * currently loaded native libraries only the first time
     * you call them.
     * Trying to call this function will result in a
     * java.lang.UnsatisfiedLinkError exception !
    public native String  unimplementedStringFromJNI();

    /* this is used to load the 'hello-jni' library on application
     * startup. The library has already been unpacked into
     * /data/data/com.example.hellojni/lib/ at
     * installation time by the package manager.
    static {
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-03-06 01:37:05 -0500

  1. cv::VideoCapture does not work in simple console application, because it has no rights to access camera. Console application does not have its own manifest and cannot allow access to camera stack.

  2. cv::VideoCapture works ok in Java code with JNI calls. Do not forget add camera access permissions to Android Manifest.

edit flag offensive delete link more


I posted my AndroidManifest.xml and Java Code above. But it's not working, it's failing with:

03-06 10:35:57.508: E/OpenCV::camera(10449): calling (pGetPropertyC)(0xa5cd0, 0) 03-06 10:35:57.508: D/OpenCV_NativeCamera(10449): CameraHandler::getProperty(0) 03-06 10:35:57.508: E/OpenCV::camera(10449): calling (pGetPropertyC)(0xa5cd0, 1) 03-06 10:35:57.508: D/OpenCV_NativeCamera(10449): CameraHandler::getProperty(1)

stephanmg gravatar imagestephanmg ( 2013-03-06 03:36:22 -0500 )edit

answered 2013-03-06 03:36:53 -0500

updated 2013-03-06 03:37:32 -0500

The minimal Android SDK version for usage of OpenCV is 8 (Android 2.2). There is no VideoCapture back-end libraries for earlier Android versions. Try to run tutorial-1-camerapreview and switch it to native camera. Check, that it works on your device.

edit flag offensive delete link more


Thanks for the suggestions. We tried another device in which the code works. The device on which this is not working is an Milestone by Motorola. The Android Version is above 2.2, i. e. 2.3. Any other suggestions - please note the tutorial-1-camerapreview works.

stephanmg gravatar imagestephanmg ( 2013-03-06 05:04:53 -0500 )edit

Does it works in both Java and Native camera cases? There are some unsolved problems with VideoCapture on old (Android 2.x) Motorola devices. See ticket for more details:

Alexander Smorkalov gravatar imageAlexander Smorkalov ( 2013-03-06 06:54:02 -0500 )edit

No. In the native camera case it does not work, seems then to be an issue for the Motorola device with Android 2.x. We use Cyanogen Mod 7 as an additional information, maybe that's of interest?

stephanmg gravatar imagestephanmg ( 2013-03-06 14:01:55 -0500 )edit

Question Tools


Asked: 2013-03-05 10:51:31 -0500

Seen: 3,329 times

Last updated: Mar 06 '13