Created
August 7, 2014 18:57
-
-
Save banthar/77cd12ec4d40d4442124 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(®); | |
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