Skip to content

Instantly share code, notes, and snippets.

@banthar
Created August 7, 2014 18:57
Show Gist options
  • Save banthar/77cd12ec4d40d4442124 to your computer and use it in GitHub Desktop.
Save banthar/77cd12ec4d40d4442124 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
#include <dmtx.h>
#include <opencv2/opencv.hpp>
void decode(const cv::Mat& image){
int width = image.cols;
int height = image.rows;
cv::Mat greyMat(width, height, CV_8UC1);
cvtColor(image, greyMat, CV_BGR2GRAY);
DmtxImage* img = dmtxImageCreate(greyMat.data, greyMat.cols, greyMat.rows, DmtxPack8bppK);
DmtxDecode* dec = dmtxDecodeCreate(img, 1);
DmtxTime timeout = dmtxTimeAdd(dmtxTimeNow(), 100);
DmtxRegion* reg = dmtxRegionFindNext(dec, &timeout);
if(reg != NULL) {
DmtxMessage* msg = dmtxDecodeMatrixRegion(dec, reg, DmtxUndefined);
if(msg != NULL) {
std::cout << msg->output << std::endl;
return;
}
dmtxRegionDestroy(&reg);
dmtxMessageDestroy(&msg);
}
dmtxImageDestroy(&img);
dmtxDecodeDestroy(&dec);
}
int main(int argc, char*argv[]) {
CvCapture* capture = cvCaptureFromCAM( 0 );
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 320 );
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 240 );
if( capture == nullptr ) {
return -1;
}
cv::namedWindow( "result", 1 );
while( true ) {
std::cout << "\n";
cv::Mat frame = cvQueryFrame( capture );
cv::imshow( "result", frame );
cv::Mat grey;
cv::cvtColor(frame, grey, CV_BGR2GRAY);
cv::Mat bw;
cv::threshold(grey, bw, 64, 255, cv::THRESH_BINARY);
cv::imshow( "bw", bw );
std::vector<std::string> codes;
cv::findDataMatrix(bw, codes);
decode(frame);
for( auto i = codes.begin(); i != codes.end(); ++i) {
std::cout << *i << '\n';
}
if(cvWaitKey(1)!=-1){
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment