Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ to implement Flutter Windows plug-in

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use C++ to achieve Flutter Windows plug-in". In daily operation, I believe many people have doubts about how to use C++ to achieve Flutter Windows plug-in. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to use C++ to achieve Flutter Windows plug-in". Next, please follow the editor to study!

How to integrate C++ barcode SDK into Flutter Windows plug-in

The directory structure of Windows plug-in project created by Flutter is as follows:

Bin / DynamsoftBarcodeReaderx64.dllinclude / flutter_barcode_sdk / flutter_barcode_sdk_plugin.h / barcode_manager.h / DynamsoftBarcodeReader.h / DynamsoftCommon.hlib / DBRx64.libCMakeLists.txtflutter_barcode_sdk_plugin.cpp

CMakeLists.txt is the configuration file for CMake.

Flutter_barcode_sdk_plugin.cpp is used to implement the C++ code logic of the plug-in.

DynamsoftBarcodeReaderx64.dll,DBRx64.lib,DynamsoftBarcodeReader.h, as well as DynamsoftCommon.h, are available from C++ SDK.

For convenience, the implementation of barcode interface calls is placed in barcode_manager.h.

The interfaces and parameters passed from Dart to C++ will be resolved in HandleMethodCall ():

Void FlutterBarcodeSdkPlugin::HandleMethodCall (const flutter::MethodCall & method_call, std::unique_ptr result) {const auto * arguments = std::get_if (method_call.arguments ()) If (method_call.method_name (). Compare ("getPlatformVersion") = = 0) {} else if (method_call.method_name (). Compare ("setLicense") = 0) {} else if (method_call.method_name (). Compare ("decodeFile") = = 0) {} else if (method_call.method_name (). Compare ("decodeFileBytes") = = 0) {} else if (method_call.method_name () .compare ("decodeImageBuffer") = = 0) {} else {result- > NotImplemented () }}}

Get the parameter and convert it to C++ type: string,int,vector:

Std::string filename;auto filename_it = arguments- > find (EncodableValue ("filename")); if (filename_it! = arguments- > end ()) {filename = std::get (filename_it- > second);} std::vector bytes;auto bytes_it = arguments- > find (EncodableValue ("bytes")); if (bytes_it! = arguments- > end ()) {bytes = std::get (bytes_it- > second);} int width = 0auto filename width_it = arguments- > find (EncodableValue (width)) If (width_it! = arguments- > end ()) {width = std::get (width_it- > second);}

The returned result needs to be encapsulated into the type defined by Flutter:

EncodableList results;result- > Success (results)

Next, the decoding and result encapsulation are implemented in barcode_manager.h.

Three decoding interfaces are defined here:

EncodableList DecodeFile (const char * filename) {EncodableList out; int ret = reader- > DecodeFile (filename, ""); if (ret = = DBRERR_FILE_NOT_FOUND) {printf ("Error code% d.% s\ n", ret, CBarcodeReader::GetErrorString (ret)); return out;} return WrapResults ();} EncodableList DecodeFileBytes (const unsigned char * bytes, int size) {reader- > DecodeFileInMemory (bytes, size, "") Return WrapResults ();} EncodableList DecodeImageBuffer (const unsigned char * buffer, int width, int height, int stride, int format) {ImagePixelFormat pixelFormat = IPF_BGR_888; switch (format) {case 0: pixelFormat = IPF_GRAYSCALED; break; case 1: pixelFormat = IPF_ARGB_8888; break;} reader- > DecodeBuffer (buffer, width, height, stride, pixelFormat, "") Return WrapResults ();}

Obtain the decoding result and package it into map and list of Flutter:

EncodableList WrapResults () {EncodableList out; TextResultArray * results = NULL; reader- > GetAllTextResults (& results); if (results- > resultsCount = = 0) {printf ("No barcode found.\ n"); CBarcodeReader::FreeTextResults (& results);} for (int index = 0; index)

< results->

ResultsCount; index++) {EncodableMap map; map [EncodableValue ("format")] = results- > results [index]-> barcodeFormatString; map [EncodableValue ("text")] = results- > results [index]-> barcodeText; map [EncodableValue ("x1")] = results- > results [index]-> localizationResult- > x1; map [EncodableValue ("y1")] = results- > results [index]-> localizationResult- > y1 Map [EncodableValue ("x2")] = results- > results [index]-> localizationResult- > x2; map [EncodableValue ("y2")] = results- > results [index]-> localizationResult- > y2; map [EncodableValue ("x3")] = results- > results [index]-> localizationResult- > x3; map [EncodableValue ("y3")] = results- > results [index]-> localizationResult- > y3; map [EncodableValue ("x4")] = results- > results [index]-> localizationResult- > x4 Map [EncodableValue ("y4")] = results- > results [index]-> localizationResult- > y4; out.push_back (map);} CBarcodeReader::FreeTextResults (& results); return out;}

At this point, the C++ code for the Windows plug-in is complete. The final step is to configure the CMakeLists.txt file.

Link to C++ library:

Link_directories ("${PROJECT_SOURCE_DIR} / lib/") target_link_libraries (${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin "DBRx64")

Package dynamic link libraries:

Set (flutter_barcode_sdk_bundled_libraries "${PROJECT_SOURCE_DIR} / bin/" PARENT_SCOPE)

This is an important step. If it is not packaged, the application will not be able to run because of the lack of DLL.

Realization of Windows desktop bar code recognition program with Flutter

With plug-ins, you can quickly implement desktop code scanning applications.

Create a new project and add dependencies in pubspec.yaml:

Dependencies: flutter: sdk: flutter flutter_barcode_sdk:

Create a SDK object:

Class _ DesktopState extends State {String _ platformVersion = 'Unknown'; final _ controller = TextEditingController (); String _ barcodeResults =''; FlutterBarcodeSdk _ barcodeReader; bool _ isValid = false; String _ file =''; @ override void initState () {super.initState (); initPlatformState (); initBarcodeSDK ();} Future initBarcodeSDK () async {_ barcodeReader = FlutterBarcodeSdk () / / Get 30-day FREEE trial license from https://www.dynamsoft.com/customer/license/trialLicense?product=dbr await _ barcodeReader.setLicense ('LICENSE-KEY');}}

Use TextField,Image,MaterialButton and Text to build UI:

@ overrideWidget build (BuildContext context) {return MaterialApp (home: Scaffold (appBar: AppBar (title: const Text ('Dynamsoft Barcode Reader'),), body: Column (children: [Container (height: 100, child: Row (children: [Text (_ platformVersion, style: TextStyle (fontSize: 14) Color: Colors.black)]),), TextField (controller: _ controller, decoration: InputDecoration (labelText: 'Input an image path', errorText: _ isValid? Null: 'File not exists',),), Expanded (child: SingleChildScrollView (child: Column (children: [getDefaultImage (), Text (_ barcodeResults, style: TextStyle (fontSize: 14, color: Colors.black) ),],), Container (height: 100, child: Row (mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [MaterialButton (child: Text ('Decode Barcode')) TextColor: Colors.white, color: Colors.blue, onPressed: () async {if (_ controller.text.isEmpty) {setState (() {_ isValid = false) _ barcodeResults ='; _ file =';}); return;} File file = File (_ controller.text) If (! file.existsSync ()) {setState (() {_ isValid = false; _ barcodeResults ='; _ file =';}); return } else {setState (() {_ isValid = true; _ file = _ controller.text;});} Uint8List bytes = await file.readAsBytes () List results = await _ barcodeReader.decodeFileBytes (bytes); setState (() {_ barcodeResults = getBarcodeResults (results);}) }),]),),]),),);}

The picture displays the picture in the resource kit by default. If the entered picture is valid, display the entered picture:

Widget getDefaultImage () {if (_ controller.text.isEmpty | |! _ isValid) {return Image.asset ('images/default.png');} else {return Image.file (File (_ file));}}

To use resource pictures, you need to add to pubspec.yaml:

Flutter: assets:-images/default.png

Finally, run the program:

Flutter run-d windows here, on "how to use C++ to achieve Flutter Windows plug-in" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report