Reading Matrix Codes from Several Images Using Asynchronous Processing Code Snippet¶
This code snippet shows how to read data matrix codes asynchronously from three separate images. The code in this snippet is valid for C++11 and newer.
C++¶
#include <atomic>
#include <chrono>
#include <thread>
#include <eVision/EasyMatrixCode/EasyMatrixCode2/EMatrixCodeReader.h>
// create a subroutine that reads the codes from an image
void Read(eVision::EImageBW8& image,
eVision::EasyMatrixCode2::EMatrixCodeReader& reader,
std::vector<eVision::EasyMatrixCode2::EMatrixCode>& codes,
std::atomic<bool>& finished)
{
// read the codes in this image
reader.Read(image);
// extract the results
codes = reader.GetReadResults();
// notify that the reader has finished
finished = true;
}
using namespace eVision;
namespace EMC2 = eVision::EasyMatrixCode2;
// Prepare three images
EImageBW8 img1, img2, img3;
// Prepare three matrix code readers
EMC2::EMatrixCodeReader reader1, reader2, reader3;
// Prepare three vectors of matrix code instances
std::vector<EMC2::EMatrixCode> codes1, codes2, codes3;
// Prepare three booleans to track the thread progress
std::atomic<bool> finished1, finished2, finished3;
// Load the three images
img1.Load("image1.bmp");
img2.Load("image2.bmp");
img3.Load("image3.bmp");
// Set the progress trackers to false
finished1 = false;
finished2 = false;
finished3 = false;
// Launch three threads to read the codes in each image
// the threads will run in the background.
std::thread thr1 = std::thread([&] { Read(img1, reader1, codes1, finished1); });
std::thread thr2 = std::thread([&] { Read(img2, reader2, codes2, finished2); });
std::thread thr3 = std::thread([&] { Read(img3, reader3, codes3, finished3); });
// Wait until one of the threads has finished
while (!(finished1 || finished2 || finished3))
std::this_thread::sleep_for(std::chrono::milliseconds(5));
// Here, we manually stop all code readers, they will stop processing
// even if they have not yet found the codes in the image
reader1.StopProcess();
reader2.StopProcess();
reader3.StopProcess();
// wait for the threads to completely finish before continuing
thr1.join();
thr2.join();
thr3.join();
C#¶
using System.Threading;
using eVision;
using EMC2 = eVision.EasyMatrixCode2;
static void Read(ref EImageBW8 image, ref EMC2.EMatrixCodeReader reader, ref EMC2.EMatrixCode[] codes, ref bool finished)
{
// read the codes in this image
reader.Read(image);
// extract the results
codes = reader.ReadResults;
// notify that the reader has finished
finished = true;
}
// Prepare three images
EImageBW8 img1 = new EImageBW8();
EImageBW8 img2 = new EImageBW8();
EImageBW8 img3 = new EImageBW8();
// Prepare three matrix code readers
EMC2.EMatrixCodeReader reader1 = new EMC2.EMatrixCodeReader();
EMC2.EMatrixCodeReader reader2 = new EMC2.EMatrixCodeReader();
EMC2.EMatrixCodeReader reader3 = new EMC2.EMatrixCodeReader();
// Prepare three vectors of matrix code instances
EMC2.EMatrixCode[] codes1 = null;
EMC2.EMatrixCode[] codes2 = null;
EMC2.EMatrixCode[] codes3 = null;
// Prepare three Booleans to track the thread progress
bool finished1 = false;
bool finished2 = false;
bool finished3 = false;
// load the images
img1.Load("image1.bmp");
img2.Load("image2.bmp");
img3.Load("image3.bmp");
// Launch three threads to read the codes in each image
Thread thr1 = new Thread(() => Read(ref img1, ref reader1, ref codes1, ref finished1));
Thread thr2 = new Thread(() => Read(ref img2, ref reader2, ref codes2, ref finished2));
Thread thr3 = new Thread(() => Read(ref img3, ref reader3, ref codes3, ref finished3));
// Start the threads, they will run in the background.
thr1.Start();
thr2.Start();
thr3.Start();
// Wait until one of the threads has finished
while (!(finished1 || finished2 || finished3))
Thread.Sleep(5);
// Here, we manually stop all code readers, they will stop processing
// even if they have not yet found the codes in the image
reader1.StopProcess();
reader2.StopProcess();
reader3.StopProcess();
// wait for the threads to completely finish before continuing
thr1.Join();
thr2.Join();
thr3.Join();
Python¶
from threading import Thread
import time
import eVision
from eVision import EasyMatrixCode2 as EMC2
## create a subroutine that reads the codes from an image
def Read(image: eVision.EImageBW8,
reader: EMC2.EMatrixCodeReader,
codes: list[EMC2.EMatrixCode],
finished: list[bool]):
# read the codes in the image
results = reader.Read(image)
# extract the results
codes.clear()
for res in results:
codes.append(res)
# notify that the reader has finished
finished[0] = True
# Prepare three images
img1 = eVision.EImageBW8()
img2 = eVision.EImageBW8()
img3 = eVision.EImageBW8()
# Prepare three matrix code readers
reader1 = EMC2.EMatrixCodeReader()
reader2 = EMC2.EMatrixCodeReader()
reader3 = EMC2.EMatrixCodeReader()
# Prepare three vectors of matrix code instances
codes1 = []
codes2 = []
codes3 = []
# Prepare three Booleans to track the thread progress
finished1 = [False]
finished2 = [False]
finished3 = [False]
# load the images
img1.Load("image1.bmp")
img2.Load("image2.bmp")
img3.Load("image3.bmp")
# Launch three threads to read the codes in each image
thr1 = Thread(target=Read, args=(img1, reader1, codes1, finished1))
thr2 = Thread(target=Read, args=(img2, reader2, codes2, finished2))
thr3 = Thread(target=Read, args=(img3, reader3, codes3, finished3))
# Start the threads, they will run in the background.
thr1.start()
thr2.start()
thr3.start()
# Wait until one of the threads has finished
while not (finished1[0] or finished2[0] or finished3[0]):
time.sleep(0.1)
# Here, we manually stop all code readers, they will stop processing
# even if they have not yet found the codes in the image
reader1.StopProcess()
reader2.StopProcess()
reader3.StopProcess()
# wait for the threads to completely finish before continuing
thr1.join()
thr2.join()
thr3.join()