Environment: Android studio 2.2.3, OpenCV 2.4.9 For Android, Android 6 with api 23 for X86


一、File: build.gradle (app)

apply plugin: 'com.android.application'

android {
compileSdkVersion
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.unsw.jeff"
minSdkVersion
targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
} dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:23.1.1'
testCompile 'junit:junit:4.12'
compile project(':openCVLibrary249')
}

二、File: build.gradle (openCVLibrary249)

apply plugin: 'com.android.library'

android {
compileSdkVersion
buildToolsVersion "23.0.2" defaultConfig {
minSdkVersion
targetSdkVersion
} buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}

三、模拟器(支持本地摄像头):

模拟器中app摄像头权限打开:

Set --> app --> 摄像头权限Open

显示效果:

开发代码


一、布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id ="@+id/activity_main"
android:layout_width ="match_parent"
android:layout_height="match_parent" tools:context="com.example.unsw.jeff.MainActivity"> <org.opencv.android.JavaCameraView
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/java_camera_view"
/> </RelativeLayout>

二、功能实现核心:JavaCameraView

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);   javaCameraView = (JavaCameraView)findViewById(R.id.java_camera_view);
  javaCameraView.setVisibility(SurfaceView.VISIBLE);
  javaCameraView.setCvCameraViewListener(this); // 因为MainActivity实现了接口:CameraBridgeViewBase.CvCameraViewListener2
}
    @Override
protected void onPause() {
super.onPause();
if (javaCameraView != null) {
javaCameraView.disableView();
}
} @Override
protected void onDestroy() {
super.onDestroy();
if (javaCameraView != null) {
javaCameraView.disableView();
}
} @Override
protected void onResume() {
super.onResume();
if (OpenCVLoader.initDebug()) {
Log.i(TAG, "OpenCV loaded successfully.");
mLoaderCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
else {
Log.i(TAG, "OpenCV not loaded.");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallBack);
}
}

其他Activity生命周期

    @Override
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(height, width, CvType.CV_8UC4);
} @Override
public void onCameraViewStopped() {
mRgba.release();
} @Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();      // By opencv api in java here, do what you want to do.
return mRgba;
}

补充:

OpenCV4Android开发实录(1):移植OpenCV3.3.0库到Android Studio  

[Android Studio] Using Java to call OpenCV的更多相关文章

  1. 关于Android Studio中使用jni进行opencv配置开发环境的要素秘诀

    使用jni进行opencv开发可以快速地将PC端的opencv代码移植到手机上,但是如何在android studio下进行配置,网上几乎找不到教程,大多都是eclipse下使用mk文件的方法,找不到 ...

  2. android studio Error:java.lang.OutOfMemoryError: GC overhead limit exceeded

    android studio Error:java.lang.OutOfMemoryError: GC overhead limit exceeded 在app下的build.gradle中找到and ...

  3. Android Studio中配置及使用OpenCV示例

    Android Studio配置及使用OpenCV 前言:最近在做项目移植,项目较大,在Eclipse中配置的Jni及OpenCV环境没任何问题,但是迁移到Studio中就问题一大堆,网上也找了一些资 ...

  4. Android Studio 运行java程序

    当我们装了Android Studio 学习安卓开发的时候,难免会要学习java,这时候,难道在重新装一个编译器吗?不需要,我们直接用 Android Studio 就可以. 1.新建一个空项目,选择 ...

  5. Mac OS下Android Studio的Java not found问题,androidfound

    Android Studio正式版已经发布一段时间了,使用Mac版的Android Studio可能与遇到Java not found:Android Studio was unable to fin ...

  6. Android Studio 中java 文件报错红色J

    用常用的方法清除Android Studio的缓存然后重启,"File" -> "Invalidate Cashes / Restart" -> & ...

  7. [Android Studio] Using NDK to call OpenCV

    NDK才是Android开发通向超高薪之路.(这句话,似乎四年前有云) 难点在于常用的non-free module (sift and surf) unsw@unsw-UX303UB$ pwd /h ...

  8. 利用Android studio开发Java工程

    1. 新建项目 新建项目肯定是去new,但到底是new project还是new module是一个问题.在这解释一下,如果new project的话是新建一个工程,相当于新建一个工作区,工程中可以有 ...

  9. 问题解决Android studio遇到 java.lang.OutOfMemoryError: GC app:transformClassesWithDexForDebug解决方法 以及gradle优化

    http://blog.csdn.net/xiaoxing0828/article/details/52242090

随机推荐

  1. shell脚本中:1>&2 2>&1 &>filename重定向的含义和区别

    当初在shell中, 看到">&1"和">&2"始终不明白什么意思.经过在网上的搜索得以解惑.其实这是两种输出. 在 shell 程 ...

  2. Drawing line on a click on ZedGraph Pane

    https://stackoverflow.com/questions/12422398/drawing-line-on-a-click-on-zedgraph-pane public Form1() ...

  3. python BeautifulSoup的简单使用

    官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc/ 参考:https://www.cnblogs.com/yupeng/p/336203 ...

  4. p中不能包含div

    一句话:有些块元素不可以包含另一些块元素 ,DTD中规定了块级元素是不能放在P里;P标签内包含块元素时,它会先结束自己,比如:<*p><*div>测试p包含div<*/d ...

  5. React页面隐藏#

    将 hashHistory 改为 browserHistory 路由用到的,可以在routes.jsx上把hashHistory 改成browserHistory https://github.com ...

  6. MySQL 复习笔记

    本文内容 SQL 语句 创建数据库 创建数据表 数据完整性约束 四种基本字符类型说明 SQL 基本语句 类型转换函数 日期函数 数学函数 字符串函数 合并结果集 union CASE 函数用法 IF ...

  7. Android性能优化-减小APK大小

    前言 用户通常会避免下载比较大的应用,特别是连接到2G和3G网络,或者按流量收费的设备.这篇文章描述了如何减小apk的大小,帮助你让更多的用户下载你的app. 一 理解APK的结构 在讨论如何减小ap ...

  8. LEAPMOTION开发UI专题(1)

    非常 抱歉的是,之前说的LEAP/UI框架设计可能仅仅有两篇 由于个人时间实在是不同意 这个问题假设展开去写的话 那么说写本书都是不为过的 且由于内容改动非常是杂乱 所以我第一篇文章用来介绍LEAP预 ...

  9. 常见网站CSS样式重置

    腾讯 1 2 3 4 5 6 7 8 9 body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea, ...

  10. CoffeeScript简介 <二>

    集合与迭代 .. 与 ... 先看例子: arr = ["a1", "a2", "a3", "a4", "a5 ...