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.pswp.cn/news/921385.shtml
繁体地址,请注明出处:http://hk.pswp.cn/news/921385.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

npy可视化方法

npviewer 是一个应用程序&#xff0c;它允许您以热图的形式可视化 numpy 的 npy 文件中的数据。该应用程序根据不同的模式自动选择适当的维度进行显示。 根据不同的模式自动选择适当的维度进行显示支持不同格式的 numpy 数据的可视化&#xff0c;如 RGB 和灰度用户友好的界面使…

【Cesium】介绍及基础使用

文章目录一、Cesium 介绍二、 使用1、引入 cesium2、Viewer 配置选项1. 基础控件配置2. 场景与渲染配置3. 地形配置4. 天空与大气效果3、坐标系系统3.1 地理坐标系3.2 笛卡尔空间直角坐标系3.3 屏幕坐标系4、Entity 实体4.1 简介4.2 Entity 常见图形类型Point 点Polyline 线Pol…

基于SpringBoot的运动服装销售系统【2026最新】

作者&#xff1a;计算机学姐 开发技术&#xff1a;SpringBoot、SSM、Vue、MySQL、JSP、ElementUI、Python、小程序等&#xff0c;“文末源码”。 专栏推荐&#xff1a;前后端分离项目源码、SpringBoot项目源码、Vue项目源码、SSM项目源码、微信小程序源码 精品专栏&#xff1a;…

【嵌入式DIY实例-ESP32篇】-倾斜弹跳球游戏

倾斜弹跳球游戏 文章目录 倾斜弹跳球游戏 1、MPU6050介绍 2、硬件准备与接线 3、代码实现 在这个交互式 ESP32 Arduino 项目中,我们模拟了一个绿色球体在全彩 ST7789 170320 LCD 屏幕上弹跳,完全由 MPU6050 陀螺仪的运动控制。当你倾斜传感器时,球体会呈现出逼真的物理运动,…

从spring MVC角度理解HTTP协议及Request-Response模式

什么是HTTP协议&#xff1f;HTTP协议&#xff08;HyperText Transfer Protocol&#xff0c;超文本传输协议&#xff09;是一种通信规则&#xff0c;它定义了客户端&#xff08;如浏览器、手机APP&#xff09; 和服务器 之间如何交换信息&#xff0c;是用于在万维网&#xff08;…

江协科技STM32学习笔记补充之003 :STM32复位电路的详细分析

电路作用与每个器件R1&#xff08;10 kΩ&#xff0c;上拉到 3V3&#xff09;让 NRST 在无外力时保持高电平&#xff1d;不复位&#xff1b;同时与电容形成 RC&#xff0c;决定上电复位延时。阻值不能太小&#xff08;否则调试器或芯片复位驱动下拉电流太大&#xff09;&#x…

Spring Boot HTTP状态码详解

Spring Boot HTTP状态码完全指南&#xff1a;从入门到精通 前言 在RESTful API开发中&#xff0c;HTTP状态码是与客户端通信的重要桥梁。Spring Boot通过HttpStatus枚举提供了完整的HTTP状态码支持。本文将深入解析这些状态码的含义、使用场景以及在Spring Boot中的最佳实践。 …

怎样让外网计算机访问局域网计算机?通过公网地址访问不同内网服务的设置方法

局域网服务器提供公网访问&#xff0c;或指定某些端口应用资源给外地访问&#xff0c;都是常见跨网通信需求。在一些场景下&#xff0c;内部网络中的服务器需要通过公网地址进行访问&#xff0c;尤其是在没有固定公网IP或需要在外部访问时。为了解决这一问题&#xff0c;可以使…

Spring Boot启动失败从循环依赖到懒加载配置的深度排查指南

&#x1f49d;&#x1f49d;&#x1f49d;欢迎莅临我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 持续学习&#xff0c;不断…

从零开始学大模型之大语言模型

大语言模型 4.1 什么是 LLM 在前三章&#xff0c;我们从 NLP 的定义与主要任务出发&#xff0c;介绍了引发 NLP 领域重大变革的核心思想——注意力机制与 Transformer 架构。随着 Transformer 架构的横空出世&#xff0c;NLP 领域逐步进入预训练-微调范式&#xff0c;以 Tran…

如何将视频从 iPhone 转移到 Mac

将视频从 iPhone 转移到 Mac 是许多用户常见的需求。无论你是想备份重要的视频&#xff0c;还是希望在更大的屏幕上观看&#xff0c;以下方法都能帮助你轻松完成。方法一&#xff1a;使用 iReaShare iPhone ManageriReaShare iPhone Manager 是一款功能强大的工具&#xff0c;可…

五、Docker 核心技术:容器数据持久化之数据卷

Docker 容器本身是无状态且生命周期短暂的。当一个容器被删除时&#xff0c;它在可写层产生的所有数据都会随之消失。这对于需要持久化存储数据的应用 (如数据库、日志系统、用户上传内容) 来说是不可接受的。为了解决这个问题&#xff0c;Docker 提供了多种数据持久化方案&…

前端视觉交互设计全解析:从悬停高亮到多维交互体系(含代码 + 图表)

在前端用户体验领域&#xff0c;视觉交互是连接用户与产品的 “隐形桥梁”—— 它通过可视化信号传递操作意图、反馈系统状态&#xff0c;直接决定用户对产品的感知。很多开发者对视觉交互的认知停留在 “鼠标悬停高亮”&#xff0c;但实际上&#xff0c;视觉交互是一个覆盖 “…

从零打造商业级LLMOps平台:开源项目LMForge详解,助力多模型AI Agent开发!

最近&#xff0c;我发现了一个超级实用的开源项目——LMForge-End-to-End-LLMOps-Platform-for-Multi-Model-Agents&#xff08;以下简称LMForge&#xff09;。这个项目是一个端到端的LLMOps&#xff08;Large Language Model Operations&#xff09;平台&#xff0c;专为多模型…

【C++练习】06.输出100以内的所有素数

目录输出100以内的所有素数方法1&#xff1a;基础判断法方法2&#xff1a;埃拉托斯特尼筛法&#xff08;效率更高&#xff09;方法3&#xff1a;优化版筛法&#xff08;只考虑奇数&#xff09;方法4&#xff1a;使用STL算法方法5&#xff1a;递归实现总结&#xff1a; 输出100以…

在开发中使用git rebase的场景

rebase介绍 一、背景 远程仓库有oh4w-dev和oh4k-dev两个分支&#xff0c;oh4k-dev是基于oh4w-dev开发到80%的代码新拉的分支&#xff1b;此后两条分支同步开发&#xff0c;当oh4k-dev开发完成&#xff0c;oh4w-dev还在开发阶段&#xff0c;oh4k-dev需要拉取到oh4w-dev自分出o…

TDengine 时序函数 NOW() 用户手册

TDengine NOW() 函数用户使用手册 目录 功能概述函数语法返回值说明技术特性使用场景及示例时间运算操作注意事项常见问题 功能概述 NOW() 函数是 TDengine 中的时间函数&#xff0c;用于获取客户端当前系统时间。该函数在时序数据库中特别有用&#xff0c;可以用于数据插入…

JavaWeb ——事务管理

文章目录事务管理事务回顾Spring事务管理事务进阶事务属性 - 回滚 rollbackFor事务属性 - 传播行为 propagationSpring框架第一大核心: IOC控制反转&#xff0c; 其第二大核心就是 AOP 面向切面编程 事务管理 事务回顾 Spring事务管理 # spring 事务管理日志 logging:level:org…

【跨国数仓迁移最佳实践8】MaxCompute Streaming Insert: 大数据数据流写业务迁移的实践与突破

本系列文章将围绕东南亚头部科技集团的真实迁移历程展开&#xff0c;逐步拆解 BigQuery 迁移至 MaxCompute 过程中的关键挑战与技术创新。本篇为第八篇&#xff0c;MaxCompute Streaming Insert&#xff1a; 大数据数据流写业务迁移的实践与突破。注&#xff1a;客户背景为东南…

2025-09-05 CSS4——浮动与定位

文章目录1 显示&#xff08;Display&#xff09;1.1 visibility:hidden1.2 display:none2 块和内联元素2.1 块元素2.2 内联元素2.3 改变元素的显示方式3 浮动&#xff08;Float&#xff09;3.1 float 属性3.2 clear 属性4 定位&#xff08;Position&#xff09;4.1 五种定位模式…