Ask Your Question
0

Use native OpenCV C++ camera in JNI call

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

stephanmg gravatar image

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

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) [https://groups.google.com/forum/?fromgroups=#!topic/android-opencv/SUpxCPvQPIE] and 2) [http://code.opencv.org/issues/1193]

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="http://schemas.android.com/apk/res/android"
      package="com.example.hellojni"
      android:versionCode="1"
      android:versionName="1.0">
    <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="android.hardware.camera.any" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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

And my Java Code:

package com.example.hellojni;

import android.app.Activity;
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. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(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() );
        setContentView(tv);
    }

    /* 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/libhello-jni.so at
     * installation time by the package manager.
     */
    static {
        System.loadLibrary("opencv_java");
        System.loadLibrary("hello-jni");
    }
}
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

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

  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

Comments

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 -0600 )edit
0

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

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

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

Comments

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 -0600 )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: http://code.opencv.org/issues/1244

Alexander Smorkalov gravatar imageAlexander Smorkalov ( 2013-03-06 06:54:02 -0600 )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 -0600 )edit

Question Tools

Stats

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

Seen: 3,717 times

Last updated: Mar 06 '13