-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
357 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
library cv; | ||
|
||
import 'dart:ffi' as ffi; | ||
|
||
import 'package:ffi/ffi.dart'; | ||
|
||
import '../core/base.dart'; | ||
import '../core/mat.dart'; | ||
import '../core/vec.dart'; | ||
import '../opencv.g.dart' as cvg; | ||
|
||
class WeChatQRCode extends CvStruct<cvg.WeChatQRCode> { | ||
WeChatQRCode._(super.ptr) : super.fromPointer() { | ||
finalizer.attach(this, ptr.cast()); | ||
} | ||
|
||
factory WeChatQRCode.empty() { | ||
final p = calloc<cvg.WeChatQRCode>(); | ||
cvRun(() => CFFI.WeChatQRCode_New(p)); | ||
return WeChatQRCode._(p); | ||
} | ||
|
||
/// Initialize the WeChatQRCode. | ||
/// It includes two models, which are packaged with caffe format. | ||
/// Therefore, there are prototxt and caffe models (In total, four paramenters). | ||
/// | ||
/// https://docs.opencv.org/4.x/d5/d04/classcv_1_1wechat__qrcode_1_1WeChatQRCode.html#a9c0dc4c37646a1a051340d6b0916f388 | ||
factory WeChatQRCode([ | ||
String detectorPrototxtPath = "", | ||
String detectorCaffeModelPath = "", | ||
String superResolutionPrototxtPath = "", | ||
String superResolutionCaffeModelPath = "", | ||
]) { | ||
return cvRunArena<WeChatQRCode>((arena) { | ||
final p = calloc<cvg.WeChatQRCode>(); | ||
final dp = detectorPrototxtPath.toNativeUtf8(allocator: arena).cast<ffi.Char>(); | ||
final dm = detectorCaffeModelPath.toNativeUtf8(allocator: arena).cast<ffi.Char>(); | ||
final srp = superResolutionPrototxtPath.toNativeUtf8(allocator: arena).cast<ffi.Char>(); | ||
final srm = superResolutionCaffeModelPath.toNativeUtf8(allocator: arena).cast<ffi.Char>(); | ||
cvRun(() => CFFI.WeChatQRCode_NewWithParams(dp, dm, srp, srm, p)); | ||
return WeChatQRCode._(p); | ||
}); | ||
} | ||
|
||
/// Both detects and decodes QR code. To simplify the usage, there is a only API: detectAndDecode. | ||
/// https://docs.opencv.org/4.x/d5/d04/classcv_1_1wechat__qrcode_1_1WeChatQRCode.html#a27c167d2d58e5ee4418fd3a9ed5876cc | ||
(List<String>, VecMat points) detectAndDecode(InputArray img, [VecMat? points]) { | ||
final p = calloc<cvg.VecMat>(); | ||
final rval = calloc<cvg.VecVecChar>(); | ||
cvRun(() => CFFI.WeChatQRCode_DetectAndDecode(ptr, img.ref, p, rval)); | ||
final vec = VecVecChar.fromPointer(rval); | ||
final points = VecMat.fromPointer(p); | ||
return (vec.asStringList(), points); | ||
} | ||
|
||
/// https://docs.opencv.org/4.x/d5/d04/classcv_1_1wechat__qrcode_1_1WeChatQRCode.html#abf807138abc2626c159abd3e9a80e791 | ||
double get scaleFactor { | ||
return cvRunArena<double>((arena) { | ||
final p = calloc<ffi.Float>(); | ||
cvRun(() => CFFI.WeChatQRCode_GetScaleFactor(ptr, p)); | ||
return p.value; | ||
}); | ||
} | ||
|
||
/// set scale factor QR code detector use neural network to detect QR. | ||
/// Before running the neural network, the input image is pre-processed by scaling. | ||
/// By default, the input image is scaled to an image with an area of 160000 pixels. | ||
/// The scale factor allows to use custom scale the input image: | ||
/// width = scaleFactor*width height = scaleFactor*width | ||
/// | ||
/// scaleFactor valuse must be > 0 and <= 1, | ||
/// otherwise the scaleFactor value is set to -1 and | ||
/// use default scaled to an image with an area of 160000 pixels. | ||
/// | ||
/// https://docs.opencv.org/4.x/d5/d04/classcv_1_1wechat__qrcode_1_1WeChatQRCode.html#a084f9aa8693fa0a62c43dd10d2533ab8 | ||
set scaleFactor(double scaleFactor) { | ||
cvRun(() => CFFI.WeChatQRCode_SetScaleFactor(ptr, scaleFactor)); | ||
} | ||
|
||
static final finalizer = OcvFinalizer<cvg.WeChatQRCodePtr>(CFFI.addresses.WeChatQRCode_Close); | ||
|
||
@override | ||
List<int> get props => [ptr.address]; | ||
|
||
@override | ||
cvg.WeChatQRCode get ref => ptr.ref; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "wechat_qrcode.h" | ||
#include <vector> | ||
|
||
CvStatus WeChatQRCode_New(WeChatQRCode *qrcode) | ||
{ | ||
BEGIN_WRAP | ||
*qrcode = {new cv::wechat_qrcode::WeChatQRCode()}; | ||
END_WRAP | ||
} | ||
CvStatus WeChatQRCode_NewWithParams(const char *detector_prototxt_path, const char *detector_caffe_model_path, | ||
const char *super_resolution_prototxt_path, | ||
const char *super_resolution_caffe_model_path, WeChatQRCode *qrcode) | ||
{ | ||
BEGIN_WRAP | ||
*qrcode = {new cv::wechat_qrcode::WeChatQRCode(detector_prototxt_path, detector_caffe_model_path, | ||
super_resolution_prototxt_path, | ||
super_resolution_caffe_model_path)}; | ||
END_WRAP | ||
} | ||
void WeChatQRCode_Close(WeChatQRCode *self) | ||
{ | ||
delete self->ptr; | ||
self->ptr = nullptr; | ||
delete self; | ||
self = nullptr; | ||
} | ||
CvStatus WeChatQRCode_DetectAndDecode(WeChatQRCode *self, Mat img, VecMat *points, VecVecChar *rval) | ||
{ | ||
BEGIN_WRAP | ||
std::vector<cv::Mat> pts; | ||
|
||
auto strings = self->ptr->detectAndDecode(*img.ptr, pts); | ||
*points = {new std::vector<cv::Mat>(pts)}; | ||
|
||
auto cstrings = new std::vector<std::vector<char>>(); | ||
cstrings->reserve(strings.size()); | ||
for (int i = 0; i < strings.size(); i++) { | ||
auto s = strings.at(i); | ||
cstrings->push_back(std::vector<char>(s.begin(), s.end())); | ||
} | ||
*rval = {cstrings}; | ||
|
||
END_WRAP | ||
} | ||
CvStatus WeChatQRCode_GetScaleFactor(WeChatQRCode *self, float *rval) | ||
{ | ||
BEGIN_WRAP | ||
*rval = self->ptr->getScaleFactor(); | ||
END_WRAP | ||
} | ||
CvStatus WeChatQRCode_SetScaleFactor(WeChatQRCode *self, float scale_factor) | ||
{ | ||
BEGIN_WRAP | ||
self->ptr->setScaleFactor(scale_factor); | ||
END_WRAP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Created by Rainyl. | ||
Licensed: Apache 2.0 license. Copyright (c) 2024 Rainyl. | ||
*/ | ||
#pragma once | ||
#ifndef WECHAT_QRCODE_H | ||
#define WECHAT_QRCODE_H | ||
|
||
#include "core/core.h" | ||
|
||
#ifdef __cplusplus | ||
#include <opencv2/opencv.hpp> | ||
#include <opencv2/wechat_qrcode.hpp> | ||
extern "C" { | ||
#endif | ||
|
||
// Main Content Start | ||
#ifdef __cplusplus | ||
CVD_TYPEDEF(cv::wechat_qrcode::WeChatQRCode, WeChatQRCode) | ||
#else | ||
CVD_TYPEDEF(void, WeChatQRCode) | ||
#endif | ||
|
||
CVD_TYPEDEF_PTR(WeChatQRCode) | ||
|
||
CvStatus WeChatQRCode_New(WeChatQRCode *qrcode); | ||
CvStatus WeChatQRCode_NewWithParams(const char *detector_prototxt_path, const char *detector_caffe_model_path, | ||
const char *super_resolution_prototxt_path, | ||
const char *super_resolution_caffe_model_path, WeChatQRCode *qrcode); | ||
void WeChatQRCode_Close(WeChatQRCode *self); | ||
CvStatus WeChatQRCode_DetectAndDecode(WeChatQRCode *self, Mat img, VecMat *points, VecVecChar *rval); | ||
CvStatus WeChatQRCode_GetScaleFactor(WeChatQRCode *self, float *rval); | ||
CvStatus WeChatQRCode_SetScaleFactor(WeChatQRCode *self, float scale_factor); | ||
|
||
// Main Content End | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.