├── .DS_Store ├── Makefile ├── README.md ├── main ├── main.cpp └── main.o /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayoungprogrammer/WebcamCodeScanner/c287eece9e6a7963f99ff32e8ccd38465b1a1634/.DS_Store -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | ARCH=-arch x86_64 3 | 4 | 5 | INCLUDE_FLAGS=-I/usr/local/include 6 | CFLAGS=-c -Wall $(INCLUDE_FLAGS) 7 | LDFLAGS= -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lzbar 8 | 9 | SOURCES = main.cpp 10 | OBJECTS=$(SOURCES:.cpp=.o) 11 | 12 | EXECUTABLE=main 13 | 14 | all : $(SOURCES) $(EXECUTABLE) 15 | 16 | $(EXECUTABLE) : $(OBJECTS) Makefile 17 | $(CC) $(ARCH) $(LDFLAGS) $(OBJECTS) -o $@ 18 | 19 | .cpp.o: 20 | $(CC) $(CFLAGS) $< -o $@ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | WebcamCodeScanner 2 | ================= 3 | 4 | Scan and read QR / Barcodes in real time 5 | 6 | [Blog Post](http://blog.ayoungprogrammer.com/2014/04/real-time-qr-code-bar-code-detection.html) 7 | -------------------------------------------------------------------------------- /main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayoungprogrammer/WebcamCodeScanner/c287eece9e6a7963f99ff32e8ccd38465b1a1634/main -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace cv; 7 | using namespace std; 8 | using namespace zbar; 9 | 10 | //g++ main.cpp /usr/local/include/ /usr/local/lib/ -lopencv_highgui.2.4.8 -lopencv_core.2.4.8 11 | 12 | int main(int argc, char* argv[]) 13 | { 14 | VideoCapture cap(0); // open the video camera no. 0 15 | 16 | // cap.set(CV_CAP_PROP_FRAME_WIDTH,800); 17 | // cap.set(CV_CAP_PROP_FRAME_HEIGHT,640); 18 | 19 | if (!cap.isOpened()) // if not success, exit program 20 | { 21 | cout << "Cannot open the video cam" << endl; 22 | return -1; 23 | } 24 | 25 | 26 | ImageScanner scanner; 27 | scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1); 28 | 29 | double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video 30 | double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video 31 | 32 | cout << "Frame size : " << dWidth << " x " << dHeight << endl; 33 | 34 | namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo" 35 | 36 | while (1) 37 | { 38 | Mat frame; 39 | 40 | bool bSuccess = cap.read(frame); // read a new frame from video 41 | 42 | if (!bSuccess) //if not success, break loop 43 | { 44 | cout << "Cannot read a frame from video stream" << endl; 45 | break; 46 | } 47 | 48 | Mat grey; 49 | cvtColor(frame,grey,CV_BGR2GRAY); 50 | 51 | int width = frame.cols; 52 | int height = frame.rows; 53 | uchar *raw = (uchar *)grey.data; 54 | // wrap image data 55 | Image image(width, height, "Y800", raw, width * height); 56 | // scan the image for barcodes 57 | int n = scanner.scan(image); 58 | // extract results 59 | for(Image::SymbolIterator symbol = image.symbol_begin(); 60 | symbol != image.symbol_end(); 61 | ++symbol) { 62 | vector vp; 63 | // do something useful with results 64 | cout << "decoded " << symbol->get_type_name() << " symbol \"" << symbol->get_data() << '"' <<" "<< endl; 65 | int n = symbol->get_location_size(); 66 | for(int i=0;iget_location_x(i),symbol->get_location_y(i))); 68 | } 69 | RotatedRect r = minAreaRect(vp); 70 | Point2f pts[4]; 71 | r.points(pts); 72 | for(int i=0;i<4;i++){ 73 | line(frame,pts[i],pts[(i+1)%4],Scalar(255,0,0),3); 74 | } 75 | //cout<<"Angle: "<