一. 我们这里需要用到两个第三方 Baidu-Voice-SDK-iOS-1.6(百度语音)、libqrencode(二维码生成)
二. 在info.plist设置允许访问网络 添加App Transport Security Settings 在他的下面再添加NSAllowsArbitraryLoads并把后面的no改为yes
三. 导入以下依赖库
GLKit.framework
CoreTelephony.framework
AVFoundation.framework
SystemConfiguration.framework
AudioToolbox.framework
libz.tbd
Security.framework
QuartzCore.framework
CoreText.framework
CoreLocation.framework
CFNetwork.framework
CoreGraphics.framework
// 导入头文件
#import "ViewController.h"
#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDVoiceRecognitionClient.h"
#import "QRCodeGenerator.h"
@interface ViewController ()<BDRecognizerViewDelegate>
@property(nonatomic,strong)BDRecognizerViewController *bdvc; //语音界面
@property(nonatomic,strong)NSMutableData *allData;
@property(nonatomic,strong)BDRecognizerViewParamsObject *bdvp; //参数设置 key 秘
// 创建显示二维码和内容的属性
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *lable;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 使用百度主题设置
BDTheme *me = [BDTheme lightOrangeTheme];
self.bdvc = [[BDRecognizerViewControlleralloc]initWithOrigin:CGPointMake(20, 10) withTheme:me];
// 设置为全屏幕
self.bdvc.enableFullScreenMode = YES;
// 设置代理
self.bdvc.delegate = self;
self.bdvp = [[BDRecognizerViewParamsObject alloc]init];
// 设置appkey
self.bdvp.apiKey = @"ANQLQINhgf2TL0gVP5xhNCxm";
self.bdvp.secretKey = @"c3d5f5f8ac5478e87802431389b2cba7";
}
// 识别语音
- (void)onRecordDataArrived:(NSData *)recordData sampleRate:(int)sampleRate{
[self.allData appendData:recordData];
}
// 把识别到的内容添加到lable上
- (void)onPartialResults:(NSString *)results
{
self.lable.text = results;
}
// 语音识别按钮 点击开始识别
- (IBAction)yuyinbutton:(id)sender {
self.allData = [[NSMutableData alloc]init];
[self.bdvc startWithParams:self.bdvp];
}
// 生成二维码按钮
- (IBAction)buttton:(id)sender {
// 生成二维码按钮
UIImage *img = [QRCodeGenerator qrImageForString:self.lable.textimageSize:self.image.frame.size.width];
self.image.image = img;
}
// 清空内容
- (IBAction)clearbutton:(id)sender {
self.lable.text = nil;
}
@end