用第三方实现百度语音识别与二维码生成

1.导入第三方框架 Baidu-Voice-SDK-iOS-1.6(百度语音)、libqrencode(二维码生成)
2.第二步:在info.plist文件中设置请求数据网络添加NSAppTransportSecurity在他的文件下添加NSAllowsArbitraryLoads后面Boolean改成YES;
3.第三步:在Build Settings中找到Other Linker Flags在后面添加上-ObjC(注意大小写)
4.加入下面这些依赖库

GLKit.framework
CoreTelephony.framework
AVFoundation.framework
SystemConfiguration.framework
AudioToolbox.framework
libz.tbd
Security.framework
QuartzCore.framework
CoreText.framework
CoreLocation.framework
CFNetwork.framework
CoreGraphics.framework

5.导入头文件

#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDVoiceRecognitionClient.h"
#import "QRCodeGenerator.h"

6.所用到的成员变量

    BDRecognizerViewController *bdvc; //语音界面
    NSMutableData *allData;
    BDRecognizerViewParamsObject *bdvp; //参数设置 key 秘钥
    UILabel *label; // 展示识别的内容
    UIImageView *imgV; // 二维码图片框 

// 在viewDidLoad中初始化按钮与Label

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置展示Label
    label = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, (self.view.frame.size.width-20), 120)];
    label.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:label];
    // 设置一个识别按钮
    UIButton *btnDiscern = [[UIButton alloc]initWithFrame:CGRectMake(10, 150, 80, 35)];
    btnDiscern.backgroundColor = [UIColor redColor];
    [btnDiscern setTitle:@"开始识别" forState:UIControlStateNormal];
    [btnDiscern setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnDiscern addTarget:self action:@selector(btn1Click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnDiscern];
    // 设置生成二维码按钮
    UIButton *btnCreate = [[UIButton alloc]initWithFrame:CGRectMake(110, 150, 80, 35)];
    btnCreate.backgroundColor = [UIColor redColor];
    [btnCreate setTitle:@"生成二维码" forState:UIControlStateNormal];
    [btnCreate setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnCreate addTarget:self action:@selector(btn2Click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnCreate];
    // 设置清除Label按钮
    UIButton *btnClean = [[UIButton alloc]initWithFrame:CGRectMake(200, 150, 80, 35)];
    btnClean.backgroundColor = [UIColor redColor];
    [btnClean setTitle:@"清除内容" forState:UIControlStateNormal];
    [btnClean setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnClean addTarget:self action:@selector(btn3Click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnClean];
    // 创建二维码图片框
    imgV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 380, 200,200)];
    // 添加到主界面
    [self.view addSubview:imgV];
    // 主题设置
    BDTheme *me = [BDTheme lightOrangeTheme];
    bdvc = [[BDRecognizerViewController alloc]initWithOrigin:CGPointMake(20, 10) withTheme:me];
    // 设置为全屏幕
    bdvc.enableFullScreenMode = YES;
    // 设置代理
    bdvc.delegate = self;
    bdvp = [[BDRecognizerViewParamsObject alloc]init];
    // bdvp.productID 不用设置(百度开发者平台的apiK)
    bdvp.apiKey = @"ANQLQINhgf2TL0gVP5xhNCxm";
    bdvp.secretKey = @"c3d5f5f8ac5478e87802431389b2cba7";
}

// 遵守协议

<BDRecognizerViewDelegate>

/**
* @brief 录音数据返回
* @param recordData 录音数据
* @param sampleRate 采样率
*/

- (void)onRecordDataArrived:(NSData *)recordData sampleRate:(int)sampleRate{
    [allData appendData:recordData];
}

// 此方法是将语音传递到lable上

- (void)onPartialResults:(NSString *)results
{
    label.text = results;
}

// 按钮一实现开始识别语音方法

-(void)btn1Click{
    allData = [[NSMutableData alloc]init];
    [bdvc startWithParams:bdvp];
}

// 按钮二将语音生成二维码的方法

- (void)btn2Click{
    // 生成二维码
    UIImage *img = [QRCodeGenerator qrImageForString:label.text imageSize:imgV.frame.size.width];
    // 将生成的二维码赋值给图片框
    imgV.image = img;
}

// 按钮三实现清除Label上语音方法

- (void)btn3Click{
    label.text = nil;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值