【问题标题】:Capture an image using openCV without image detection iOS使用 openCV 捕获图像,无需图像检测 iOS
【发布时间】:2016-09-15 11:16:36
【问题描述】:

我遇到了一些问题,我已经找到了答案,但没有一个对我有用。

目标:在尝试检测 3 个 QR 码 60 秒后,提供手动捕获图像并将该图像发送到电子邮件的选项。类似于:60 秒过去了 --> UIAlertController 带有一个“确定”按钮,该按钮导致图像捕获视图 --> 用户捕获图像,然后将该图像作为附件添加到电子邮件中,然后他们可以发送

问题:找不到使用https://www.toptal.com/machine-learning/real-time-object-detection-using-mser-in-ios中描述的opencv库捕获图像的方法

我尝试过的事情:

  • 在播放视频时截取屏幕截图(有效,但我正在寻找一种更复杂的方法来做到这一点

    (UIImage *)capture {
        UIGraphicsBeginImageContext(self.view.bounds.size);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *imageView = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return imageView;
    }
    
  • 使用 UIImagePickerController(无法让 MFMailComposeViewController 显示。还不断收到“对尚未渲染的视图进行快照会导致空快照。确保您的视图在快照或屏幕更新后的快照之前至少渲染一次。” )

    (IBAction)takePhoto:(id)sender {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.allowsEditing = NO;
        picker.modalPresentationStyle = UIModalPresentationCurrentContext;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:nil];
    }
    
    (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [self dismissViewControllerAnimated:YES completion:nil];
        [self emailImage:image];
     }
    
    (void)emailImage:(UIImage *)image {
        if ([MFMailComposeViewController canSendMail]) {
            MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
            mailComposeViewController.mailComposeDelegate = self;
            mailComposeViewController.navigationBar.barStyle = UIBarStyleDefault;
            mailComposeViewController.modalPresentationStyle = UIModalPresentationPageSheet;
            NSString *messageBody = @"";
            messageBody = [messageBody stringByAppendingFormat:@"Image Attached"];
            NSArray *recipients = [NSArray arrayWithObjects: @"x@x.com", nil];
            [mailComposeViewController setMessageBody:messageBody isHTML:NO];
            [mailComposeViewController setToRecipients:recipients];
    
            NSData *data = UIImagePNGRepresentation(image);
            [mailComposeViewController addAttachmentData:data mimeType:@"image/png" fileName:@"Photo"];
    
            NSString *messageSubject = [NSString stringWithFormat:@"Cannot Scan"];
            [mailComposeViewController setSubject:messageSubject];
    
            [self presentViewController:mailComposeViewController animated:YES completion:nil];
        }
    }
    (void)mailComposeController:(MFMailComposeViewController*)controller
      didFinishWithResult:(MFMailComposeResult)result
                    error:(NSError*)error {
        NSString *msg1;
        switch (result)
        {
            case MFMailComposeResultCancelled:
                msg1 =@"Sending Mail is cancelled";
                break;
            case MFMailComposeResultSaved:
                msg1=@"Sending Mail is Saved";
                break;
            case MFMailComposeResultSent:
                msg1 =@"Your Mail has been sent successfully. We will be responding shortly with your results.";
                break;
            case MFMailComposeResultFailed:
                msg1 =@"Message sending failed";
                break;
            default:
                msg1 =@"Your Mail is not Sent";
                break;
        }
        UIAlertView *mailResultAlert = [[UIAlertView alloc]initWithFrame:CGRectMake(10, 170, 300, 120)];
        mailResultAlert.message=msg1;
        mailResultAlert.title=@"Message";
        [mailResultAlert addButtonWithTitle:@"OK"];
        [mailResultAlert show];
    
        dispatch_async(dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
            [self dismissViewControllerAnimated:YES completion:nil];
        }); 
    }
    

有人知道使用 openCV videoCamera 方法手动捕获图像的方法吗? 或者如何修复 UIImagePickerController? 还是有其他替代解决方案?

谢谢!

【问题讨论】:

    标签: ios objective-c iphone image


    【解决方案1】:

    我发现了 UIImagePickerController 的解决方案。

    在 60 秒未检测到 3 个二维码后,会显示 UIAlertController。当按下“OK”按钮时,我执行到另一个视图控制器的 segue。

    - (void)tooLong
    {
        if ([self.navigationController.visibleViewController isKindOfClass:[UIAlertController class]]) {
            [self.navigationController.visibleViewController dismissViewControllerAnimated:YES completion:nil];
        }
        [self.contourScanner stopScan];
        self.analysisInProgress = NO;
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Oops", nil)
                                                                       message:NSLocalizedString(@"It looks like we cannot detect your cup. Press 'OK' to take a photo manually. This image will be emailed to us to analyze ourselves.", nil)
                                                                preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)  style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self performSegueWithIdentifier:@"takePhoto" sender:nil];
            }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
        });
    }
    

    然后在那个新的 viewcontroller.m 文件中,我创建了一个全局变量 (@property (strong, nonatomic) UIImagePickerController *picker; ),然后我添加了以下内容以正确拍摄原始图像。

    - (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
        if (!self.hasPhotoCaptured) {
            [self takePhoto];
        }
    }
    
    - (void)takePhoto {
        self.hasPhotoCaptured = YES;
        self.picker = [[UIImagePickerController alloc] init];
        self.picker.allowsEditing = NO;
        self.picker.modalPresentationStyle = UIModalPresentationCurrentContext;
        self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.picker.delegate = self;
        self.picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
        self.picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    
        [self presentViewController:self.picker animated:YES completion:nil];
    }
    

    我最初的问题是我没有设置 self.picker.delegate = self;添加后,将调用 OP imagePickerController。

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 2015-07-18
      • 2014-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多