2014-04-08 88 views
1

我是新来的本机android并不知道它,我只是在学习的初始阶段,我正在遵循this教程,但这给了我错误。以下是我的代码本机Android教程给出错误

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 

    } 

    } 

这是我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); 
} 

,这是我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> 

这是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> 

现在日问题是代码工作正常,但是当我在我的设备上运行它时,它刚刚启动,但什么都没显示,它只运行黑屏。

任何给予解释的帮助是非常appriciated和有益的,这样我可以更好地学习如何更好地理解它

感谢

+0

在主活动类,尝试取消注释'包com。示例。 myapp' – Lunchbox

+0

我做了,但仍然错误 – AHF

+0

我建议使用http://www.mybringback.com/series/android-basics/作为基础。那是我开始的地方。我知道这不能回答你的问题,但我认为它可以帮助你 – Lunchbox

回答

2

我有同样的问题,因为你和我做了以下几件事:

  1. 注释掉使用“org.opencv.android.NativeCameraView”的“Activity_main.xml”中的所有部分。这个类和“org.opencv.android.JavaCameraView”之间的区别可以深入阅读 - >What is the difference between `opencv.android.JavaCameraView` and `opencv.android.NativeCameraView`
  2. 在“/ RES /值”名为“attrs.xml”创建一个文件,并粘贴此部分
<?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>