代码如下:
@implementation LPRViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"图片识别";
if (!_idRecognizeView)
{
_idRecognizeView = [IdentityRecognizeView tableViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
}
[self.effectView addSubview:_idRecognizeView];
__weak typeof(self)weakSelf = self;
[_idRecognizeView didSelectedToGetImageWithBlock:^{
[weakSelf theWayOfGetImage];
}];
}
- (void)theWayOfGetImage
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择车牌图片获取方式" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *camera = [UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self selectedImageWithType:UIImagePickerControllerSourceTypeCamera];
}];
UIAlertAction *library = [UIAlertAction actionWithTitle:@"从图库中获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self selectedImageWithType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
}];
[alertController addAction:cancel];
[alertController addAction:camera];
[alertController addAction:library];
[self presentViewController:alertController animated:YES completion:nil];
});
}
- (void)selectedImageWithType:(UIImagePickerControllerSourceType)type
{
if ([UIImagePickerController isSourceTypeAvailable:type])
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.sourceType = type;
self.modalPresentationStyle = UIModalPresentationCurrentContext;
picker.delegate = self;
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
}
else
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"无法打开摄像头" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
}
}
#pragma mark 获取拍照图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
__weak typeof(self)weakSelf = self;
[weakSelf dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
if (image)
{
UIImage *adjustImage = [ImageUtil resizeImageWithImage:image targetSize:CGSizeMake(480, 480)];
_idRecognizeView.identityImageView.image = adjustImage;
[self postImageDataWithImage:adjustImage];
}
}
- (void)postImageDataWithImage:(UIImage *)image
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:
@"application/json",
@"text/html",
@"image/jpeg",
@"image/png",
@"application/octet-stream",
@"text/json",
nil];
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 300.0f;//超时时间
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
[manager POST:@"http://255.255.255.255/xx/xxxx/xxxxx"
parameters:nil
constructingBodyWithBlock:^(id _Nonnull formData) {
NSData *imageData =UIImageJPEGRepresentation(image,1);
[formData appendPartWithFileData:imageData
name:@""
fileName:@""
mimeType:@""];
}
progress:^(NSProgress * _Nonnull uploadProgress) {
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"Success:%@",responseObject);
if (responseObject)
{
_idRecognizeView.infoNumber = 10;
[_idRecognizeView.identityList reloadData];
NSArray *dic = responseObject;
_idRecognizeView.image = adjustImage;
_idRecognizeView.infos = dic;
}
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Fail:%@",error);
}];
}
#pragma mark Dealloc
- (void)dealloc
{
}
@end