-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathnative_opencv.dart
49 lines (39 loc) · 1.37 KB
/
native_opencv.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import 'dart:ffi' as ffi;
import 'dart:io';
import 'package:ffi/ffi.dart';
// C function signatures
typedef _CVersionFunc = ffi.Pointer<Utf8> Function();
typedef _CProcessImageFunc = ffi.Void Function(
ffi.Pointer<Utf8>,
ffi.Pointer<Utf8>,
);
// Dart function signatures
typedef _VersionFunc = ffi.Pointer<Utf8> Function();
typedef _ProcessImageFunc = void Function(ffi.Pointer<Utf8>, ffi.Pointer<Utf8>);
// Getting a library that holds needed symbols
ffi.DynamicLibrary _openDynamicLibrary() {
if (Platform.isAndroid) {
return ffi.DynamicLibrary.open('libnative_opencv.so');
} else if (Platform.isWindows) {
return ffi.DynamicLibrary.open("native_opencv_windows_plugin.dll");
}
return ffi.DynamicLibrary.process();
}
ffi.DynamicLibrary _lib = _openDynamicLibrary();
// Looking for the functions
final _VersionFunc _version =
_lib.lookup<ffi.NativeFunction<_CVersionFunc>>('version').asFunction();
final _ProcessImageFunc _processImage = _lib
.lookup<ffi.NativeFunction<_CProcessImageFunc>>('process_image')
.asFunction();
String opencvVersion() {
return _version().toDartString();
}
void processImage(ProcessImageArguments args) {
_processImage(args.inputPath.toNativeUtf8(), args.outputPath.toNativeUtf8());
}
class ProcessImageArguments {
final String inputPath;
final String outputPath;
ProcessImageArguments(this.inputPath, this.outputPath);
}