接到一个需求,给图片中二维码打马赛克,立即联想到ZBar,用ZBar扫描后, 获取二维码在图中的位置,直接打码:
int main(void) { Mat mat = imread("E:\\qrcodetest.jpg"); zbar::ImageScanner scnner; scnner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1); cvtColor(mat, mat, COLOR_BGR2GRAY); zbar::Image imageZbar(mat.cols, mat.rows, "Y800", (uchar *)mat.data, mat.cols * mat.rows); scnner.scan(imageZbar); SymbolIterator symbol = imageZbar.symbol_begin(); if (imageZbar.symbol_begin() != imageZbar.symbol_end()) { //图中有二维码 SymbolIterator iter = imageZbar.symbol_begin(); //iter->get_location_size(): 二维码矩形有几个点,一般都是4个,也就是二维码一般都是方形的 cout << iter->get_location_size() << endl; for (int i = 0; i < iter->get_location_size();i ++) { //获取第i个点的x cout << iter->get_location_x(i) << endl; //获取第i个点的y cout << iter->get_location_y(i) << endl; } } getc(stdin); return 0; }
需要注意的是,二维码矩形点(x,y)的顺序: