Zxing二维码扫描(Android开源库)

最近研究了一下二维码扫描,github地址(https://github.com/zxing/zxing)。本来想做个扫描相册内的二维码的,结果没做成。我先分析一下整个开源项目的流程
这里写图片描述

这里我要重点接收一下restartPreviewAndDecode这个函数

/**
 * A single preview frame will be returned to the handler supplied. The data
 * will arrive as byte[] in the message.obj field, with width and height
 * encoded as message.arg1 and message.arg2, respectively.
 * 
 * @param handler
 *            The handler to send the message to.
 * @param message
 *            The what field of the message to be sent.
 */
public synchronized void requestPreviewFrame(Handler handler, int message) {
    Camera theCamera = camera;
    if (theCamera != null && previewing) {
        previewCallback.setHandler(handler, message);
        theCamera.setOneShotPreviewCallback(previewCallback);
    }
}

意思很明确,会返回handler一个消息,消息的what域就是这个id。
好,我们继续往下看。
其中setHandler函数就是把handler和message绑定在一个PreviewCallback类里面
setOneShotPreviewCallback参数是一个接口,这里传入previewCallback这个实例,那么必然会调用继承的函数onPreviewFrame

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        Point cameraResolution = configManager.getCameraResolution();
        Handler thePreviewHandler = previewHandler;
        if (cameraResolution != null && thePreviewHandler != null) {
            Message message = thePreviewHandler.obtainMessage(previewMessage,
                    cameraResolution.x, cameraResolution.y, data);
            message.sendToTarget();
            previewHandler = null;
        } else {
            Log.d(TAG,
                    "Got preview callback, but no handler or resolution available");
        }
    }

注意到了没,这个byte[] data数据就是之后会解析的数据,但是如何生成的这里看不到,它又是如何从bitmap转化来的,也不知道。网上的一些demo有的是自己生成自己解析的,那当然知道数据来源以及格式。
二维码的解析函数大致摘抄如下:

RGBLuminanceSource source = new RGBLuminanceSource(width,
                            height, pixels);
                    Result rawResult = null;
                    if (source != null) {
                        MultiFormatReader multiFormatReader = new MultiFormatReader();
                        BinaryBitmap binaryBitmap = new BinaryBitmap(
                                new HybridBinarizer(source));
                        try {
                            rawResult = multiFormatReader.decode(binaryBitmap);
                            Toast.makeText(
                                    getApplicationContext(),
                                    rawResult.getBarcodeFormat() + "\n"
                                            + rawResult.getText().toString(),
                                    Toast.LENGTH_SHORT).show();
                        } catch (ReaderException re) {
                            Toast.makeText(getApplicationContext(), "二维码解析有误",
                                    Toast.LENGTH_SHORT).show();
                        } finally {
                            multiFormatReader.reset();
                        }
                    }

由于不知道上文所述的byte[] data哪里来的导致RGBLuminanceSource 构造函数有问题,所以就一直Toast二维码解析有误。有没有大神指点一二啊

OpenCV+zbar开源实现摄像头识别二维码,测试验证识别率非常高,已实现简单的应用。 打包源码在VS2013下可以完全编译成功,附加包含OpenCV及zbar-0.10-setup.exe,zbar-0.10.tar.bz2 下载Demo后需要安装 zbar-0.10-setup.exe 以下代码可以可以完成整个流程的开发,也可以贡献积分下载资源包。 1、 环境准备 (1) OpenCV2.49 (2) ZBar开源 (3) VS2013 2、 VS2013环境配置 (1) 配置附加包含目录 C/C++ -- 附加包含目录 include\opencv\include\ include\opencv\include\opencv include\opencv\include\opencv2 include (2) 配置链接器 链接器 -- 附加目录 lib32\opencv\lib lib32 (3) 配置链接器 链接器--输入--附加依赖项 opencv_core249d.lib opencv_highgui249d.lib opencv_imgproc249d.lib libzbar-0.lib 3、 代码开发 (1)包含头文件 include include include include include include using namespace std; using namespace zbar; using namespace cv; (2)实现函数 void MatToCImage(cv::Mat &mat, CImage &cImage) { //create new CImage int width = mat.cols; int height = mat.rows; int channels = mat.channels(); cImage.Destroy(); //clear cImage.Create(width, height, 8 * channels); //默认图像像素单通道占用1个字节 //copy values uchar* ps; uchar* pimg = (uchar*)cImage.GetBits(); //A pointer to the bitmap buffer int step = cImage.GetPitch(); for (int i = 0; i (i)); for (int j = 0; j GetDlgItem(IDC_STATIC_IMG)->GetClientRect(▭); cv::VideoCapture capture(0);//从摄像头读入图像 while (!m_bCloseCamera) { cv::Mat frame; capture >> frame; cv::Mat newframe; cv::Size ResImgSiz = cv::Size(rect.Width(), rect.Height()); cv::resize(frame, newframe, ResImgSiz, CV_INTER_CUBIC); MatToCImage(newframe, imgDst); imgDst.Draw(pThis->GetDlgItem(IDC_STATIC_IMG)->GetDC()->GetSafeHdc(), rect); ImageScanner scanner; scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1); Mat imageGray; cvtColor(frame, imageGray, CV_RGB2GRAY); int width = imageGray.cols; int height = imageGray.rows; uchar *raw = (uchar *)imageGray.data; Image imageZbar(width, height, "Y800", raw, width * height); scanner.scan(imageZbar); //扫描条码 Image::SymbolIterator symbol = imageZbar.symbol_begin(); if (imageZbar.symbol_begin() == imageZbar.symbol_end()) { } else { iIndex++; if (iIndex > 999999) { iIndex = 0; } for (; symbol != imageZbar.symbol_end(); ++symbol) { char szInfo[1024]; memset(szInfo, 0, sizeof(szInfo)); sprintf(szInfo, "[d]类型:%s\r\n条码:%s\r\n", iIndex , symbol->get_type_name().c_str(), symbol->get_data().c_str()); pThis->GetDlgItem(IDC_EDIT1)->SetWindowText(szInfo); } } imageZbar.set_data(NULL, 0); } imgDst.Destroy(); capture.release(); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值