iOS相机叠加捕获图像

时间:2014-02-06 10:28:08

标签: ios iphone sdk avfoundation

我面临一些真实的问题。我已经研究过使用UIImagePickerController和AVFoundation在相机中添加叠加的几个教程。一切都对我很好。我可以使用两者在相机上添加叠加。然而,我主要担心的是当用户点击扫描按钮时提取该覆盖视图下的图像部分。我在叠加层和按钮上有一个白色视图,因此只能检索该白色视图下的图像。

-(IBAction)cameraClicked:(id)sender {

    OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    self.imagePickerController = [[UIImagePickerController alloc] init];
    self.imagePickerController.delegate = self;

    if ([UIImagePickerController 
    isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imagePickerController.cameraCaptureMode =     
        UIImagePickerControllerCameraCaptureModePhoto;
    }
    else {
        self.imagePickerController.sourceType =    
        UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }

    self.imagePickerController.showsCameraControls = NO;
    self.imagePickerController.navigationBarHidden = YES;      
    self.imagePickerController.toolbarHidden = YES;

    self.imagePickerController.wantsFullScreenLayout = YES;
    self.imagePickerController.cameraViewTransform =     
    CGAffineTransformScale(self.imagePickerController.cameraViewTransform, 
    CAMERA_TRANSFORM_X,CAMERA_TRANSFORM_Y);
    self.imagePickerController.cameraOverlayView = overlay;
    [self presentViewController:self.imagePickerController animated:YES     
    completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    CGRect cropRect;
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *crosshair = [UIImage imageNamed:@"overlay.png"];

    UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height));
    [image drawInRect: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    if(isIOS7)
        cropRect = CGRectMake(10, 440, crosshair.size.width, crosshair.size.height);
    else
        cropRect = CGRectMake(10, 405, crosshair.size.width, crosshair.size.height);

    CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);
    UIImage *setImage = [UIImage imageWithCGImage:imageRef];
    self.finalImageView.image = setImage;

    [self dismissViewControllerAnimated:YES completion:nil];
}

1 个答案:

答案 0 :(得分:3)

试试这个...工作..

  self.imagePickerController=nil;
  self.imagePickerController = [[UIImagePickerController alloc] init];
  self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  self.imagePickerController.delegate = self;
  self.imagePickerController.allowsEditing = NO;
  self.imagePickerController.showsCameraControls = NO;

//为叠加层创建视图

 CGRect overlayRect = CGRectMake(0, 0, self.imagePickerController.view.frame.size.width,      self.imagePickerController.view.frame.size.height);

    UIView *overlayView = [[UIView alloc] initWithFrame:overlayRect];

    btn_Overlay = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn_Overlay setBackgroundImage:[UIImage imageNamed:@"IMageNmae"] forState:UIControlStateNormal];
    btn_Overlay.frame = CGRectMake(80.0, 170, 150.0, 30.0);
    btn_Overlay.tag = 0;
    [btn_Overlay setTitle:@"Button Title" forState:UIControlStateNormal];
    [btn_Overlay addTarget:self action:@selector(ActinMethod:) forControlEvents:UIControlEventTouchUpInside];
    [overlayView addSubview:btn_Overlay];

[self.imagePickerController setCameraOverlayView:overlayView];
    // display imagePicker
    [self.navigationController presentViewController:self.imagePickerController animated:YES completion:nil];