Rust Programming with Dynamsoft Barcode Reader

Xiao Ling
Xiao Ling
Oct 11, 2018 · 2 min read

A few days ago, I accepted a new challenge of creating a simple command line barcode reader using Rust and Dynamsoft Barcode Reader SDK. Rust is a system programming language similar to C++. The learning process did not go through smoothly as I expected. It is not as easy as learning other high-level programming languages such as Java and Python. In this article, I share my experience of learning and using Rust.

Prerequisites

Building Rust Barcode Reader on Windows

Project initialization

Create a new project:

In main.rs, get the input file name from arguments:

Build and run the app:

Bridging Rust and native methods

To simplify the invoking process, create bridge.h and bridge.c.

To link the native library and build the bridge, create build.rs in the project root:

Add the configuration to Cargo.toml:

Why not call native methods directly? It is complicated. If you do this, you have to define all C structures in Rust. The next step is to generate Rust FFI bindings based on the bridge.h file.

Converting C header files to Rust FFI bindings

Use bindgen to convert bridge.h to bridge.rs:

When I ran the above command line the first time, I got the following error message:

To fix the issue, install rustfmt-preview:

We can automate the process by adding bindgen as a build dependency in Cargo.toml:

Then add the following code to build.rs:

Automatically copy *.dll files to the output directory after building the project

On Windows, we use .lib files for building projects and use .dll files for running apps. To make the program run, we have to copy DLL files to the output directory.

The first step is to get the path of the output folder:

Copy DLL files:

Here is the final build.rs:

Build and run Rust barcode reader

Call the barcode detection function in main.rs:

Build the app:

Clean the build:

Run the app:

Image for post
Image for post

References

Source Code

https://github.com/yushulx/rust-barcode


Originally published at www.codepool.biz on October 11, 2018.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store