gpt4 book ai didi

java - native Android 教程给出错误

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:31 25 4
gpt4 key购买 nike

我是native android新手,完全不懂,刚开始学习,关注this教程,但这给了我错误。以下是我的代码

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文件

#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>

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

任何给出解释的帮助都是非常有用的,这样我就可以更好地学习如何更好地理解它

谢谢

最佳答案

我遇到了和你一样的问题,我做了以下事情:

  1. 注释掉“Activity_main.xml”中使用“org.opencv.android.NativeCameraView”的所有部分。这个类和“org.opencv.android.JavaCameraView”的区别你可以深入阅读-> What is the difference between `opencv.android.JavaCameraView` and `opencv.android.NativeCameraView` .
  2. 在“/res/values”中创建一个名为“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>

关于java - native Android 教程给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22929038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com