Coder Social home page Coder Social logo

android-opencv-demo's Introduction

Android Studio使用OpenCV

1. 配置开发环境

  1. 下载OpenCV安卓SDK开发包

  2. as 新建普通的工程,导入opencv模块

    File->New->Import Module 找到

    将解压后的sdk下的 OpenCV-android-sdk/sdk/java 目录

    1562733889227

导入后的默认是作为application应用的,文件夹图标右下角是一个绿色点的,因为下面app要将这个导入的模块作为依赖,所以需要将它变成library库

打开导入后的模块的build.grade文件,修改

apply plugin: 'com.android.appdaimalication

apply plugin: 'com.android.library'

将导入的opencv4模块设置为app的依赖(Dependencies)

打开工程结构,选择app的Dependencies,添加Module Dependency,选择刚刚导入的opencv4

1562734230922

  1. 复制so库

    在app目录下新建jniLibs文件夹

    复制 解压后的SDK OpenCV-android-sdk/sdk/native/libs下的全部文件夹到jniLibs目录下

    1562734457542

这里可以按需复制

打开app模块的build.gradle文件,添加下面的代码到android节点下

1562734577340

测试,视频分解

用VideoCaputer 读取视频,并提取每一帧保存为图片

在Activity里引入加载so库

static{    System.loadLibrary("opencv_java4");}

resume函数里(这里是参考官方例子的,在onCreate里初始化一次应该也是可以的)

   if(!OpenCVLoader.initDebug())
        {
        Toast.makeText(this,"init success",Toast.LENGTH_SHORT).show();
        }
        else
        {
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }

提取帧核心代码

 @Override
    protected String doInBackground(String... strings) {


        VideoCapture video = new VideoCapture(strings[0]);

        Mat frame = new Mat();

        int index = 0;
        while(video.read(frame))
        {
            File out = new File(this.outPath,String.format("%d.png",index));
            Imgcodecs.imwrite(out.getAbsolutePath(),frame);
            index+=1;

            publishProgress(new ExtraMessage(0,index,out.getAbsolutePath()));

        }

        return null;
    }

android-opencv-demo's People

Contributors

formatfa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.