This commit is contained in:
2021-11-17 14:57:56 -06:00
commit f17b64232d
177 changed files with 6639 additions and 0 deletions

8
native_opencv/.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
.DS_Store
.dart_tool/
.packages
.pub/
build/
include/

10
native_opencv/.metadata Normal file
View File

@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.
version:
revision: 0b8abb4724aa590dd0f429683339b1e045a1594d
channel: stable
project_type: plugin

View File

@@ -0,0 +1,3 @@
## 0.0.1
* TODO: Describe initial release.

1
native_opencv/LICENSE Normal file
View File

@@ -0,0 +1 @@
TODO: Add your license here.

14
native_opencv/README.md Normal file
View File

@@ -0,0 +1,14 @@
# native_opencv
A new flutter plugin project.
## Getting Started
This project is a starting point for a Flutter
[plug-in package](https://flutter.dev/developing-packages/),
a specialized package that includes platform-specific implementation code for
Android and/or iOS.
For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.

10
native_opencv/android/.gitignore vendored Normal file
View File

@@ -0,0 +1,10 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.cxx/
src/main/jniLibs/

View File

@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.4.1)
include_directories(../include)
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java4.so)
find_library(log-lib log)
add_library(native_opencv SHARED ../ios/Classes/native_opencv.cpp)
target_link_libraries(native_opencv lib_opencv ${log-lib})

View File

@@ -0,0 +1,23 @@
package io.flutter.plugins;
import io.flutter.plugin.common.PluginRegistry;
/**
* Generated file. Do not edit.
*/
public final class GeneratedPluginRegistrant {
public static void registerWith(PluginRegistry registry) {
if (alreadyRegisteredWith(registry)) {
return;
}
}
private static boolean alreadyRegisteredWith(PluginRegistry registry) {
final String key = GeneratedPluginRegistrant.class.getCanonicalName();
if (registry.hasPlugin(key)) {
return true;
}
registry.registrarFor(key);
return false;
}
}

View File

@@ -0,0 +1,59 @@
group 'com.example.native_opencv'
version '1.0-SNAPSHOT'
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.3'
}
}
rootProject.allprojects {
repositories {
google()
jcenter()
}
}
apply plugin: 'com.android.library'
android {
packagingOptions {
pickFirst 'lib/arm64-v8a/libopencv_java4.so'
pickFirst 'lib/armeabi-v7a/libopencv_java4.so'
pickFirst 'lib/x86/libopencv_java4.so'
pickFirst 'lib/x86_64/libopencv_java4.so'
}
compileSdkVersion 28
defaultConfig {
minSdkVersion 24
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
// Enabling exception, RTTI and setting C++ standard version
cppFlags '-frtti -fexceptions -std=c++11'
// Shared runtime for shared libraries
arguments "-DANDROID_STL=c++_shared"
}
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
lintOptions {
disable 'InvalidPackage'
}
}
dependencies {}

View File

@@ -0,0 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true

View File

@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip

View File

@@ -0,0 +1 @@
rootProject.name = 'native_opencv'

View File

@@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.native_opencv">
</manifest>

View File

@@ -0,0 +1,38 @@
package com.example.native_opencv;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
/** NativeOpencvPlugin */
public class NativeOpencvPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private MethodChannel channel;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "native_opencv");
channel.setMethodCallHandler(this);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else {
result.notImplemented();
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}

39
native_opencv/ios/.gitignore vendored Normal file
View File

@@ -0,0 +1,39 @@
.idea/
.vagrant/
.sconsign.dblite
.svn/
.DS_Store
*.swp
profile
DerivedData/
build/
GeneratedPluginRegistrant.h
GeneratedPluginRegistrant.m
.generated/
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
!default.pbxuser
!default.mode1v3
!default.mode2v3
!default.perspectivev3
xcuserdata
*.moved-aside
*.pyc
*sync/
Icon?
.tags*
/Flutter/Generated.xcconfig
/Flutter/flutter_export_environment.sh
opencv2.framework

View File

View File

@@ -0,0 +1,4 @@
#import <Flutter/Flutter.h>
@interface NativeOpencvPlugin : NSObject<FlutterPlugin>
@end

View File

@@ -0,0 +1,15 @@
#import "NativeOpencvPlugin.h"
#if __has_include(<native_opencv/native_opencv-Swift.h>)
#import <native_opencv/native_opencv-Swift.h>
#else
// Support project import fallback if the generated compatibility header
// is not copied when this plugin is created as a library.
// https://forums.swift.org/t/swift-static-libraries-dont-copy-generated-objective-c-header/19816
#import "native_opencv-Swift.h"
#endif
@implementation NativeOpencvPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
[SwiftNativeOpencvPlugin registerWithRegistrar:registrar];
}
@end

View File

@@ -0,0 +1,14 @@
import Flutter
import UIKit
public class SwiftNativeOpencvPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "native_opencv", binaryMessenger: registrar.messenger())
let instance = SwiftNativeOpencvPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
result("iOS " + UIDevice.current.systemVersion)
}
}

View File

@@ -0,0 +1,124 @@
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <chrono>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
#define IS_WIN32
#endif
#ifdef __ANDROID__
#include <android/log.h>
#endif
#ifdef IS_WIN32
#include <windows.h>
#endif
#if defined(__GNUC__)
// Attributes to prevent 'unused' function from being removed and to make it visible
#define FUNCTION_ATTRIBUTE __attribute__((visibility("default"))) __attribute__((used))
#elif defined(_MSC_VER)
// Marking a function for export
#define FUNCTION_ATTRIBUTE __declspec(dllexport)
#endif
using namespace cv;
using namespace std;
long long int get_now()
{
return chrono::duration_cast<std::chrono::milliseconds>(
chrono::system_clock::now().time_since_epoch())
.count();
}
void platform_log(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
#ifdef __ANDROID__
__android_log_vprint(ANDROID_LOG_VERBOSE, "ndk", fmt, args);
#elif defined(IS_WIN32)
char *buf = new char[4096];
std::fill_n(buf, 4096, '\0');
_vsprintf_p(buf, 4096, fmt, args);
OutputDebugStringA(buf);
delete[] buf;
#else
vprintf(fmt, args);
#endif
va_end(args);
}
// Avoiding name mangling
extern "C"
{
FUNCTION_ATTRIBUTE
const char *version()
{
return CV_VERSION;
}
FUNCTION_ATTRIBUTE
void process_image(char *inputImagePath, char *outputImagePath, double frame_rate)
{
platform_log("Starting the process");
long long start = get_now();
//Video Capture
VideoCapture cap = VideoCapture(inputImagePath);
platform_log("DURRRR %f",frame_rate);
const double scale = 1.0 / 16.0;
//Working Image Containers
Mat src;
Mat dst;
//Background sub
Mat fgMask;
auto subtractor = createBackgroundSubtractorMOG2(150, 100);
Size k_size = Size(1, 1);
//Centroid Variables
double posx = 0.0;
double posy = 0.0;
const double alpha = 0.995;
Moments mom;
double cX;
double cY;
//Drawing
const Scalar color = Scalar(0, 0, 255);
//Loop Management
while (true)
{
cap >> src;
if (src.empty())
{
break;
}
resize(src, src, Size(), scale, scale);
blur(src, dst, k_size);
subtractor->apply(src, fgMask);
mom = moments(fgMask);
if (mom.m00 != 0)
{
cX = mom.m10 / mom.m00;
cY = mom.m01 / mom.m00;
posx = posx * alpha + cX * (1.0 - alpha);
posy = posy * alpha + cY * (1.0 - alpha);
}
platform_log("Positions %f %f %f", posx, posy, mom.m00/(src.size[0] * src.size[1]* 255));
}
int evalInMillis = static_cast<int>(get_now() - start);
platform_log("Processing done in %dms\n", evalInMillis);
}
}

View File

@@ -0,0 +1,38 @@
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint native_opencv.podspec' to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'native_opencv'
s.version = '0.0.1'
s.summary = 'A new flutter plugin project.'
s.description = <<-DESC
A new flutter plugin project.
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Your Company' => 'email@example.com' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.platform = :ios, '8.0'
# Flutter.framework does not contain a i386 slice.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386 arm64' }
s.swift_version = '5.0'
# telling CocoaPods not to remove framework
s.preserve_paths = 'opencv2.framework'
# telling linker to include opencv2 framework
s.xcconfig = { 'OTHER_LDFLAGS' => '-framework opencv2' }
# including OpenCV framework
s.vendored_frameworks = 'opencv2.framework'
# including native framework
s.frameworks = 'AVFoundation'
# including C++ library
s.library = 'c++'
end

View File

@@ -0,0 +1,13 @@
import 'dart:async';
import 'package:flutter/services.dart';
class NativeOpencv {
static const MethodChannel _channel =
const MethodChannel('native_opencv');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
}

168
native_opencv/pubspec.lock Normal file
View File

@@ -0,0 +1,168 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.1"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
flutter:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
native_opencv_linux:
dependency: "direct main"
description:
path: "../native_opencv_linux"
relative: true
source: path
version: "0.0.1"
native_opencv_macos:
dependency: "direct main"
description:
path: "../native_opencv_macos"
relative: true
source: path
version: "0.0.1"
native_opencv_windows:
dependency: "direct main"
description:
path: "../native_opencv_windows"
relative: true
source: path
version: "0.0.1"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
sky_engine:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
source_span:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.10.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
sdks:
dart: ">=2.12.0 <3.0.0"
flutter: ">=1.20.0"

View File

@@ -0,0 +1,37 @@
name: native_opencv
description: A new flutter plugin project.
version: 0.0.1
publish_to: none
environment:
sdk: ">=2.12.0 <3.0.0"
flutter: ">=1.20.0"
dependencies:
flutter:
sdk: flutter
native_opencv_macos:
path: ../native_opencv_macos
native_opencv_linux:
path: ../native_opencv_linux
native_opencv_windows:
path: ../native_opencv_windows
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
plugin:
platforms:
android:
package: com.example.native_opencv
pluginClass: NativeOpencvPlugin
ios:
pluginClass: NativeOpencvPlugin
macos:
default_package: native_opencv_macos
linux:
default_package: native_opencv_linux
windows:
default_package: native_opencv_windows

View File

@@ -0,0 +1,23 @@
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:native_opencv/native_opencv.dart';
void main() {
const MethodChannel channel = MethodChannel('native_opencv');
TestWidgetsFlutterBinding.ensureInitialized();
setUp(() {
channel.setMockMethodCallHandler((MethodCall methodCall) async {
return '42';
});
});
tearDown(() {
channel.setMockMethodCallHandler(null);
});
test('getPlatformVersion', () async {
expect(await NativeOpencv.platformVersion, '42');
});
}