Пример #1
0
    void Frame::FindKeypoints(const cv::Mat& img)
    {
        std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
        scanimg = img;
        int kPcount = 0;

        zbar::ImageScanner scanner;
        scanner.set_config( zbar::ZBAR_NONE,  zbar::ZBAR_CFG_ENABLE, 1);

        Image qrimage(scanimg.cols, scanimg.rows, "Y800", scanimg.data, scanimg.cols* scanimg.rows);

        std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();

        if( scanner.scan(qrimage) > 0){
            for(Image::SymbolIterator symbol = qrimage.symbol_begin();symbol !=
                                                                      qrimage.symbol_end();
                ++symbol)
            {
                kPcount++;
                for(int k = 0; k < 4; k++)if(symbol->get_location_size()==4){

                        keyFeature kfeature;

                        cv::KeyPoint kp;
                        kp.pt.x = (float)symbol->get_location_x(k);
                        kp.pt.y = (float)symbol->get_location_y(k);

                        kfeature.kp = kp;
                        kfeature.marker_id = k;
                        kfeature.code_id =  stoi(symbol->get_data());
                        frameKeyFeatures.push_back(kfeature);

                        //std::cout << "locations are "  <<  symbol->get_location_x(k) <<" "<<symbol->get_location_y(k)<<std::endl;
                        cv::Point sympos =  cv::Point( symbol->get_location_x(k), symbol->get_location_y(k));
                        cv::circle( scanimg, sympos, 4, cv::Scalar( 0, 0, 255 ), 3, 8 );
                    }


                std::string out =  "the id of code is " + symbol->get_data();
                //std::cout << out<< std::endl;
                //cv::imshow("123", scanimg);
            }

            std::chrono::steady_clock::time_point t3 = std::chrono::steady_clock::now();

            double ttrack1 = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
            double ttrack2 = std::chrono::duration_cast<std::chrono::microseconds>(t3 - t2).count();
            double ttrack3 = std::chrono::duration_cast<std::chrono::microseconds>(t3 - t1).count();
            //std::cout <<"ttrack1 "<< ttrack1 <<"us ttrack2 "<< ttrack2 <<"us ttrack3 is "<< ttrack3 <<"us"<< std::endl;
            isKeyframe = true;
        }
    }
Пример #2
0
void QRCodeScanner::NewFrameSampled(void *buffer, int width, int height, long size)
{
	Image img(width, height, "YUY2", buffer, size);
	Image converted(img.convert(Y800));

	int result = scanner->scan(converted);
	if(result == 0) lastResult = std::string();

	for (Image::SymbolIterator symbol = converted.symbol_begin(); symbol != converted.symbol_end(); ++symbol) {
		if(symbol->get_data() != lastResult)
		{
			lastResult = symbol->get_data();
			ShellExecuteA(NULL, "open", lastResult.c_str(), NULL, NULL, SW_SHOWNORMAL);
		}
	}
}
Пример #3
0
tResult cQrPlayer::ProcessVideo(adtf::IMediaSample* pISample)
{
	RETURN_IF_POINTER_NULL(pISample);
//  creating new temporary variables
	adtf_util::cImage oImage;	
	const tVoid* l_pSrcBuffer;

//  receiving data from inputsample, and saving to oImage
	if (IS_OK(pISample->Lock(&l_pSrcBuffer)))
	{
			oImage.SetBits((tUInt8*) l_pSrcBuffer, &m_sInputFormat);
			
			pISample->Unlock(l_pSrcBuffer);
	}

//  convert adtf-image to greyscaled image which is used by zbar
	RETURN_IF_FAILED(oImage.Convert(&m_sProcessFormat));	

//  creates image which is used by the QR Scanner   
	Image image(m_sInputFormat.nWidth, m_sInputFormat.nHeight, "Y800", (unsigned char*)oImage.GetBitmap(), m_sInputFormat.nWidth * m_sInputFormat.nHeight);

//  scan image for codes and check for errors		
	int n =  m_qrScanner.scan(image);
	if (n== -1) LOG_ERROR(cString::Format("QR Player: Error in QRScanner"));	
	
//  extract results to console
    for(Image::SymbolIterator symbol = image.symbol_begin();
        symbol != image.symbol_end();
        ++symbol) {
        //print only new results
        if (m_lastQRCode!=symbol->get_data())
            {
            m_lastQRCode=symbol->get_data();            
            }
        ProcessCommand(cString(symbol->get_data().c_str()));
    }
    
    // clean up
    image.set_data(NULL, 0);		

	RETURN_NOERROR;
}
Пример #4
0
/**
 * 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;

}
Пример #5
0
string QRScanner::getQRCode()
{
    stringstream out;

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

    return  out.str();
}
Пример #6
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);

}
Пример #7
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);
}
Пример #8
0
void QrTracker::track(Mat frame, int &size_x, int &size_y, string &data, Point2f &p1, Point2f &p2, Point2f &p3, Point2f &p4)
{
    Mat frame_grayscale;
    cvtColor(frame, frame_grayscale, COLOR_BGR2HSV);

    // wrap image
    int width = frame_grayscale.cols;
    int height = frame_grayscale.rows;
    uchar *raw = (uchar *)(frame_grayscale.data);
    Image image(width, height, "Y800", raw, width * height);

    //Scan the image for QRcodes
    scanner.scan(image);
    for (Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol)
    {
        //Size
        size_x = symbol->get_location_x(2) - symbol->get_location_x(1);
        size_y = symbol->get_location_y(1) - symbol->get_location_y(0);

        //Data
        data = symbol->get_data().c_str();

        //Points
        if (symbol->get_location_size() == 4)
        {
            p1 = Point2f(symbol->get_location_x(0), symbol->get_location_y(0));
            p2 = Point2f(symbol->get_location_x(1), symbol->get_location_y(1));
            p3 = Point2f(symbol->get_location_x(2), symbol->get_location_y(2));
            p4 = Point2f(symbol->get_location_x(3), symbol->get_location_y(3));
        }
        else
        {
            p1 = Point2f(-1,-1);
            p2 = Point2f(-1,-1);
            p3 = Point2f(-1,-1);
            p4 = Point2f(-1,-1);
        }
    }
}
Пример #9
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);

}
Пример #10
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;
}
Пример #11
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");
}
Пример #12
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;
}
Пример #13
0
bool AmQrcodeReader::qrcode_read(const char *result_path)
{
  FILE *fp = NULL;
  bool ret = true;
  AmConfig *config = NULL;
  VDeviceParameters *vdev_config = NULL;
  AmSimpleCam *simple_cam = NULL;

  do {
    if (AM_UNLIKELY(NULL == (fp = fopen(result_path, "w+")))) {
      ERROR("Failed to open %s: %s", result_path, strerror(errno));
      ret = false;
      break;
    }

    if (AM_UNLIKELY(NULL == (config = new AmConfig()))) {
      ERROR("Failed to create an instance of AmConfig!");
      ret = false;
      break;
    }

    config->set_vin_config_path(vin_config_path);
    if (AM_UNLIKELY(!config->load_vdev_config ())) {
      ERROR("Failed to load vdev configurations!");
      ret = false;
      break;
    }

    if (AM_UNLIKELY(NULL == (vdev_config = config->vdevice_config()))) {
      ERROR("Failed to get vdevice configurations!");
      ret = false;
      break;
    }

    if (AM_UNLIKELY(NULL == (simple_cam = new AmSimpleCam(vdev_config)))) {
      ERROR("Failed to create simplecam instance!");
      ret = false;
      break;
    } else {
      for (uint32_t i = 0; i < vdev_config->vin_number; i ++) {
        simple_cam->set_vin_config(config->vin_config(i), i);
      }

      for (uint32_t i = 0; i < vdev_config->vout_number; i ++) {
        simple_cam->set_vout_config(config->vout_config(i), i);
      }

      simple_cam->set_encoder_config(config->encoder_config());
      for (uint32_t i = 0; i < vdev_config->stream_number; ++ i) {
        simple_cam->set_stream_config(config->stream_config(i), i);
      }
      if (!simple_cam->enter_preview()) {
        ERROR("Simple camera failed to enter preview!");
        ret = false;
        break;
      } else {
        int ret_val = 0;
        int count = 0;
        YBufferFormat y_buffer = { 0 };
        ImageScanner zbar_scanner;
        do {
          if (simple_cam->get_y_data(&y_buffer)) {
            Image image(y_buffer.y_width,
                        y_buffer.y_height,
                        "Y800",
                        y_buffer.y_addr,
                        y_buffer.y_width * y_buffer.y_height);
            if ((ret_val = zbar_scanner.scan(image)) > 0) {
              for (Image::SymbolIterator symbol = image.symbol_begin();
                  symbol != image.symbol_end(); ++ symbol) {
                fputs(symbol->get_data().c_str(), fp);
              }
            } else if (ret_val == 0) {
              NOTICE("No symbol found");
            } else {
              ERROR("Zbar scan error!");
            }
          } else {
            ERROR("Failed to get Y data!");
          }
        } while ((ret_val <= 0) && (++ count < 10));
        if ((ret_val <= 0) && (count >= 10)) {
          ERROR("Failed to resolve symbol!");
          ret = false;
          break;
        }
      }
    }
  } while (0);

  if (AM_LIKELY(fp)) {
    fputs("\n", fp);
    fclose(fp);
  }

  if (AM_UNLIKELY(!ret)) {
    /* delete wifi.conf when qr code fails to read wifi configuration */
    if (AM_UNLIKELY(result_path && (remove(result_path) < 0))) {
      ERROR("Failed to remove %s: %s", result_path, strerror (errno));
    }
  }
  delete config;
  delete simple_cam;

  return ret;
}
Пример #14
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;
}