3

I am new to native android and have no idea about it , i am just in the initial stage of learning , i am following this tutorial , but this is giving me error . Following are my code

MainActivity.java

    import org.opencv.android.BaseLoaderCallback;
    import org.opencv.android.CameraBridgeViewBase;
    import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
    import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
    import org.opencv.android.LoaderCallbackInterface;
    import org.opencv.core.Mat;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;

    //package com.example.myapp

    //Add imports here
    public class MainActivity extends Activity implements CvCameraViewListener2 {

     public native int convertNativeGray(long matAddrRgba, long matAddrGray);

     private Mat mRgba;
     private Mat mGray;
     private final static String TAG = "MainActivity";
     private CameraBridgeViewBase mOpenCvCameraView;
     public native int convertNativeGray(int n);
    //protected CameraBridgeViewBase mOpenCvCameraView;

     // other part

     private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) 
     {
         @Override
         public void onManagerConnected(int status) {
             switch (status) {
                 case LoaderCallbackInterface.SUCCESS:
                 {
                     System.loadLibrary("nativegray");// Load Native module
                     //String TAG = null;
                    Log.i(TAG, "OpenCV loaded successfully");
                     //CameraBridgeViewBase mOpenCvCameraView = null;
                    mOpenCvCameraView.enableView();
                 } break;
                 default:
                 {
                     super.onManagerConnected(status);
                 } break;
             }
         }
     };

     // some more stuff

     public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
         mRgba = inputFrame.rgba();
         convertNativeGray(mRgba.getNativeObjAddr(), 
                           mGray.getNativeObjAddr());
         return mGray;
     }

    @Override
    public void onCameraViewStarted(int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCameraViewStopped() {
        // TODO Auto-generated method stub

    }

    }

This is my jni file

#include <jni.h>
#include "opencv2/core/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>

using namespace std;
using namespace cv;

int toGray(Mat img, Mat& gray);

extern "C" {
JNIEXPORT jint JNICALL Java_com_nextin_filters_MainActivity_convertNativeGray(JNIEnv*, jobject, jlong addrRgba, jlong addrGray);

JNIEXPORT jint JNICALL Java_com_nextin_filters_MainActivity_convertNativeGray(JNIEnv*, jobject, jlong addrRgba, jlong addrGray) {

    Mat& mRgb = *(Mat*)addrRgba;
    Mat& mGray = *(Mat*)addrGray;

    int conv;
    jint retVal;

    conv = toGray(mRgb, mGray);
    retVal = (jint)conv;

    return retVal;

}

}

int toGray(Mat img, Mat& gray)
{
    cvtColor(img, gray, CV_RGBA2GRAY); // Assuming RGBA input

    if (gray.rows == img.rows && gray.cols == img.cols)
    {
        return (1);
    }
    return(0);
}

and this is my Activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:opencv="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <org.opencv.android.NativeCameraView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        android:id="@+id/opencv_part_native_surface_view"
        opencv:show_fps="true"
        opencv:camera_id="any" />

    <org.opencv.android.JavaCameraView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        android:id="@+id/opencv_part_java_surface_view"
        opencv:show_fps="true"
        opencv:camera_id="any" />

</LinearLayout>

This is manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nextin.filters"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myapp.Opencvpart"
            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>

    <uses-permission android:name="android.permission.CAMERA"/>

    <uses-feature android:name="android.hardware.camera" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

</manifest>

Now the problem is that code is working fine , but when i run it on my device it just start but show nothing , it run with just the black screen .

Any help giving explanation is highly appriciated and helpful so that i can learn better how understand it better

Thanks

Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
AHF
  • 1,070
  • 2
  • 15
  • 47
  • In your Main activity class, try to uncomment `package com.example.myapp` – Lunchbox Apr 08 '14 at 07:04
  • i did but still error – AHF Apr 10 '14 at 08:23
  • I would suggest using http://www.mybringback.com/series/android-basics/ for the basics. That's where I started. I know that this does not answer your question, but I thought that it could help you out – Lunchbox Apr 10 '14 at 09:44

1 Answers1

3

I had the same problem as you and I did the following things:

  1. commenting out all the part in "Activity_main.xml" that uses "org.opencv.android.NativeCameraView" . The difference between this class and "org.opencv.android.JavaCameraView" you can read in depth -> What is the difference between `opencv.android.JavaCameraView` and `opencv.android.NativeCameraView` .
  2. Create a file in "/res/values" with the name "attrs.xml" and paste this part
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name = "CameraBridgeViewBase" >
        <attr name="show_fps" format="boolean"/>
        <attr name="camera_id" format="integer" >
            <enum name="any" value="-1" />
            <enum name="back" value="2" />
            <enum name="front" value="1" />
        </attr>
    </declare-styleable>
</resources>
Community
  • 1
  • 1
Andrei
  • 39
  • 8
  • It partially helped me. I got another error now : `incompatible types: int[] cannot be converted to int` at `int cameraIndex = styledAttrs.getInt(R.styleable.CameraBridgeViewBase_camera_id, -1);` from `R.styleable.CameraBridgeViewBase_camera_id` – Jérémy Jan 23 '19 at 19:33