void QRScanner::init(char *raw, int width,int height){
	// create a reader
	ImageScanner scanner;

	// configure the reader
	scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

	myImage = new Image(width, height, "Y800", raw, width * height);

	// scan the image for barcodes
	numLines = scanner.scan(*myImage);
}
/**
 * This is a (modified) test program written by Michael Young
 * (https://github.com/ayoungprogrammer/WebcamCodeScanner). It was modified to
 * work with the Raspicam.
 */
int main(int argc, char* argv[])
{
	PiCamera cam; // open the video camera no. 0
	ImageScanner scanner;  
	scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);  

	namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

	while (1)
	{
		Mat frame = cam.Snap();
		Mat grey;
		cvtColor(frame,grey,CV_BGR2GRAY);

		int width = frame.cols;  
		int height = frame.rows;  
		uchar *raw = (uchar *)grey.data;  
		// wrap image data  
		zbar::Image image(width, height, "Y800", raw, width * height);  
		// scan the image for barcodes  
		int n = scanner.scan(image);  
		// extract results  
		for(Image::SymbolIterator symbol = image.symbol_begin();  
				symbol != image.symbol_end();  
				++symbol) {  
			vector<Point> vp;  
			// do something useful with results  
			cout << "decoded " << symbol->get_type_name()  << " symbol \"" << symbol->get_data() << '"' <<" "<< endl;  
			int n = symbol->get_location_size();  
			for(int i=0;i<n;i++){  
				vp.push_back(Point(symbol->get_location_x(i),symbol->get_location_y(i))); 
			}  
			RotatedRect r = minAreaRect(vp);  
			Point2f pts[4];  
			r.points(pts);  
			for(int i=0;i<4;i++){  
				line(frame,pts[i],pts[(i+1)%4],Scalar(255,0,0),3);  
			}  
		}  

		imshow("MyVideo", frame); //show the frame in "MyVideo" window

		if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
		{
			cout << "esc key is pressed by user" << endl;
			break; 
		}
	}
	return 0;

}
void test(char *s) {
    freopen(s, "r", stdin);
    int h, w, nb = 0;
    scanf("%d%d", &h, &w);
    __board.resize(h);
    for (int i = 0; i < h; i ++) {
        cin >> __board[i];
        for (int j = 0; j < w; j ++)
            if (__board[i][j] == '1')
                nb ++;
    }
    __scanned.resize(h, false);
    ImageScanner ct;
    vector<string> result = ct.restore(h, w, nb, 0);
}
Exemple #4
0
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ImageScanner scanner;
    //scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
    scanner.set_config(ZBAR_QRCODE, ZBAR_CFG_ENABLE, 1);

    QImage qimage;
    bool imgLoaded = qimage.load("C:/Users/emi/Dropbox/projekts/qt/zBar_32bit/codePictures/qrCode3.png");

    if(imgLoaded == false)
    {
        qDebug() << "error loading image";
    }

    QImage convertedImage = qimage.convertToFormat(QImage::Format_Grayscale8,Qt::MonoOnly);

    int width = convertedImage.width();
    int height= convertedImage.height();


    unsigned char * data = convertedImage.bits();

    Image image(width, height, "Y800" , data, width*height);
  // Image image(width, height, "GRAY" , data, width*height);

    // scan the image for barcodes
    int n = scanner.scan(image);


    // extract results
    for(Image::SymbolIterator symbol = image.symbol_begin();
        symbol != image.symbol_end();
        ++symbol)
    {
        // do something useful with results
       qDebug() << "decoded " << QString::fromStdString(symbol->get_type_name())
             << " symbol \"" << QString::fromStdString(symbol->get_data()) << '"' << endl;
    }

    // clean up
    image.set_data(NULL, 0);

}
Exemple #5
0
int main (int argc, char **argv)
{
    unsigned long nowTime = getCurrentTime();
    // std::cout<<"nowTime: "<<getCurrentTime()<<"\n";
    if(argc < 2) return(1);

    // create a reader
    ImageScanner scanner;

    // // configure the reader
    scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
    /* obtain image data */
    int width = 1920, height = 1080;
    char *raw = (char*)malloc(width*height);
    get_yuv_data(argv[1], width, height, raw);
    // wrap image data
    Image image(width, height, "Y800", raw, width * height);

    // scan the image for barcodes
    int n = scanner.scan(image);
    if(0==n||-1==n)
    {
      printf("no symbols were found or -1 if an error occurs");
      return -1;
    }
    // extract results
    for(Image::SymbolIterator symbol = image.symbol_begin();
        symbol != image.symbol_end();
        ++symbol)
    {
        // do something useful with results
        cout << "decoded " << symbol->get_type_name()
             << " symbol \"" << symbol->get_data() << '"' << endl;
    }

    // clean up
    image.set_data(NULL, 0);
    free(raw);
    std::cout<<getCurrentTime()-nowTime<<"\n";
    return(0);
}
Exemple #6
0
void code_qr::decoder(Mat& img_qr, string& message){
	ImageScanner scanner;
      	scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
	Mat img;
	img_qr.copyTo(img);
	//if( !img.data ){
          	//return 0;
      	//}
	Mat imgout;
      	cvtColor(img,imgout,CV_GRAY2RGB);
      	int width = img.cols;
      	int height = img.rows;
   	uchar *raw = (uchar *)img.data;
	Image image(width, height, "Y800", raw, width * height);
	int n = scanner.scan(image);
	for(Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol) {
		message = symbol->get_data();	     	
		//cout << "decoded " << symbol->get_type_name()<< " symbol \"" << symbol->get_data() << '"' <<" "<< endl;
		
	}
	image.set_data(NULL, 0);

}
QString ZBarReaderTest::decodeIterative(const QImage &image)
{
    Image zbarImg(image.width(), image.height(), "Y800", image.bits(), image.bytesPerLine() * image.height());
    QString line;
    ImageScanner scanner;
    scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
    scanner.scan(zbarImg);

    SymbolSet s = scanner.get_results();
    int resSize = s.get_size();
    if (resSize == 0) {

        int fib1 = 1, fib2 = 1;
        int fib;
        for (fib = fib1 + fib2; fib < 11; fib = fib1 + fib2) {
            int roiWidth =  (1. / fib) * image.width(), roiHeight = (1. / fib) * image.height();
            for (int x = 0; x < image.width(); x += 50) {
                for (int y = 0; y < image.height(); y += 50) {
//                qDebug () << "roiWidth" << roiWidth << "roiHeight" << roiHeight;
                    QImage img = image.copy(x, y, roiWidth, roiHeight);
                    Image i(img.width(), img.height(), "Y800", img.bits(), img.bytesPerLine() * img.height());
                    scanner.scan(i);
                }
            }
            int temp = fib1;
            fib1 = fib;
            fib2 = temp;
        }
    }

    SymbolSet s2 = scanner.get_results();
    resSize = s2.get_size();
    if (resSize > 0) {
        mTotalRead++;
        for (SymbolIterator symbol = scanner.get_results().symbol_begin(); symbol != scanner.get_results().symbol_end(); ++symbol) {
            if (QString::compare("QR-Code ", QString(symbol->get_type_name().data()).trimmed()))
                line += " | " + QString(symbol->get_data().data()).replace("\n", "<br>");
        }
    }
    return line;
}
QString ZBarReaderTest::decode(const QImage &image)
{
    Image zbarImg(image.width(), image.height(), "Y800", image.bits(), image.bytesPerLine() * image.height());

    QString line;
    ImageScanner scanner;
    scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
    scanner.scan(zbarImg);

    SymbolSet s = scanner.get_results();
    int resSize = s.get_size();
    if (resSize > 0) {
        mTotalRead++;
        for (SymbolIterator symbol = scanner.get_results().symbol_begin(); symbol != scanner.get_results().symbol_end(); ++symbol) {
            if (QString::compare("QR-Code ", QString(symbol->get_type_name().data()).trimmed()))
                line += " | " + QString(symbol->get_data().data()).replace("\n", "<br>");
        }
    }
    return line;
}
Exemple #9
0
int main(int argc, char **argv) {
    int cam_idx = 0;

    if (argc == 2) {
        cam_idx = atoi(argv[1]);
    }

    VideoCapture cap(cam_idx);
    if (!cap.isOpened()) {
        cerr << "Could not open camera." << endl;
        exit(EXIT_FAILURE);
    }
    //cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    //cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

    namedWindow("captured", CV_WINDOW_AUTOSIZE);
    
    // Create a zbar reader
    ImageScanner scanner;
    
    // Configure the reader
    scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

    for (;;) {
        // Capture an OpenCV frame
        cv::Mat frame, frame_grayscale;
        cap >> frame;

        // Convert to grayscale
        cvtColor(frame, frame_grayscale, CV_BGR2GRAY);

        // Obtain image data
        int width = frame_grayscale.cols;
        int height = frame_grayscale.rows;
        uchar *raw = (uchar *)(frame_grayscale.data);

        // Wrap image data
        Image image(width, height, "Y800", raw, width * height);

        // Scan the image for barcodes
        //int n = scanner.scan(image);
        scanner.scan(image);

        // Extract results
        int counter = 0;
        for (Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol) {
            time_t now;
            tm *current;
            now = time(0);
            current = localtime(&now);

            // do something useful with results
            cout    << "[" << current->tm_hour << ":" << current->tm_min << ":" << setw(2) << setfill('0') << current->tm_sec << "] " << counter << " "
                    << "decoded " << symbol->get_type_name()
                    << " symbol \"" << symbol->get_data() << '"' << endl;

            //cout << "Location: (" << symbol->get_location_x(0) << "," << symbol->get_location_y(0) << ")" << endl;
            //cout << "Size: " << symbol->get_location_size() << endl;
            
            // Draw location of the symbols found
            if (symbol->get_location_size() == 4) {
                //rectangle(frame, Rect(symbol->get_location_x(i), symbol->get_location_y(i), 10, 10), Scalar(0, 255, 0));
                line(frame, Point(symbol->get_location_x(0), symbol->get_location_y(0)), Point(symbol->get_location_x(1), symbol->get_location_y(1)), Scalar(0, 255, 0), 2, 8, 0);
                line(frame, Point(symbol->get_location_x(1), symbol->get_location_y(1)), Point(symbol->get_location_x(2), symbol->get_location_y(2)), Scalar(0, 255, 0), 2, 8, 0);
                line(frame, Point(symbol->get_location_x(2), symbol->get_location_y(2)), Point(symbol->get_location_x(3), symbol->get_location_y(3)), Scalar(0, 255, 0), 2, 8, 0);
                line(frame, Point(symbol->get_location_x(3), symbol->get_location_y(3)), Point(symbol->get_location_x(0), symbol->get_location_y(0)), Scalar(0, 255, 0), 2, 8, 0);
            }
            
            // Get points
            /*for (Symbol::PointIterator point = symbol.point_begin(); point != symbol.point_end(); ++point) {
                cout << point << endl;
            } */
            counter++;
        }

        // Show captured frame, now with overlays!
        imshow("captured", frame);
                                                                                                                                                          
        // clean up
        image.set_data(NULL, 0);
        
        waitKey(30);
    }

    return 0;
}
Exemple #10
0
JNIEXPORT void JNICALL Java_com_example_qr_MainActivity_nativeDetect(
		JNIEnv * jenv, jclass, jlong imageGray) {
	LOGD(
			"Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect enter");
	try {
		vector<Rect> RectFaces;
		//       ((DetectionBasedTracker*)thiz)->process(*((Mat*)imageGray));
		//       ((DetectionBasedTracker*)thiz)->getObjects(RectFaces);
		//       vector_Rect_to_Mat(RectFaces, *((Mat*)faces));

		Mat img = *((Mat*) imageGray);
		Mat imgout;
		cvtColor(img, imgout, CV_GRAY2RGB);
		int width = img.cols;
		int height = img.rows;
		uchar *raw = (uchar *) img.data;
//		uchar *raw = (uchar *) imageGray;
		// wrap image data
		Image image(width, height, "Y800", raw, width * height);
		// scan the image for barcodes
		ImageScanner scanner;
		scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

		int n = scanner.scan(image);
		// extract results
		for (Image::SymbolIterator symbol = image.symbol_begin();
				symbol != image.symbol_end(); ++symbol) {
			vector<Point> vp;
			// do something useful with results
			cout << "decoded " << symbol->get_type_name() << " symbol \""
					<< symbol->get_data() << '"' << " " << endl;
			int n = symbol->get_location_size();
			for (int i = 0; i < n; i++) {
				vp.push_back(
						Point(symbol->get_location_x(i),
								symbol->get_location_y(i)));

			}
			RotatedRect r = minAreaRect(vp);
			Point2f pts[4];
			r.points(pts);
			for (int i = 0; i < 4; i++) {
				line(imgout, pts[i], pts[(i + 1) % 4], Scalar(255, 0, 0), 3);
			}
			cout << "Angle: " << r.angle << endl;
		}
		imshow("imgout.jpg", imgout);
		// clean up
		image.set_data(NULL, 0);
		waitKey();

	} catch (cv::Exception& e) {
		LOGD("nativeCreateObject caught cv::Exception: %s", e.what());
		jclass je = jenv->FindClass("org/opencv/core/CvException");
		if (!je)
			je = jenv->FindClass("java/lang/Exception");
		jenv->ThrowNew(je, e.what());
	} catch (...) {
		LOGD("nativeDetect caught unknown exception");
		jclass je = jenv->FindClass("java/lang/Exception");
		jenv->ThrowNew(je,
				"Unknown exception in JNI code DetectionBasedTracker.nativeDetect()");
	}
	LOGD(
			"Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect exit");
}
Exemple #11
0
int main(int argc, char *argv[])
{
  AmConfig *config = new AmConfig();
  int enable_send_ipaddr = 0;
  unsigned short server_msg_port = 4848;//hard default code here
  unsigned short local_msg_port = 8484;//hard default code here
  int i = 0;
  int dgram_socket = -1;
  struct sockaddr_in dest_addr;
  int get_url = 0;
  char server_ip_addr_buffer[64] = {0};
  char local_ip_addr_buffer[INET6_ADDRSTRLEN + 4] = {0};//plus 4 for safe
  char local_rtsp_url_buffer[128] = {0};

  //If you have different config, please set it here
  config->set_vin_config_path(SIMPLE_CAM_VIN_CONFIG);
  //config->set_vout_config_path    (vout_config_path);
  //config->set_vdevice_config_path (vdevice_config_path);
  //config->set_record_config_path  (record_config_path);

  //simple check arguments
  if (argc > 1) {
    for (i = 0; i < argc; ++ i) {
      if (!strcmp("--enable-msgport", argv[i])) {
        enable_send_ipaddr = 1;
        fprintf(stdout, "\tparams: enable msg port.\n");
      } else if (!strcmp("--msgport", argv[i])) {
        ++ i;
        server_msg_port = atoi(argv[i]);
        fprintf(stdout, "\tparams: msg port %hu.\n", server_msg_port);
      } else if (!strcmp("--localmsgport", argv[i])) {
        ++ i;
        local_msg_port = atoi(argv[i]);
        fprintf(stdout, "\tparams: local msg port %hu.\n", local_msg_port);
      }
    }
  }

  if (config && config->load_vdev_config()) {
    VDeviceParameters *vdevConfig = config->vdevice_config();
    YBufferFormat yBuffer = {0};
    ImageScanner zbarScanner;
    int retVal = 0;
    int count = 0;

    if (vdevConfig) {
      AmSimpleCam simplecam(vdevConfig);
      for (uint32_t i = 0; i < vdevConfig->vin_number; ++ i) {
        simplecam.set_vin_config(config->vin_config(i), i);
      }
      for (uint32_t i = 0; i < vdevConfig->vout_number; ++ i) {
        simplecam.set_vout_config(config->vout_config(i), i);
      }
      simplecam.set_encoder_config(config->encoder_config());
      for (uint32_t i = 0; i < vdevConfig->stream_number; ++ i) {
        simplecam.set_stream_config(config->stream_config(i), i);
      }
      do {
        if (simplecam.get_y_data(&yBuffer)) {
          Image image(yBuffer.y_width, yBuffer.y_height, "Y800",
                      yBuffer.y_addr, yBuffer.y_width * yBuffer.y_height);
          if (AM_LIKELY((retVal = zbarScanner.scan(image)) > 0)) {
            for (Image::SymbolIterator symbol = image.symbol_begin();
                 symbol != image.symbol_end(); ++ symbol) {
              fprintf(stdout, "\n%s\n\n", symbol->get_data().c_str());
              if (enable_send_ipaddr) {
                get_url = 1;
                strncpy(server_ip_addr_buffer, symbol->get_data().c_str(),
                        sizeof(server_ip_addr_buffer) - 1);
                server_ip_addr_buffer[sizeof(server_ip_addr_buffer) - 1] = 0x0;
              }
            }
          } else if (retVal == 0) {
            NOTICE("No symbold found!");
          } else if (retVal < 0) {
            ERROR("Zbar scan error!");
          }
        } else {
          ERROR("Failed to get Y data!");
        }
        ++ count;
      } while ((retVal <= 0) && (count < 20));
      if (count >= 20) {
        ERROR("Failed to resolve symbol!");
      }
    } else {
      ERROR("Faild to get VideoDevice's configurations!");
    }
    delete config;
  } else {
    ERROR("Failed to load configurations!");
  }

  //parse host ipaddress, and port, have
  if (enable_send_ipaddr && get_url) {
    dgram_socket = _setup_datagram_socket(INADDR_ANY, local_msg_port, false);
    if (_build_addr_from_url(&dest_addr, server_ip_addr_buffer,
                             server_msg_port) < 0) {
      ERROR("build addr fail, url %s\n", server_ip_addr_buffer);
    } else {
      //send rtsp url to server
      _get_local_ip_address(local_ip_addr_buffer);
      _buildup_rtsp_url(local_ip_addr_buffer, local_rtsp_url_buffer,
                        sizeof(local_rtsp_url_buffer) - 1, "stream1+stream2");
      _post_msg_to_server(local_rtsp_url_buffer, &dest_addr, dgram_socket);
    }
    if (dgram_socket >= 0) {
      close(dgram_socket);
    }
  }

  return 0;
}
int main(int argc, char *argv[])
{
  if (argc < 2 || argc > 3) {
    NOTICE("Usage: %s <YUV filename>", argv[0]);
    return 0;
  }
  int status;
  FILE *fp = NULL;
  AmConfig *config = new AmConfig();
  char command[128];

  snprintf(command, sizeof(command),
           "ImageServerDaemon");
  status = system(command);
  if (WEXITSTATUS(status)) {
    ERROR("Failed to run ImageServerDaemon");
  }

  if (NULL == (fp = fopen(argv[1],"wb+"))) {
    ERROR("Failed to open %s: %s", argv[1], strerror(errno));
    return -1;
  }

  //If you have different config, please set it here
  config->set_vin_config_path(SIMPLE_CAM_VIN_CONFIG);
  //config->set_vout_config_path    (vout_config_path);
  //config->set_vdevice_config_path (vdevice_config_path);
  //config->set_record_config_path  (record_config_path);

  if (config && config->load_vdev_config()) {
    VDeviceParameters *vdevConfig = config->vdevice_config();
    if (vdevConfig) {
      AmSimpleCam simplecam(vdevConfig);
      for (uint32_t i = 0; i < vdevConfig->vin_number; ++ i) {
        simplecam.set_vin_config(config->vin_config(i), i);
      }
      for (uint32_t i = 0; i < vdevConfig->vout_number; ++ i) {
        simplecam.set_vout_config(config->vout_config(i), i);
      }
      simplecam.set_encoder_config(config->encoder_config());
      for (uint32_t i = 0; i < vdevConfig->stream_number; ++ i) {
        simplecam.set_stream_config(config->stream_config(i), i);
      }
      if (AM_LIKELY(simplecam.enter_preview())) {
        YBufferFormat yBuffer = {0};
        ImageScanner zbarScanner;
        int retVal = 0;
        int count = 0;

        do {
          if (simplecam.get_y_data(&yBuffer)) {
            Image image(yBuffer.y_width, yBuffer.y_height, "Y800",
                        yBuffer.y_addr, yBuffer.y_width * yBuffer.y_height);
            if (AM_LIKELY((retVal = zbarScanner.scan(image)) > 0)) {
              for (Image::SymbolIterator symbol = image.symbol_begin();
                   symbol != image.symbol_end(); ++ symbol) {
                fprintf(stdout, "\n%s\n\n", symbol->get_data().c_str());
              }
              snprintf(command, sizeof(command),
                       "aplay -vv -fdat ./OK.wav");
              status = system(command);
              if (WEXITSTATUS(status)) {
                ERROR("Failed to play wav file");
              }
              size_t written = fwrite(yBuffer.y_addr, sizeof(uint8_t),
                                      yBuffer.y_height * yBuffer.y_width, fp);

              NOTICE("written data: %d Bytes, width: %d, height: %d.",
                     written, yBuffer.y_width, yBuffer.y_height);
            } else if (retVal == 0) {
              NOTICE("No symbold found!");
            } else if (retVal < 0) {
              ERROR("Zbar scan error!");
            }
          } else {
            ERROR("Failed to get Y data!");
          }
          ++ count;
        } while ((retVal <= 0) && (count < 20));
        if (count >= 20) {
          snprintf(command, sizeof(command),
                   "aplay -vv -fdat ./ERROR.wav");
          status = system(command);
          if (WEXITSTATUS(status)) {
            ERROR("Failed to play wav file");
          }
          ERROR("Failed to resolve symbol!");
          size_t written = fwrite(yBuffer.y_addr, sizeof(uint8_t),
                                  yBuffer.y_height * yBuffer.y_width, fp);

          NOTICE("written data: %d Bytes, width: %d, height: %d.",
                 written, yBuffer.y_width, yBuffer.y_height);
        }
      } else {
        ERROR("Failed to enter preview!");
      }
    } else {
      ERROR("Faild to get VideoDevice's configurations!");
    }
    delete config;
  } else {
    ERROR("Failed to load configurations!");
  }
  snprintf(command, sizeof(command),
           "ImageServerDaemon -k");
  status = system(command);
  if (WEXITSTATUS(status)) {
    ERROR("Failed to kill ImageServerDaemon");
  }
  fclose(fp);
  return 0;
}