openharmony hi3518开发板实现扫码 原创 精华

我曾是少年_
发布于 2021-9-29 17:34
浏览
2收藏

前期准备

    1、开发板烧入鸿蒙系统

    2、下载轻量/小型/标准系统(2.0 Canary)源码

    3、下载zxing c++版本

 

配置

    1、将zxing项目放入/SambaShare/code-2.0-canary/third_party目录下

    2、找到/SambaShare/code-2.0-canary/applications/sample/camera/media目录,打开BUILD.gn进行配置

    3、include_dirs中加入//third_party/zxing/core/src

    4、sources中加入所有zxing相关的cpp

sources = [
    "camera_sample.cpp",
    "ImageReaderSource.cpp",
    "jpgd.cpp",
    "lodepng.cpp",
    "//third_party/zxing/core/src/zxing/BarcodeFormat.cpp",
    "//third_party/zxing/core/src/zxing/Binarizer.cpp",
    "//third_party/zxing/core/src/zxing/BinaryBitmap.cpp",
    "//third_party/zxing/core/src/zxing/ChecksumException.cpp",
    "//third_party/zxing/core/src/zxing/DecodeHints.cpp",
    "//third_party/zxing/core/src/zxing/Exception.cpp",
    "//third_party/zxing/core/src/zxing/FormatException.cpp",
    "//third_party/zxing/core/src/zxing/InvertedLuminanceSource.cpp",
    "//third_party/zxing/core/src/zxing/LuminanceSource.cpp",
    "//third_party/zxing/core/src/zxing/MultiFormatReader.cpp",
    "//third_party/zxing/core/src/zxing/Reader.cpp",
    "//third_party/zxing/core/src/zxing/Result.cpp",
    "//third_party/zxing/core/src/zxing/ResultIO.cpp",
    "//third_party/zxing/core/src/zxing/ResultPoint.cpp",
    "//third_party/zxing/core/src/zxing/ResultPointCallback.cpp",
    "//third_party/zxing/core/src/zxing/aztec/AztecDetectorResult.cpp",
    "//third_party/zxing/core/src/zxing/aztec/AztecReader.cpp",
    "//third_party/zxing/core/src/zxing/aztec/decoder/Decoder.cpp",
    "//third_party/zxing/core/src/zxing/aztec/detector/Detector.cpp",
    "//third_party/zxing/core/src/zxing/common/BitArray.cpp",
    "//third_party/zxing/core/src/zxing/common/BitArrayIO.cpp",
    "//third_party/zxing/core/src/zxing/common/BitMatrix.cpp",
    "//third_party/zxing/core/src/zxing/common/BitSource.cpp",
    "//third_party/zxing/core/src/zxing/common/CharacterSetECI.cpp",
    "//third_party/zxing/core/src/zxing/common/DecoderResult.cpp",
    "//third_party/zxing/core/src/zxing/common/DetectorResult.cpp",
    "//third_party/zxing/core/src/zxing/common/GlobalHistogramBinarizer.cpp",
    "//third_party/zxing/core/src/zxing/common/GreyscaleLuminanceSource.cpp",
    "//third_party/zxing/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp",
    "//third_party/zxing/core/src/zxing/common/GridSampler.cpp",
    "//third_party/zxing/core/src/zxing/common/HybridBinarizer.cpp",
    "//third_party/zxing/core/src/zxing/common/IllegalArgumentException.cpp",
    "//third_party/zxing/core/src/zxing/common/PerspectiveTransform.cpp",
    "//third_party/zxing/core/src/zxing/common/Str.cpp",
    "//third_party/zxing/core/src/zxing/common/StringUtils.cpp",
    "//third_party/zxing/core/src/zxing/common/detector/MonochromeRectangleDetector.cpp",
    "//third_party/zxing/core/src/zxing/common/detector/WhiteRectangleDetector.cpp",
    "//third_party/zxing/core/src/zxing/common/reedsolomon/GenericGF.cpp",
    "//third_party/zxing/core/src/zxing/common/reedsolomon/GenericGFPoly.cpp",
    "//third_party/zxing/core/src/zxing/common/reedsolomon/ReedSolomonDecoder.cpp",
    "//third_party/zxing/core/src/zxing/common/reedsolomon/ReedSolomonException.cpp",
    "//third_party/zxing/core/src/zxing/datamatrix/DataMatrixReader.cpp",
    "//third_party/zxing/core/src/zxing/datamatrix/Version.cpp",
    "//third_party/zxing/core/src/zxing/datamatrix/decoder/BitMatrixParser.cpp",
    "//third_party/zxing/core/src/zxing/datamatrix/decoder/DataBlock.cpp",
    "//third_party/zxing/core/src/zxing/datamatrix/decoder/DecodedBitStreamParser.cpp",
    "//third_party/zxing/core/src/zxing/datamatrix/decoder/Decoder.cpp",
    "//third_party/zxing/core/src/zxing/datamatrix/detector/CornerPoint.cpp",
    "//third_party/zxing/core/src/zxing/datamatrix/detector/Detector.cpp",
    "//third_party/zxing/core/src/zxing/datamatrix/detector/DetectorException.cpp",
    "//third_party/zxing/core/src/zxing/multi/ByQuadrantReader.cpp",
    "//third_party/zxing/core/src/zxing/multi/GenericMultipleBarcodeReader.cpp",
    "//third_party/zxing/core/src/zxing/multi/MultipleBarcodeReader.cpp",
    "//third_party/zxing/core/src/zxing/multi/qrcode/QRCodeMultiReader.cpp",
    "//third_party/zxing/core/src/zxing/multi/qrcode/detector/MultiDetector.cpp",
    "//third_party/zxing/core/src/zxing/multi/qrcode/detector/MultiFinderPatternFinder.cpp",
    "//third_party/zxing/core/src/zxing/oned/CodaBarReader.cpp",
    "//third_party/zxing/core/src/zxing/oned/Code39Reader.cpp",
    "//third_party/zxing/core/src/zxing/oned/Code93Reader.cpp",
    "//third_party/zxing/core/src/zxing/oned/Code128Reader.cpp",
    "//third_party/zxing/core/src/zxing/oned/EAN8Reader.cpp",
    "//third_party/zxing/core/src/zxing/oned/EAN13Reader.cpp",
    "//third_party/zxing/core/src/zxing/oned/ITFReader.cpp",
    "//third_party/zxing/core/src/zxing/oned/MultiFormatOneDReader.cpp",
    "//third_party/zxing/core/src/zxing/oned/MultiFormatUPCEANReader.cpp",
    "//third_party/zxing/core/src/zxing/oned/OneDReader.cpp",
    "//third_party/zxing/core/src/zxing/oned/OneDResultPoint.cpp",
    "//third_party/zxing/core/src/zxing/oned/UPCAReader.cpp",
    "//third_party/zxing/core/src/zxing/oned/UPCEANReader.cpp",
    "//third_party/zxing/core/src/zxing/oned/UPCEReader.cpp",
    "//third_party/zxing/core/src/zxing/pdf417/PDF417Reader.cpp",
    "//third_party/zxing/core/src/zxing/pdf417/decoder/BitMatrixParser.cpp",
    "//third_party/zxing/core/src/zxing/pdf417/decoder/DecodedBitStreamParser.cpp",
    "//third_party/zxing/core/src/zxing/pdf417/decoder/Decoder.cpp",
    "//third_party/zxing/core/src/zxing/pdf417/decoder/ec/ErrorCorrection.cpp",
    "//third_party/zxing/core/src/zxing/pdf417/decoder/ec/ModulusGF.cpp",
    "//third_party/zxing/core/src/zxing/pdf417/decoder/ec/ModulusPoly.cpp",
    "//third_party/zxing/core/src/zxing/pdf417/detector/Detector.cpp",
    "//third_party/zxing/core/src/zxing/pdf417/detector/LinesSampler.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/ErrorCorrectionLevel.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/FormatInformation.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/QRCodeReader.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/Version.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/decoder/BitMatrixParser.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/decoder/DataBlock.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/decoder/DataMask.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/decoder/DecodedBitStreamParser.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/decoder/Decoder.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/decoder/Mode.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/detector/AlignmentPattern.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/detector/AlignmentPatternFinder.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/detector/Detector.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/detector/FinderPattern.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/detector/FinderPatternFinder.cpp",
    "//third_party/zxing/core/src/zxing/qrcode/detector/FinderPatternInfo.cpp",
    "//third_party/zxing/core/src/bigint/BigInteger.cc",
    "//third_party/zxing/core/src/bigint/BigIntegerAlgorithms.cc",
    "//third_party/zxing/core/src/bigint/BigIntegerUtils.cc",
    "//third_party/zxing/core/src/bigint/BigUnsigned.cc",
    "//third_party/zxing/core/src/bigint/BigUnsignedInABase.cc" ]

添加解码代码

    参考zxing/cli/src/main.cpp和工具类,在camera_sample.cpp文件里的FrameStateCallback的OnFrameFinished回调中进行二维码解码

cout << "Receive frame complete inform." << endl;
        if (fc.GetFrameConfigType() == FRAME_CONFIG_CAPTURE) {
            cout << "Capture frame received." << endl;
            list<Surface *> surfaceList = fc.GetSurfaces();
            for (Surface *surface : surfaceList) {
                SurfaceBuffer *buffer = surface->AcquireBuffer();
                if (buffer != nullptr) {
                    unsigned char *virtAddr = reinterpret_cast<unsigned char *>(buffer->GetVirAddr());
                    if (virtAddr != nullptr) {
                           cout << "start read_image...." << endl;
                           int width, height;
                           int comps = 0;
                           char *bufferImg = reinterpret_cast<char*>(jpgd::decompress_jpeg_image_from_memory(
                              virtAddr, buffer->GetSize(),&width, &height, &comps, 4));
                           cout << "Size:" << buffer->GetSize() <<" width:" << width<< " height:" << height<< " comps:" << comps << endl;
                           zxing::ArrayRef<char> image = zxing::ArrayRef<char>(bufferImg, 4*width * height);
                           free(bufferImg);
                           if (!image) {
                             ostringstream msg;
                             msg << "Loading jpeg_image_from_memory failed.";
                             throw zxing::IllegalArgumentException(msg.str().c_str());
                           }
                           Ref<LuminanceSource> source = Ref<LuminanceSource>(new ImageReaderSource(image, width, height, comps));
                           int res = read_image(source, true, "jpg");
                           cout << "end read_image...." << endl;
                           if(res == 0){
                             //扫码成功
                           }
                    }
                    surface->ReleaseBuffer(buffer);
                }
                delete surface;
            }
        }
        delete &fc;

编译和运行

    1、在/SambaShare/code-2.0-canary/applications/sample/camera/media目录下执行命令进行编译 hb build camera_lite

          生成的文件在/SambaShare/code-2.0-canary/out/hispark_aries/ipcamera_hispark_aries/dev_tools/bin

          找到camera_sample可执行文件并在开发板上运行

openharmony hi3518开发板实现扫码-鸿蒙开发者社区

    2、选择1.captrue 或者自己写个方法如4.scancode进行图像抓取,抓取时将二维码图片放在摄像头前

         如识别失败:

Receive frame complete inform.
Capture frame received.
start read_image....
Size:100393 width:1280 height:720 comps:3
cell_result:zxing::ReaderException: No code detected
end read_image....

         如识别成功:

Receive frame complete inform.
Capture frame received.
start read_image....
Size:104433 width:1280 height:720 comps:3
cell_result:
result:::
https://u.wechat.com/EHLp4BGfLmfjtmCl6o-SPMg
end read_image....

遇到的问题

    1、解码时可能会出现OOM,需要降低image的width,height

          1)初始化相机时选择较低的width,height

      const CameraAbility *ability = camKit->GetCameraAbility(cam);
        /* find camera which fits user's ability */
        list<CameraPicSize> sizeList = ability->GetSupportedSizes(0);
        for (auto &pic : sizeList) {
            cout << "pic.width: " << pic.width << endl;
            cout << "pic.height: " << pic.height << endl;
            if (pic.width == 1280 && pic.height == 720) {
                camId = cam;
                break;
            }
        }

            2)配置Surface,设置相同的width,height

Surface *surface = Surface::CreateSurface();
surface->SetWidthAndHeight(1280, 720); 

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2021-9-30 09:23:18修改
5
收藏 2
回复
举报
1条回复
按时间正序
/
按时间倒序
鸿联
鸿联

3518的好贴!

回复
2021-9-30 12:44:16
回复
    相关推荐