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;
}