Android 集成OpenCV

记录自己在学习使用OpenCV的过程 我使用的是4.10.0 版本

Android 集成OpenCV 步骤

  1. 下载OpenCV
  2. 新建工程
  3. 依赖OpenCV
  4. 初始化及逻辑处理

1、下载OpenCV 并解压到自己的电脑

官网 地址:https://opencv.org/releases/

个人地址:https://pan.baidu.com/s/19fPjLcV9Y7ZRL3-Uqy-XAQ

提取码: encv

2、新建工程

在Android Studio 中创建一个空的项目

步骤


3、依赖 OpenCV

a、集成OpenCV到工程


b、在app中添加依赖




至此app已依赖OpenCV

初始化及逻辑处理

1、初始化
java 复制代码
  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
        }
2、逻辑处理

本次为图片灰度处理的例子

本案例中使用了registerForActivityResult 跳转页面,因为StartActivityForResult 已过时

java 复制代码
  val getImage = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){result->
           if (result.resultCode == Activity.RESULT_OK) {
               val uri: Uri? = result.data?.data
               println(result.data)
               println(uri)
               bitmap = BitmapFactory.decodeStream(uri?.let { contentResolver.openInputStream(it) })
               imageView.setImageBitmap(bitmap)
           }
        }
        click(getImage)
java 复制代码
 fun click(getImage: ActivityResultLauncher<Intent>) {
        button.setOnClickListener{
            val intents = Intent(Intent.ACTION_GET_CONTENT).apply {
                type="image/*"
                addCategory(Intent.CATEGORY_OPENABLE)
            }
            getImage.launch(intents);
        }
        button2.setOnClickListener {
            if (bitmap == null){
                return@setOnClickListener;
            }
            var bit = bitmap!!.copy(Bitmap.Config.ARGB_8888, false);
            var src = Mat(bit?.height!!, bit?.width!!, CvType.CV_8UC3)
            Utils.bitmapToMat(bit,src);
            Imgproc.cvtColor(src,src,Imgproc.COLOR_BGR2GRAY);
            Utils.matToBitmap(src,bitmap);
            imageView.setImageBitmap(bitmap)
        }
    }

布局文件

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MissingConstraints"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:srcCompat="@tools:sample/avatars"
        android:scaleType="fitXY"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="加载图片"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginLeft="20dp"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="灰度化"
       app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginRight="20dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

至此本次图片灰度处理已经结束

相关推荐
_大学牲1 分钟前
全网爆火的豆包手机,到底是如何实现自动化的?一篇文章带你了解
android·人工智能·agent
fie88895 分钟前
基于BP神经网络和支持向量机实现风机故障诊断
人工智能·神经网络·支持向量机
serve the people5 分钟前
tensorflow 零基础吃透:RaggedTensor 在 Keras 和 tf.Example 中的实战用法 (补充)
人工智能·tensorflow·keras
老蒋新思维8 分钟前
创客匠人 2025 万人峰会深度:AI+IP 信任三角重构知识变现 —— 从单次成交到终身绑定的生态逻辑
大数据·网络·人工智能·tcp/ip·重构·创始人ip·创客匠人
乾元10 分钟前
从命令行到自动诊断:构建 AI 驱动的故障树与交互式排障机器人引言
运维·开发语言·网络·人工智能·华为·自动化
老蒋新思维11 分钟前
创客匠人峰会总结:私域 AI 化引爆知识变现 —— 创始人 IP 的智能增长新范式
网络·人工智能·网络协议·tcp/ip·重构·创始人ip·创客匠人
clarance201513 分钟前
ChatBI王者之争:ThoughtSpot、Databricks、Power BI等五大产品深度对决与选型指南
大数据·人工智能·信息可视化·数据挖掘·数据分析
lin625342214 分钟前
Android仿小米视频播放器的缩放滚轮
android·git·github