- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在使用原生 OpenCv 2.4.11(3.0.0 也可以)和 Android Studio 1.3+ 以及新的 ndk 支持时遇到问题。所有关于 .mk 文件的教程,但我想将它与新的实验性 gradle 一起使用。
使用 Kiran 答案 OpenCV in Android Studio我在 Java 中工作得很好。我还更改了
gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip
build.gradle(应用程序):
classpath 'com.android.tools.build:gradle-experimental:0.2.0'
build.gradle(模块):
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.0"
defaultConfig.with {
applicationId = "android.overloaded.nativetestv4"
minSdkVersion.apiLevel = 18
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
}
android.ndk{
moduleName = "mymodule"
ldLibs += ['log']
cppFlags += "-std=c++11"
cppFlags += "-fexceptions"
cppFlags += "-I${file("C:/DevAndroid/OpenCV-android-sdk/sdk/native/jni/include")}".toString()
cppFlags += "-I${file("C:/DevAndroid/OpenCV-android-sdk/sdk/native/jni/include/opencv")}".toString()
ldLibs += ["android", "EGL", "GLESv2", "dl", "log", "z"] //added z
stl = "stlport_static"//"gnustl_shared" changed to stlport static
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.pro')
}
}
android.productFlavors { //added all productFlavors code
create("arm") {
ndk.with {
abiFilters += "armeabi"
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\armeabi\\" //"-L" +
ldLibs += libsDir + "libnative_camera_r4.3.0.so"
ldLibs += libsDir + "libopencv_contrib.a"
ldLibs += libsDir + "libopencv_core.a"
ldLibs += libsDir + "libopencv_highgui.a"
ldLibs += libsDir + "libopencv_imgproc.a"
ldLibs += libsDir + "libopencv_info.so"
ldLibs += libsDir + "libopencv_java.so"
ldLibs += libsDir + "libopencv_legacy.a"
ldLibs += libsDir + "libopencv_ml.a"
ldLibs += libsDir + "libopencv_ts.a"
}
}
create("armv7") {
ndk.with {
abiFilters += "armeabi-v7a"
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\armeabi-v7a\\" //"-L" +
ldLibs += libsDir + "libnative_camera_r4.3.0.so"
ldLibs += libsDir + "libopencv_contrib.a"
ldLibs += libsDir + "libopencv_core.a"
ldLibs += libsDir + "libopencv_highgui.a"
ldLibs += libsDir + "libopencv_imgproc.a"
ldLibs += libsDir + "libopencv_info.so"
ldLibs += libsDir + "libopencv_java.so"
ldLibs += libsDir + "libopencv_legacy.a"
ldLibs += libsDir + "libopencv_ml.a"
ldLibs += libsDir + "libopencv_ts.a"
}
}
create("fat") {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.0'
compile project(':openCVLibrary2411')
}
通过此更改,我可以在 jni 文件夹下的 C++ 文件中调用我的 native 虚拟函数。还有 #include <opencv2/core/core.hpp>
和其他人由 CLion 支持......当我声明 cv::Mat mat;给我这个错误:
Linker failed while linking libmymodule.so failed.
C:\DevAndroid\OpenCV-android-sdk\sdk\native\jni\include/opencv2/core/mat.hpp:278: error: undefined reference to 'cv::fastFree(void*)'
C:\DevAndroid\OpenCV-android-sdk\sdk\native\jni\include/opencv2/core/mat.hpp:367: error: undefined reference to 'cv::Mat::deallocate()'
collect2.exe: error: ld returned 1 exit status
C++代码:
#include <jni.h>
#include <vector>
#include <string>
#include "helpers.h"
#include <opencv2/core/core.hpp> //This is supported by CLion
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
extern "C" {
JNIEXPORT jstring JNICALL
Java_android_overloaded_nativetestv4_MainActivity_FuncV1(JNIEnv *env, jclass type) {
cv::Mat mat; //causing error
string s = "Cpp v1 - succ ";
return env->NewStringUTF(s.data());
}
}
更新
- 将“z”添加到 ldLibs
- 添加了 android.productFlavors block
现在编译成功,应用程序第二次启动然后崩溃并显示此消息
事件日志:
IllegalArgumentException: Error while starting native debug session: Unable to find process for android.overloaded.nativetestv4 on device huawei-huawei_y530_u00-64a6513ccd8e
调试信息:
Restart ADB integration and try again
Waiting for process: android.overloaded.nativetestv4
DEVICE SHELL COMMAND: run-as android.overloaded.nativetestv4 mkdir -p /data/data/android.overloaded.nativetestv4/lldb/bin
DEVICE SHELL COMMAND: cat /data/local/tmp/lldb-server | run-as android.overloaded.nativetestv4 sh -c 'cat > /data/data/android.overloaded.nativetestv4/lldb/bin/lldb-server; chmod 700 /data/data/android.overloaded.nativetestv4/lldb/bin/lldb-server'
DEVICE SHELL COMMAND: rm /data/local/tmp/lldb-server
DEVICE SHELL COMMAND: cat /data/local/tmp/start_lldb_server.sh | run-as android.overloaded.nativetestv4 sh -c 'cat > /data/data/android.overloaded.nativetestv4/lldb/bin/start_lldb_server.sh; chmod 700 /data/data/android.overloaded.nativetestv4/lldb/bin/start_lldb_server.sh'
DEVICE SHELL COMMAND: rm /data/local/tmp/start_lldb_server.sh
Now Launching Native Debug Session
Error while starting native debug session: java.lang.IllegalArgumentException: Unable to find process for android.overloaded.nativetestv4 on device huawei-huawei_y530_u00-64a6513ccd8e
日志:
A/libc﹕ Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 3077 (ed.nativetestv4)
最佳答案
经过几周的痛苦,我成功了。这是我针对 Android studio 1.3.1、OpenCv 2.4.11 的正确代码。首先你应该这样做OpenCV in Android Studio (不要使用包含空格的路径,无论是项目还是 opencv 提取文件夹)然后,对于 opencv 是 native 的:
gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip
build.gradle(应用程序):
classpath 'com.android.tools.build:gradle-experimental:0.2.0'
build.gradle(模块):
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.0"
defaultConfig.with {
applicationId = "android.overloaded.nativetestv4"
minSdkVersion.apiLevel = 18
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
}
android.ndk{
moduleName = "mymodule"
ldLibs += ['log']
cppFlags += "-std=c++11"
cppFlags += "-fexceptions"
cppFlags += "-I${file("C:/DevAndroid/OpenCV-android-sdk/sdk/native/jni/include")}".toString()
cppFlags += "-I${file("C:/DevAndroid/OpenCV-android-sdk/sdk/native/jni/include/opencv")}".toString()
// ldFlags += linkOpt
ldLibs += ["android", "EGL", "GLESv2", "dl", "log", "z"]// , "ibopencv_core"
stl = "gnustl_shared"//"gnustl_static"//"gnustl_shared"//"stlport_static"
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.pro')
}
}
android.productFlavors {
create("arm") {
ndk.with {
abiFilters += "armeabi"
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\armeabi\\" //"-L" +
ldLibs += libsDir + "libnative_camera_r4.3.0.so"
ldLibs += libsDir + "libopencv_contrib.a"
ldLibs += libsDir + "libopencv_core.a"
ldLibs += libsDir + "libopencv_highgui.a"
ldLibs += libsDir + "libopencv_imgproc.a"
ldLibs += libsDir + "libopencv_info.so"
ldLibs += libsDir + "libopencv_java.so"
ldLibs += libsDir + "libopencv_legacy.a"
ldLibs += libsDir + "libopencv_ml.a"
ldLibs += libsDir + "libopencv_ts.a"
}
}
create("armv7") {
ndk.with {
abiFilters += "armeabi-v7a"
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\armeabi-v7a\\" //"-L" +
ldLibs += libsDir + "libnative_camera_r4.3.0.so"
ldLibs += libsDir + "libopencv_contrib.a"
ldLibs += libsDir + "libopencv_core.a"
ldLibs += libsDir + "libopencv_highgui.a"
ldLibs += libsDir + "libopencv_imgproc.a"
ldLibs += libsDir + "libopencv_info.so"
ldLibs += libsDir + "libopencv_java.so"
ldLibs += libsDir + "libopencv_legacy.a"
ldLibs += libsDir + "libopencv_ml.a"
ldLibs += libsDir + "libopencv_ts.a"
}
}
create("x86") {
ndk.with {
abiFilters += "x86"
}
}
create("mips") {
ndk.with {
abiFilters += "mips"
}
}
create("fat") {
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.0'
compile project(':openCVLibrary2411')
}
.cpp
#include <jni.h>
#include <vector>
#include <string>
#include "helpers.h"
#include <opencv2/core/core.hpp>
using namespace std;
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jstring JNICALL
Java_android_overloaded_nativetestv4_MainActivity_FuncV1(JNIEnv *env, jclass type) {
cv::Mat mat(33,22, 1);
string s = "Cpp v1 - succ, rows#:" + Helpers::ToStringNum(mat.rows);
Helpers::Log(s);
return env->NewStringUTF(s.data());
}
}
.java
package android.overloaded.nativetestv4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
static {
System.loadLibrary("mymodule");
}
public static native String FuncV1();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, FuncV1(), Toast.LENGTH_LONG).show();
((TextView)findViewById(R.id.txtInfo)).setText(FuncV1());
}
Android Studio 1.5.1、OpenCV 3.1 更新
1.首先你应该这样做OpenCV in Android Studio (不要使用包含空格的路径,无论是项目还是 opencv 提取文件夹),之后你应该有正常工作的 Java 应用程序
2.更改项目根目录中的build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle-experimental:0.4.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
修改opencv模块中的build.gradle:
apply plugin: 'com.android.model.library'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.2"
defaultConfig.with {
minSdkVersion.apiLevel = 11
targetSdkVersion.apiLevel = 23
}
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles.add(file("proguard-rules.txt"))
}
}
}
更改应用模块中的 build.gradle:
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.2"
defaultConfig.with {
applicationId = "android.overloaded.opencv31v1" //Name of package
minSdkVersion.apiLevel = 11
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
}
android.ndk {
moduleName = "native" // Name of C++ module, for System.loadLibrary("native")
cppFlags.add("-std=c++11") // Add provisions to allow C++11 functionality
cppFlags.add("-fexceptions")
// YOUR OPENCV DIRECTORY!!!
cppFlags.add("-I${file("C:/DevAndroid/OpenCV-android-sdk/sdk/native/jni/include")}".toString())
ldLibs.addAll(["android", "EGL", "GLESv2", "dl", "log", "z"])
stl = "gnustl_shared"
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles.add(file("proguard-rules.txt"))
}
}
android.productFlavors {
create("arm") {
ndk.with{
abiFilters.add("armeabi")
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath + "\\src\\main\\jniLibs\\armeabi\\"
ldLibs.add(libsDir + "libopencv_core.a")
ldLibs.add(libsDir + "libopencv_highgui.a")
ldLibs.add(libsDir + "libopencv_imgproc.a")
ldLibs.add(libsDir + "libopencv_java3.so")
ldLibs.add(libsDir + "libopencv_ml.a")
}
}
create("armv7") {
ndk.with {
abiFilters.add("armeabi-v7a")
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath + "\\src\\main\\jniLibs\\armeabi-v7a\\"
ldLibs.add(libsDir + "libopencv_core.a")
ldLibs.add(libsDir + "libopencv_highgui.a")
ldLibs.add(libsDir + "libopencv_imgproc.a")
ldLibs.add(libsDir + "libopencv_java3.so")
ldLibs.add(libsDir + "libopencv_ml.a")
ldLibs.add(libsDir + "libopencv_ts.a")
}
}
}
android.sources{
main{
jni{
source{
srcDirs += ['src/main/jniMy']
}
}
}
}
}
dependencies {
compile fileTree(dir: "libs", include: [$/*.jar/$])
compile "com.android.support:appcompat-v7:23.1.1" //Do not use 23.2.0
compile project(":openCVLibrary310")
}
创建目录 app/src/main/jniMy,这是存放您的 .cpp 文件的地方
可选择在 jniMy 中创建文件 chelper.c,代码如下:
#ifndef OPENCV31V1_CHELPER_H
#define OPENCV31V1_CHELPER_H
#include <jni.h>
#include <vector>
#include <string>
#include <sstream>
#include <android/log.h>
using namespace std;
class Helpers{
public:
template <typename T>
static string ToStringNum(T tNumber){
ostringstream ostrStream;
ostrStream << tNumber;
return ostrStream.str();
}
static void Log(string s){
__android_log_print(ANDROID_LOG_INFO, "My", s.data());
}
static void Log(std::exception ee){
string s = "Exception: " + string(ee.what()) ;
__android_log_print(ANDROID_LOG_INFO, "My", s.data());
}
static long Time(){
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (long)((int64_t)now.tv_sec * 1000LL + now.tv_nsec /1000000LL);
}
static string Time(long iTime){
return ToStringNum(Time() - iTime) + "ms";
}
};
#endif //OPENCV31V1_CHELPER_H
在 jniMy 中创建文件 native_exporter.cpp,代码如下:
#include <jni.h>
#include <string>
#include <opencv2/core/core.hpp>
#include "chelper.h"
extern "C"{
JNIEXPORT jstring JNICALLJava_android_overloaded_opencv31v1_MainActivity_FuncV1(JNIEnv *env,
jclass type) {
cv::Mat mat(33,22, CV_32FC2);
string s = "Cpp v1 - succ, rows#:" +Helpers::ToStringNum(mat.rows);
Helpers::Log(s);
auto i = 5;
for (int i2 : {1,3,5}){
Helpers::Log(Helpers::ToStringNum(i2));
}
return env->NewStringUTF(s.data());
}
}
MainActivity.java 中的代码:
package android.overloaded.opencv31v1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceView;
import android.widget.TextView;
import android.widget.Toast;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2
{
private static void Log(String sMsg){Log.i("My", "MainActivity." + sMsg );}
static{
System.loadLibrary("opencv_java3");
System.loadLibrary("native");
}
public native String FuncV1();
JavaCameraViewCustom2 _javaCameraView; //Similar to JavaCameraView
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(getSupportActionBar() != null)getSupportActionBar().hide();
String sRes = FuncV1();
Toast.makeText(this, sRes, Toast.LENGTH_LONG).show();
((TextView)findViewById(R.id.txtInfo)).setText(sRes);
_javaCameraView = (JavaCameraViewCustom2)findViewById(R.id.cameraView);
_javaCameraView.setVisibility(SurfaceView.VISIBLE);
_javaCameraView.setCvCameraViewListener(this);
}
@Override
protected void onResume()
{
super.onResume();
_javaCameraView.enableView();
}
@Override
protected void onPause()
{
super.onPause();
_javaCameraView.disableView();
}
Mat _imgBgrTemp;
@Override
public void onCameraViewStarted(int width, int height)
{
_imgBgrTemp = new Mat(height,width, CvType.CV_8UC4);
}
@Override
public void onCameraViewStopped()
{
_imgBgrTemp.release();
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
{
_imgBgrTemp = inputFrame.rgba();
return _imgBgrTemp;
}
}
Android Studio 会要求你更改 jdk,它还会安装新的 gradle,你应该按照它的要求做所有事情。
关于android - OpenCv 和 Android studio 1.3+ 使用新的 gradle - undefined reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32171259/
我遇到了一个奇怪的问题。我的应用程序的 Release 版本似乎运行良好,但最近当我切换到 Debug 版本时,我在启动时立即遇到访问冲突。当释放分配的内存块时,就会发生访问冲突。所有这些都发生在静态
我在 C# 中偶然发现了这种奇怪的语法形式,并试图弄清楚它的含义以及如何使用它。网络上似乎没有关于此的任何文档。 object data = new { var1 = someValue, var2
我正在尝试使用浏览器的内置类型 CSSStyleDeclaration 以编程方式传递和修改样式(由于 .cssText 属性,这很方便)。 但是,new CSSStyleDeclaration()
我有现成的代码: internal bool firstAsSymbol(out Symbol s) { return (s = first as Symbol) !=
在新的 Eclipse 版本 2022-03 中,一些(但不是全部)java 项目在 Project Explorer View 中的外观发生了变化。尽管 Package Presentation 设
我正在尝试使用 FormData 通过获取 API 在 POST 请求中发送用户输入的数据。问题是,当我用我创建的表单创建一个新的 FormData 对象时,它一直在创建一个空对象——没有条目/键/值
我有一个用一些 intel-intrinsincs 编写的 C 代码。在我先用 avx 然后用 ssse3 标志编译后,我得到了两个完全不同的汇编代码。例如: AVX: vpunpckhbw %xm
最近,discord 为您自己的应用程序添加了对斜杠命令的支持。我通读了它的文档,并尝试搜索一些视频(但是该功能刚刚出现),但我不明白我实际上需要做什么才能使其正常工作。我正在使用 WebStorm(
我想使用 JRI 从 Java 调用 R。 我在 eclipse 下在主类中运行它: Rengine c = new Rengine(new String[] { "--vanilla" },
我正在使用新的 Place Autocomplete那是来自新的静态Google Places SDK 客户端库 (here)。所以它真的很容易使用,我刚得到this tutorial它按预期工作。
我刚刚更新到 flutter 版本 1.25.0-5.0.pre.92,我的代码中出现了很多与空安全相关的错误,这些错误以前运行良好。我没有以任何方式选择空安全,我所做的只是运行 flutter 升级
我已经使用 React Native 有一段时间了,但我想我会在网络上试用 React。所以我遵循了这个指南:https://reactjs.org/docs/create-a-new-react-a
周六早上在这里。尝试学习新的 Scala 编译器 dotty。 安装在我的 Mac 上使用 brew install lampepfl/brew/dotty 安装成功。我有版本 dotr -versi
我使用了谷歌地方的新依赖。单击自动完成 View 时应用程序崩溃。错误如下。, java.lang.NullPointerException: Place Fields must be set.
我关注了这个博客-> https://medium.com/@teyou21/training-your-object-detection-model-on-tensorflow-part-2-e9e
在哪里可以找到用于在此架构上进行组装的新寄存器的名称? 我指的是 X86 中的寄存器,如 EAX、ESP、EBX 等。但我希望它们是 64 位的。 我认为它们与我反汇编 C 代码时不同,我得到的是 r
新的服务总线库 Azure.Messaging.ServiceBus 使用 ServiceBusReceivedMessage 来接收消息 https://learn.microsoft.com/en
需要使用实时流媒体 channel 的实时编码类型在新的 Azure 门户中配置广告插入和石板图像。请帮忙解决这个问题,因为我找不到该功能。 最佳答案 此处描述了 Azure 媒体服务的广告插入选项
我正在使用新的 GitHub 操作,下面的工作流程的想法是在打开或同步 pr 时运行,它应该首先检查并安装依赖项,然后运行一些 yarn 脚本 name: PR to Master on: pul
我听说 DMD 2.058 中将有一个用于匿名函数的新语法,但我找不到任何相关信息。新语法是什么?旧语法是否会被弃用? 最佳答案 我相信它就像 C#'s . 以下内容是等效的: delegate(i,
我是一名优秀的程序员,十分优秀!