C++用zxing识别二维码

zxing 可以从github的官方网站上下载下来,这里提供一个VS 2010编译zxing的静态库工程,编译时注意一点是:zxing的很多不同的文件夹下含有相同名称的源文件,在编译时应该分别设置这些源文件的obj文件输出到不同的路径下,否则VS默认会将这些obj文件输出到同一个目录下,从而产生相互覆盖,编译期也会给出警告,,这样编译生成的库不全,后期链接调用时很可能发生链接不到的错误。具体可以参考下图。

我这提供一个可用的zxing编译成静态库的VS 2010的工程。

参考github上给出的示例程序,我这提供了两个解码二维码的接口,1.接收图片文件为参数;2.接收图片的二进制数据为参数。

相应的源文件有:

//BufferBitmapSource.h#include <zxing/LuminanceSource.h>#include <stdio.h>#include <stdlib.h>using namespace zxing;class BufferBitmapSource : public LuminanceSource {private:typedef LuminanceSource Super; int width, height; ArrayRef<char> buffer; public: BufferBitmapSource(int inWidth, int inHeight, ArrayRef<char> inBuffer); ~BufferBitmapSource();int getWidth() const; int getHeight() const; ArrayRef<char> getRow(int y, ArrayRef<char> row) const; ArrayRef<char> getMatrix() const; };//BufferBitmapSource.cpp#include "parseQR\BufferBitmapSource.h"#include <iostream>BufferBitmapSource::BufferBitmapSource(int inWidth, int inHeight, ArrayRef<char> inBuffer) :Super(inWidth,inHeight),buffer(inBuffer){width = inWidth; height = inHeight; buffer = inBuffer; } BufferBitmapSource::~BufferBitmapSource(){} int BufferBitmapSource::getWidth() const{return width; } int BufferBitmapSource::getHeight() const{return height; } ArrayRef<char> BufferBitmapSource::getRow(int y, ArrayRef<char> row) const{if (y < 0 || y >= height) {fprintf(stderr, "ERROR, attempted to read row %d of a %d height image.\n", y, height); return NULL; }// WARNING: NO ERROR CHECKING! You will want to add some in your code. if (row == NULL) row = ArrayRef<char>(getWidth());for (int x = 0; x < width; x ++){row[x] = buffer[y*width+x]; }return row; } ArrayRef<char> BufferBitmapSource::getMatrix() const{return buffer; } //ImageReaderSource.h// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-#ifndef __IMAGE_READER_SOURCE_H_#define __IMAGE_READER_SOURCE_H_/* * Copyright 2010-2011 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */#include <zxing/LuminanceSource.h>#include <zxing/common/Array.h>class ImageReaderSource : public zxing::LuminanceSource {private: typedef LuminanceSource Super; const zxing::ArrayRef<char> image; const int comps; char convertPixel(const char* pixel) const;public: static zxing::Ref<LuminanceSource> create(std::string const& filename); ImageReaderSource(zxing::ArrayRef<char> image, int width, int height, int comps); zxing::ArrayRef<char> getRow(int y, zxing::ArrayRef<char> row) const; zxing::ArrayRef<char> getMatrix() const;};#endif /* __IMAGE_READER_SOURCE_H_ *///ImageReaderSource.cpp/* * Copyright 2010-2011 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */#include "parseQR\ImageReaderSource.h"#include <zxing/common/IllegalArgumentException.h>#include <iostream>#include <sstream>#include <cstdlib>#include <algorithm>#include "parseQR\lodepng.h"#include "parseQR\jpgd.h"using std::string;using std::ostringstream;using zxing::Ref;using zxing::ArrayRef;using zxing::LuminanceSource;inline char ImageReaderSource::convertPixel(char const* pixel_) const { unsigned char const* pixel = (unsigned char const*)pixel_; if (comps == 1 || comps == 2) {// Gray or gray+alphareturn pixel[0]; } if (comps == 3 || comps == 4) {// Red, Green, Blue, (Alpha)// We assume 16 bit values here// 0x200 = 1<<9, half an lsb of the result to force roundingreturn (char)((306 * (int)pixel[0] + 601 * (int)pixel[1] +117 * (int)pixel[2] + 0x200) >> 10); } else {throw zxing::IllegalArgumentException("Unexpected image depth"); }}ImageReaderSource::ImageReaderSource(ArrayRef<char> image_, int width, int height, int comps_): Super(width, height), image(image_), comps(comps_) {}Ref<LuminanceSource> ImageReaderSource::create(string const& filename) { string extension = filename.substr(filename.find_last_of(".") + 1); std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower); int width, height; int comps = 0; zxing::ArrayRef<char> image; if (extension == "png") {std::vector<unsigned char> out;{ unsigned w, h;unsigned error = lodepng::decode(out, w, h, filename);if (error) {ostringstream msg;msg << "Error while loading ‘" << lodepng_error_text(error) << "’";throw zxing::IllegalArgumentException(msg.str().c_str());}width = w;height = h;}comps = 4;image = zxing::ArrayRef<char>(4 * width * height);memcpy(&image[0], &out[0], image->size()); } else if (extension == "jpg" || extension == "jpeg") {char *buffer = reinterpret_cast<char*>(jpgd::decompress_jpeg_image_from_file(filename.c_str(), &width, &height, &comps, 4));image = zxing::ArrayRef<char>(buffer, 4 * width * height); } if (!image) {ostringstream msg;msg << "Loading \&;" << filename << "\&; failed.";throw zxing::IllegalArgumentException(msg.str().c_str()); } return Ref<LuminanceSource>(new ImageReaderSource(image, width, height, comps));}zxing::ArrayRef<char> ImageReaderSource::getRow(int y, zxing::ArrayRef<char> row) const { const char* pixelRow = &image[0] + y * getWidth() * 4; if (!row) {row = zxing::ArrayRef<char>(getWidth()); } for (int x = 0; x < getWidth(); x++) {row[x] = convertPixel(pixelRow + (x * 4)); } return row;}/** This is a more efficient implementation. */zxing::ArrayRef<char> ImageReaderSource::getMatrix() const { const char* p = &image[0]; zxing::ArrayRef<char> matrix(getWidth() * getHeight()); char* m = &matrix[0]; for (int y = 0; y < getHeight(); y++) {for (int x = 0; x < getWidth(); x++) {*m = convertPixel(p);m++;p += 4;} } return matrix;}

还有其他几个jpd.h,lodepng.h及其对应的cpp文件都是示例程序给出的,这里不再添加了。

每年的同一天和他庆祝生日,每年的情人节、圣诞节、除夕,

C++用zxing识别二维码

相关文章:

你感兴趣的文章:

标签云: