How to Build Windows Virtual Scanner and Feed Custom Images

Document Acquisition Process

Pre-requisites

  • Visual Studio 2017 or above
  • Qt 5.12.11 msvc2017
  • Test tools:

How to Build and Debug the TWAIN DS Sample

  1. Get the source code:
  1. Run twacker (32-bit/64-bit) application.
  2. In Visual Studio, set a breakpoint in DS_Entry() and then go to Debug > Attach to Process to find the twacker process.

Feeding Windows Virtual Scanner with Custom Images

{
"folder": "C:/Users/admin/Pictures/barcode"
}
{
"index": 0
}
#include "json.hpp"
using json = nlohmann::json;

#include <filesystem>
#include <fstream>
namespace fs = std::experimental::filesystem;

CScanner_FreeImage::CScanner_FreeImage()
{
...

char sourceConfig[PATH_MAX];
SSNPRINTF(sourceConfig, sizeof(sourceConfig), PATH_MAX, "%s%csource.json", szTWAIN_DS_DIR, PATH_SEPERATOR);
vector<string> images;

if (FILE_EXISTS(sourceConfig))
{
// Read the image folder from source.json
ifstream stream(sourceConfig);
json source;
stream >> source;
stream.close();

string imageFolder = source["folder"];

if (FILE_EXISTS(imageFolder.c_str()))
{
// Get the image index
string infoPath = imageFolder + PATH_SEPERATOR + "info.json";
ifstream infoStream(infoPath);
json info;
infoStream >> info;
infoStream.close();

int index = info["index"];

for (const auto& entry : fs::directory_iterator(imageFolder))
{
std::string path{ entry.path().u8string() };
string suffix = path.substr(path.length() - 4, 4);
// Get JPEG or PNG files
if (!suffix.compare(".jpg") || !suffix.compare(".png"))
{
images.push_back(path);
}
}

if (images.size() > 0)
{
if (index >= images.size()) index = 0;

// Set a custom image
SSNPRINTF(m_szSourceImagePath, sizeof(m_szSourceImagePath), PATH_MAX, images[index].c_str());

// Save image index to info.json
index += 1;
info["index"] = index;
std::ofstream stream(infoPath);
stream << info << std::endl;
stream.close();
}
}
}

// If there's no config file for custom image set, use the default image
if (images.size() == 0)
{
SSNPRINTF(m_szSourceImagePath, sizeof(m_szSourceImagePath), PATH_MAX, "%s%cTWAIN_logo.png", szTWAIN_DS_DIR, PATH_SEPERATOR);
}
...
}

Source Code

--

--

--

Manager of Dynamsoft Open Source Projects | Tech Lover

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

AWS Lambda | API Gateway | VPC | RDS | NodeJS | TypeORM

Create your VPC network

Code Refactoring VS Optimization

Media Center — File System

How to scrap Didier Deschamps email

Native Apps vs Hybrid Apps: How do You Choose One?

Shifting to Better Secrets in Kubernetes

Keep Your Local Machine Clean with Docker

Deploy Terraform with CI/CD pipeline

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
Xiao Ling

Xiao Ling

Manager of Dynamsoft Open Source Projects | Tech Lover

More from Medium

How to Optimize Parameters for Multiple QR Code Decoding in a Single Image with YOLO

How many iterates this for loop will run [for (double x = 10.0; x != 0.0; x -= 0.1)] ?

Jetson Nano Boot From USB/SSD Guid

Jetson Nano Boot From USB/SSD

Code/papeReview: ELMo