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
英文地址,請注明出處:http://en.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 五種定位模式…