之前还需要导入的第三方库有 libqrencode 、 Baidu-Voice-SDK-iOS-1
#import "ViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDRecognizerViewController.h"
#import "JSONKit.h"
#import "BDVoiceRecognitionClient.h"
#import "BDVRFileRecognizer.h"
#import "BDVRRawDataRecognizer.h"
#import "QRCodeGenerator.h"
@interface ViewController (){
BDRecognizerViewController *bdrv;
NSMutableData *alldata;
BDRecognizerViewParamsObject *bdvp;
UITextView *TextView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
TextView = [[UITextView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-350)/2, self.view.frame.size.height-600, 350, 150)];
TextView.backgroundColor = [UIColor blackColor]; TextView.layer.cornerRadius = 8;
TextView.layer.masksToBounds = YES; TextView.textColor = [UIColor orangeColor];
TextView.font = [UIFont systemFontOfSize:18]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width-150)/2, self.view.frame.size.height-400, 150, 44)];
btn.backgroundColor = [UIColor blackColor];
[btn setTitle:@"语音识别" forState:0];
[btn setTitleColor:[UIColor redColor] forState:0];
btn.titleLabel.font = [UIFont systemFontOfSize:22];
btn.layer.cornerRadius = 8;
btn.layer.masksToBounds = YES;
[btn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:TextView]; [self.view addSubview:btn];
BDTheme *me = [BDTheme lightRedTheme];
bdrv = [[BDRecognizerViewController alloc] initWithOrigin:CGPointMake(20, 180) withTheme:me];
bdrv.enableFullScreenMode = YES;
bdrv.delegate = self;
bdvp = [[BDRecognizerViewParamsObject alloc] init];
bdvp.apiKey = @"TT49YpLnc0Wk8PqdxhimHcZg";
bdvp.secretKey = @"f790c0bfc23974d19ed50d23c6745299";
}
-(void)add{ alldata = [[NSMutableData alloc] init];
[bdrv startWithParams:bdvp];
}
-(void)onEndWithViews:(BDRecognizerViewController *)aBDRecognizerViewController withResults:(NSArray *)aResults{
TextView.text = [[[[aResults lastObject] lastObject] allKeys] lastObject];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, self.view.frame.size.height-300, 200, 200)];
image.image = [QRCodeGenerator qrImageForString:[[[[aResults lastObject] lastObject] allKeys] lastObject] imageSize:self.view.frame.size.width];
[self.view addSubview:image];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[TextView resignFirstResponder];
}
@end