当前位置: 首页 > news >正文

Android集成OpenCV4实例

Android集成OpenCV4分以下几步骤:

使用Android Studio Giraffe | 2022.3.1创建一个Empty Views Activity空项目,包名为:com.example.andopencvdemo00 ,Android Studio创建Empty Views Activity空项目
创建成功后,进行以下相关设置:

第一步:在项目settings.gradle里,添加国内阿里云源:

        maven { url 'https://jitpack.io' }
//        maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }maven { url 'https://maven.aliyun.com/repository/public' }maven { url 'https://maven.aliyun.com/nexus/content/groups/public/'}maven { url 'https://maven.aliyun.com/nexus/content/repositories/gradle-plugin'}maven { url 'https://maven.aliyun.com/nexus/content/repositories/apache-snapshots'}google()mavenCentral()gradlePluginPortal()

settings.gradle整体示例:

pluginManagement {repositories {maven { url 'https://jitpack.io' }
//        maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }maven { url 'https://maven.aliyun.com/repository/public' }maven { url 'https://maven.aliyun.com/nexus/content/groups/public/'}maven { url 'https://maven.aliyun.com/nexus/content/repositories/gradle-plugin'}maven { url 'https://maven.aliyun.com/nexus/content/repositories/apache-snapshots'}google()mavenCentral()gradlePluginPortal()}
}
dependencyResolutionManagement {repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)repositories {maven { url 'https://jitpack.io' }
//        maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }maven { url 'https://maven.aliyun.com/repository/public' }maven { url 'https://maven.aliyun.com/nexus/content/groups/public/'}maven { url 'https://maven.aliyun.com/nexus/content/repositories/gradle-plugin'}maven { url 'https://maven.aliyun.com/nexus/content/repositories/apache-snapshots'}google()mavenCentral()gradlePluginPortal()}
}rootProject.name = "AndOpenCVDemo00"
include ':app'

第二步:在app下build.gradle添加OpenCV和ndk:

在dependencies里添加OpenCV

 implementation 'org.opencv:opencv:4.12.0'  //集成OpenCV 4.12.0版本

在defaultConfig 里添加ndk架构

 ndk {abiFilters "arm64-v8a", "armeabi-v7a" // 主流架构,可按需添加x86_64}

build.gradle整体示例:

plugins {id 'com.android.application'
}android {namespace 'com.example.andopencvdemo00'compileSdk 33defaultConfig {applicationId "com.example.andopencvdemo00"minSdk 24targetSdk 33versionCode 1versionName "1.0"ndk {abiFilters "arm64-v8a", "armeabi-v7a" // 主流架构,可按需添加x86_64}testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}}dependencies {implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])implementation 'androidx.appcompat:appcompat:1.6.1'implementation 'com.google.android.material:material:1.8.0'implementation 'androidx.constraintlayout:constraintlayout:2.1.4'testImplementation 'junit:junit:4.13.2'androidTestImplementation 'androidx.test.ext:junit:1.1.5'androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'//implementation 'org.opencv:opencv:4.9.0' //集成OpenCV 4.9.0版本//implementation 'org.opencv:opencv:4.11.0' //集成OpenCV 4.11.0版本implementation 'org.opencv:opencv:4.12.0'  //集成OpenCV 4.12.0版本
}

第三步:编写测试程序

MainActivity文件代码:

package com.example.andopencvdemo00;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.Toast;import org.opencv.android.CameraActivity;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;import java.util.Collections;
import java.util.List;/*** Android OpenCV(>= 4.9.0) 示例代码:* 参考网址:https://docs.opencv.org/4.x/d5/df8/tutorial_dev_with_OCV_on_Android.html* 注:开发板必须接上摄像头*/public class MainActivity extends CameraActivity implements CameraBridgeViewBase.CvCameraViewListener2 {private static final String TAG = "DDSD";private CameraBridgeViewBase mOpenCvCameraView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (OpenCVLoader.initLocal()) {Log.i(TAG, "OpenCV loaded successfully");} else {Log.e(TAG, "OpenCV initialization failed!");(Toast.makeText(this, "OpenCV initialization failed!", Toast.LENGTH_LONG)).show();return;}getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);setContentView(R.layout.activity_main);mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);mOpenCvCameraView.setCvCameraViewListener(this);}@Overridepublic void onPause(){super.onPause();if (mOpenCvCameraView != null)mOpenCvCameraView.disableView();}@Overridepublic void onResume(){super.onResume();if (mOpenCvCameraView != null)mOpenCvCameraView.enableView();}@Overrideprotected List<? extends CameraBridgeViewBase> getCameraViewList() {return Collections.singletonList(mOpenCvCameraView);}@Overridepublic void onDestroy() {super.onDestroy();if (mOpenCvCameraView != null)mOpenCvCameraView.disableView();}@Overridepublic void onCameraViewStarted(int width, int height) {}@Overridepublic void onCameraViewStopped() {}@Overridepublic Mat onCameraFrame(CvCameraViewFrame inputFrame) {return inputFrame.rgba();}}

activity_main.xml文件内容:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="match_parent"android:layout_height="match_parent" ><!-- [camera_view] --><org.opencv.android.JavaCameraViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:visibility="gone"android:id="@+id/tutorial1_activity_java_surface_view"opencv:show_fps="true"opencv:camera_id="any" /><!-- [camera_view] --></FrameLayout>

AndroidManifest.xml文件内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><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"/><applicationandroid:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.AndOpenCVDemo00"tools:targetApi="31"><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

代码下载地址:https://download.csdn.net/download/weixin_43800734/91868294

http://www.dtcms.com/a/366771.html

相关文章:

  • Java 与 Docker 的最佳实践
  • docker更新jar包,懒人执行脚本
  • MaxKB4j智能体平台 Docker Compose 快速部署教程
  • 飞算JavaAI全面解析:重塑Java开发流程的智能引擎
  • 【数学建模】用Matlab玩转图论:从画图到求最短路径
  • 想要给文档加密?2025年顶尖文件加密软件分享
  • C++并发编程-23. 线程间切分任务的方法
  • uniapp vue页面传参到webview.nvue页面的html或者另一vue中
  • Web应用:返回图片URL
  • Python快速入门专业版(一):Windows/macOS/Linux 系统环境搭建(附常见报错解决)
  • 【连接器专题】案例:带屏蔽膜FPC出现概率性短路,真是供应商的锅?
  • EasyVoice与cpolar:构建私域有声平台的本地化方案
  • Spring线程池ThreadPoolTaskExecutor‌详解
  • 隔空盗刷、AI钓鱼、代理劫持…金融黑产竟进化至此?
  • Elasticsearch 8 中 Nested 数据类型的使用方法
  • 【iOS】 懒加载
  • 一文吃透 CSS 伪类:从「鼠标悬停」到「斑马纹表格」的 30 个实战场景
  • 中值滤波、方框滤波、高斯滤波、均值滤波、膨胀、腐蚀、开运算、闭运算
  • HTML图片标签及路径详解
  • Python开篇撬动未来的万能钥匙 从入门到架构的全链路指南
  • 工厂模式总结
  • C++知识
  • C 盘清理技巧分享:释放磁盘空间,提升系统性能
  • 将 PDF 转换为 TIFF 图片:简单有效的 Java 教程
  • 数据传输,数据解析与写数据库
  • django全国小米su7的行情查询系统(代码+数据库+LW)
  • 阿瓦隆 A15 Pro 221TH/S:SHA-256 算力与高效能耗
  • 大模型部署全攻略:Docker+FastAPI+Nginx搭建高可用AI服务
  • Linux 编译 Android 版 QGroundControl 软件并运行到手机上
  • 一天涨幅2000倍的期权有吗?