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>

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

相关推荐
豆芽81911 分钟前
图解YOLO(You Only Look Once)目标检测(v1-v5)
人工智能·深度学习·学习·yolo·目标检测·计算机视觉
逾非时23 分钟前
MySQL触法器
android·mysql·sqlserver
算力云34 分钟前
深度剖析!GPT-image-1 API 开放对 AI 绘画技术生态的冲击!
人工智能·openai图像生成模型·gpt-image-1
孤寂码农_defector38 分钟前
AI 人工智能模型:从理论到实践的深度解析⚡YQW · Studio ⚡【Deepseek】【Chat GPT】
人工智能
北上ing1 小时前
从FP32到BF16,再到混合精度的全景解析
人工智能·pytorch·深度学习·计算机视觉·stable diffusion
还是鼠鼠1 小时前
Android移动应用开发入门示例:Activity跳转界面
android·前端·gitee·android studio·android-studio
小奕同学A1 小时前
数字化技术的五个环节:大数据、云计算、人工智能、区块链、移动互联网
大数据·人工智能·云计算
Eric.Lee20211 小时前
数据集-目标检测系列- F35 战斗机 检测数据集 F35 plane >> DataBall
人工智能·算法·yolo·目标检测·计算机视觉
白熊1881 小时前
【计算机视觉】CV实践- 基于PaddleSeg的遥感建筑变化检测全解析:从U-Net 3+原理到工程实践
人工智能·计算机视觉
cmoaciopm2 小时前
Obsidian和Ollama大语言模型的交互过程
人工智能·语言模型