代碼如下:
@implementation LPRViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"圖片識(shí)別";
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:@"請(qǐng)選擇車牌圖片獲取方式" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *camera = [UIAlertAction actionWithTitle:@"打開(kāi)相機(jī)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self selectedImageWithType:UIImagePickerControllerSourceTypeCamera];
}];
UIAlertAction *library = [UIAlertAction actionWithTitle:@"從圖庫(kù)中獲取" 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:@"無(wú)法打開(kāi)攝像頭" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
}
}
#pragma mark 獲取拍照?qǐng)D片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)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;//超時(shí)時(shí)間
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
[manager POST:@"http://255.255.255.255/xx/xxxx/xxxxx"
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> _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