IOS开发(17)IOS 使用LBXScan框架开发扫一扫功能

说起IOS开发,我是个新手,最近公司接到的项目,C、C++都快忘光的我做IOS开发表示又很大的压力。无奈,还是的搞。找了找资料,看了几天就走在了IOS APP开发的道路上。
在做扫一扫功能,由于自己没有做过,还是比较迷茫,android的扫一扫是集成第三方的框架做的,比较简单几步就搞定了,但是IOS的扫一扫功能,现在来看也是很简单的。只不过第一次做,完全没有经验而已。我的扫一扫功能是使用LBXScan这个框架做的。如果你们以后要做这个功能,只是扫一扫得到二维码的code,那么用我下面说的方法即可。源码在下面。

1 我的开发环境

  • XCODE9.2
  • Cocoapods1.4.0
  • Objective-C

2 LBXScan下载地址

https://github.com/MxABC/LBXScan

3 集成使用步骤

3.1 创建项目

创建项目截图

3.2 创建pod依赖管理文件

在项目根目录创建Podfile文件
注意没有用过Cocoapods请参考:
https://blog.csdn.net/ruyulin/article/details/79534032

Profile文件创建位置

3.3 编辑Podfile文件

platform :ios, ‘8.0#Demo-LBXScan 是target目录名称,一般情况与项目名称相同的那个。
target 'Demo-LBXScan' do 
  #扫一扫
  pod 'LBXScan/LBXNative','~> 2.3'
  pod 'LBXScan/LBXZXing','~> 2.3'
  pod 'LBXScan/LBXZBar','~> 2.3'
  pod 'LBXScan/UI','~> 2.3'
  pod 'LBXAlertAction', '~> 1.0.6'
end

3.3 在命令行进入项目跟目录进行进行依赖安装操作

witsystemdeMacBook-Pro:Demo-LBXScan witsystem$ ls
Demo-LBXScan        Demo-LBXScanTests   Podfile
Demo-LBXScan.xcodeproj  Demo-LBXScanUITests
witsystemdeMacBook-Pro:Demo-LBXScan witsystem$ pod install
Analyzing dependencies
Downloading dependencies
Installing LBXAlertAction (1.0.6)
Installing LBXScan (2.3)
Installing ZBarSDK (1.3.1)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `Demo-LBXScan.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 5 dependencies from the Podfile and 3 total pods installed.

[!] Smart quotes were detected and ignored in your Podfile. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.

3.4 安装依赖后,项目截图
安装依赖后,项目截图

3.5 编译项目

使用command + b项目编译报错:

ld: warning: directory not found for option '-L/Users/witsystem/Library/Developer/Xcode/DerivedData/Demo-LBXScan-dtnxdyiagqhprgajvucupburjhlk/Build/Products/Debug-iphonesimulator/LBXAlertAction'
ld: warning: directory not found for option '-L/Users/witsystem/Library/Developer/Xcode/DerivedData/Demo-LBXScan-dtnxdyiagqhprgajvucupburjhlk/Build/Products/Debug-iphonesimulator/LBXScan'
ld: warning: directory not found for option '-L/Users/witsystem/Library/Developer/Xcode/DerivedData/Demo-LBXScan-dtnxdyiagqhprgajvucupburjhlk/Build/Products/Debug-iphonesimulator/ZBarSDK'
ld: library not found for -lLBXAlertAction
clang: error: linker command failed with exit code 1 (use -v to see invocation)

解决方式,请参考:
https://blog.csdn.net/ruyulin/article/details/79534032

3.6 创建PrefixHeader.pch文件

    //
    //  PrefixHeader.pch
    //  Restaurant
    //
    //  Created by witsystem on 2018/3/21.
    //  Copyright © 2018年 witsystem. All rights reserved.
    //

    #ifndef PrefixHeader_pch
    #define PrefixHeader_pch

    // Include any system framework and library headers here that should be included in all compilation units.
    // You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
    //LBXScan 如果需要使用LBXScanViewController控制器代码,那么下载了那些模块,请定义对应的宏
    #define LBXScan_Define_Native  //包含native库
    #define LBXScan_Define_ZXing   //包含ZXing库
    #define LBXScan_Define_ZBar   //包含ZBar库
    #define LBXScan_Define_UI     //包含界面库

    #endif /* PrefixHeader_pch */

3.7 配置文件PrefixHeader.pch
配置文件PrefixHeader.pch文件截图

3.8 添加权限

    <key>NSCameraUsageDescription</key>
    <string>需要相机权限</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>相册权限</string>

3.9 修改项目

  1. 删除Main.storyboard
  2. 删除Info.plist中Main storyboard file base name
  3. 删除项目配置Main Interface

    3.10 修改代理类AppDelegate.m

    //
    //  AppDelegate.m
    //  Demo-LBXScan
    //
    //  Created by witsystem on 2018/3/23.
    //  Copyright © 2018年 witsystem. All rights reserved.
    //

    #import "AppDelegate.h"
    #import "WITHomeViewController.h"

    @interface AppDelegate ()

    @end

    @implementation AppDelegate


    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        WITHomeViewController *vc = [[WITHomeViewController alloc] init];
        self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController: vc];

        [self.window makeKeyAndVisible];


        return YES;
    }


    - (void)applicationWillResignActive:(UIApplication *)application {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }


    - (void)applicationDidEnterBackground:(UIApplication *)application {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }


    - (void)applicationWillEnterForeground:(UIApplication *)application {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }


    - (void)applicationDidBecomeActive:(UIApplication *)application {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }


    - (void)applicationWillTerminate:(UIApplication *)application {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


    @end

3.11 添加文件WITHomeViewController.h

    //
    //  WITHomeViewController.h
    //  Demo-LBXScan
    //
    //  Created by witsystem on 2018/3/23.
    //  Copyright © 2018年 witsystem. All rights reserved.
    //

    #import <UIKit/UIKit.h>

    @interface WITHomeViewController : UIViewController

    @end

3.12 添加文件WITHomeViewController.m

    //
    //  WITHomeViewController.m
    //  Demo-LBXScan
    //
    //  Created by witsystem on 2018/3/23.
    //  Copyright © 2018年 witsystem. All rights reserved.
    //

    #import "WITHomeViewController.h"
    #import <LBXScanViewStyle.h>
    #import "WITOrderScanViewController.h"

    @interface WITHomeViewController ()

    @end

    @implementation WITHomeViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title = @"扫一扫";
        self.view.backgroundColor = [UIColor whiteColor];

        UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake([UIScreen mainScreen].bounds.size.width * 0.5-50, [UIScreen mainScreen].bounds.size.height * 0.5, 100, 50)];
        [button addTarget:self action: @selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle: @"扫一扫" forState: UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [self.view addSubview: button];
    }


    - (IBAction)btnClick:(id)sender {
        //设置扫码区域参数
        LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
        style.centerUpOffset = 44;
        style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Inner;
        style.photoframeLineW = 3;
        style.photoframeAngleW = 18;
        style.photoframeAngleH = 18;
        style.isNeedShowRetangle = NO;
        style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;

        //qq里面的线条图片
        UIImage *imgLine = [UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_light_green"];
        style.animationImage = imgLine;

        style.notRecoginitonArea = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        WITOrderScanViewController *vc = [WITOrderScanViewController new];
        vc.style = style;
        vc.isOpenInterestRect = YES;
        vc.libraryType = SLT_Native;
        [self.navigationController pushViewController:vc animated:YES];
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    /*
    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    @end

3.13 添加文件WITOrderScanViewController.h

    //
    //  WITOrderScanViewController.h
    //  Restaurant
    //
    //  Created by witsystem on 2018/3/20.
    //  Copyright © 2018年 witsystem. All rights reserved.
    //

    #import "LBXScanViewController.h"

    @interface WITOrderScanViewController : LBXScanViewController

    @end

3.14 添加文件WITOrderScanViewController.m

    //
    //  WITOrderScanViewController.m
    //  Restaurant
    //
    //  Created by witsystem on 2018/3/20.
    //  Copyright © 2018年 witsystem. All rights reserved.
    //

    #import "WITOrderScanViewController.h"
    #import "LBXAlertAction.h"

    @interface WITOrderScanViewController ()

    @end

    @implementation WITOrderScanViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.cameraInvokeMsg = @"相机启动中";
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }

    #pragma mark -实现类继承该方法,作出对应处理

    - (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array
    {
        if (!array ||  array.count < 1)
        {
            [self popAlertMsgWithScanResult:nil];

            return;
        }

        //经测试,可以ZXing同时识别2个二维码,不能同时识别二维码和条形码
        //    for (LBXScanResult *result in array) {
        //
        //        NSLog(@"scanResult:%@",result.strScanned);
        //    }

        LBXScanResult *scanResult = array[0];

        NSString*strResult = scanResult.strScanned;

        self.scanImage = scanResult.imgScanned;

        if (!strResult) {

            [self popAlertMsgWithScanResult:nil];

            return;
        }

        //TODO: 这里可以根据需要自行添加震动或播放声音提示相关代码
        //...

        [self showNextVCWithScanResult:scanResult];
    }

    - (void)popAlertMsgWithScanResult:(NSString*)strResult
    {
        if (!strResult) {

            strResult = @"识别失败";
        }

        __weak __typeof(self) weakSelf = self;
        [LBXAlertAction showAlertWithTitle:@"扫码内容" msg:strResult buttonsStatement:@[@"知道了"] chooseBlock:^(NSInteger buttonIdx) {

            [weakSelf reStartDevice];
        }];
    }

    - (void)showNextVCWithScanResult:(LBXScanResult*)strResult {
        NSLog(@"二维码为:%@",strResult.strScanned);
    }

    @end            

3.15 测试

成功截图

启动项目扫描二维码,会在控制台打印出二维码,扫描后会调用下面这个方法

    - (void)showNextVCWithScanResult:(LBXScanResult*)strResult {
        NSLog(@"二维码为:%@",strResult.strScanned);
    }

项目源码:
https://github.com/ruyulin/Demo-LBXScan.git

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值