原生Android教程是给错误 [英] Native Android Tutorial is giving error
问题描述
我是新来的原生Android并没有关于它的想法,我只是在学习的初级阶段,我下面的本教程,但是这是给我的错误。以下是我的code
MainActivity.java 进口org.opencv.android.BaseLoaderCallback;
进口org.opencv.android.CameraBridgeViewBase;
进口org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
进口org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
进口org.opencv.android.LoaderCallbackInterface;
进口org.opencv.core.Mat; 进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log; //包com.example.myapp //此处添加进口
公共类MainActivity扩展活动实现CvCameraViewListener2 { 公共本土INT convertNativeGray(长matAddrRgba,长matAddrGray); 私人垫mRgba;
私人垫mGray;
私人最终静态字符串变量=MainActivity;
私人CameraBridgeViewBase mOpenCvCameraView;
公共本土INT convertNativeGray(INT N);
//保护CameraBridgeViewBase mOpenCvCameraView; //另一部分 私人BaseLoaderCallback mLoaderCallback =新BaseLoaderCallback(本)
{
@覆盖
公共无效onManagerConnected(INT状态){
开关(状态){
案例LoaderCallbackInterface.SUCCESS:
{
的System.loadLibrary(nativegray); //加载机模块
//字符串标记= NULL;
Log.i(TAG的OpenCV加载成功);
// CameraBridgeViewBase mOpenCvCameraView = NULL;
mOpenCvCameraView.enableView();
}打破;
默认:
{
super.onManagerConnected(状态);
}打破;
}
}
}; //一些更多的东西 公共垫onCameraFrame(CvCameraViewFrame inputFrame){
mRgba = inputFrame.rgba();
convertNativeGray(mRgba.getNativeObjAddr(),
mGray.getNativeObjAddr());
返回mGray;
} @覆盖
公共无效onCameraViewStarted(INT宽度,高度INT){
// TODO自动生成方法存根 } @覆盖
公共无效onCameraViewStopped(){
// TODO自动生成方法存根 } }
这是我的 JNI文件
的#include< jni.h>
#包括opencv2 /核心/ core.hpp
#包括LT&; opencv2 / highgui / highgui.hpp>
#包括LT&; opencv2 / imgproc / imgproc.hpp>
#包括LT&;&stdio.h中GT;使用命名空间std;
使用命名空间的简历;INT toGray(IMG垫,垫和放大器;灰色);为externC{
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){ 垫&安培; mRgb = *(*垫)addrRgba;
垫&安培; mGray = *(*垫)addrGray; INT CONV;
jint retVal的; 兑换= toGray(mRgb,mGray);
retVal的=(jint)CONV; 返回retVal的;}}INT toGray(IMG垫,垫和放大器;灰色)
{
cvtColor(IMG,灰色,CV_RGBA2GRAY); //假设RGBA输入 如果(gray.rows == img.rows&放大器;&安培; gray.cols == img.cols)
{
返回(1);
}
返回(0);
}
这是我的 Activity_main
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
的xmlns:OpenCV的=http://schemas.android.com/apk/res-auto
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直> < org.opencv.android.NativeCameraView
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:知名度=水涨船高
机器人:ID =@ + ID / opencv_part_native_surface_view
OpenCV的:show_fps =真
OpenCV的:camera_id =任何/> < org.opencv.android.JavaCameraView
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:知名度=水涨船高
机器人:ID =@ + ID / opencv_part_java_surface_view
OpenCV的:show_fps =真
OpenCV的:camera_id =任何/>< / LinearLayout中>
这是manifest.xml的
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.nextin.filters
安卓版code =1
机器人:=的versionName1.0> <用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =19/> <应用
机器人:allowBackup =真
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名字=com.example.myapp.Opencvpart
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途> <使用许可权的android:NAME =android.permission.CAMERA/> <使用特征的android:NAME =android.hardware.camera机器人:所需=FALSE/>
<使用特征的android:NAME =android.hardware.camera.autofocus机器人:所需=FALSE/>
<使用特征的android:NAME =android.hardware.camera.front机器人:所需=FALSE/>
<使用特征的android:NAME =android.hardware.camera.front.autofocus机器人:所需=FALSE/>
<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAGE/>< /清单>
现在的问题是,code工作正常,但是当我在我的设备上运行它,它只是开始,但什么都不显示,它只有在黑色的屏幕上运行。
任何解释给予帮助是非常appriciated和有益的,这样我可以更好地学习如何更好地理解它。
感谢
我有同样的问题,因为你和我做了以下几件事:
- 注释掉在使用org.opencv.android.NativeCameraViewactivity_main.xml中中的所有部分。这个类和org.opencv.android.JavaCameraView之间的区别,你可以深入阅读 - > <一个href=\"http://stackoverflow.com/questions/16626343/what-is-the-difference-between-opencv-android-javacameraview-and-opencv-andro\">What是`opencv.android.JavaCameraView`和`opencv.android.NativeCameraView` 的之间的差异。
- 在/ RES /值名为attrs.xml创建一个文件,并粘贴此部分
&LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;资源&GT;
&LT;申报-设置样式名称=CameraBridgeViewBase&GT;
&LT; attr指示NAME =show_fps格式=布尔/&GT;
&LT; attr指示NAME =camera_id格式=整数&GT;
&LT;枚举名=任何值= - 1/&GT;
&LT;枚举名=后面的价值=2/&GT;
&LT;枚举名=前值=1/&GT;
&LT; / ATTR&GT;
&LT; /申报,设置样式&GT;
&LT; /资源&GT;
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
I had the same problem as you and I did the following things:
- 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` .
- 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>
这篇关于原生Android教程是给错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!