如何在iPhone中捕捉相机仅可见的视图

时间:2014-05-31 06:58:32

标签: ios iphone objective-c avcapturesession avcapturedevice

我创建了一个视图控制器,50%的视图是摄像机视图,其他50%是按钮等。
我面临的问题是当我捕获图像时捕获了更大的图像并且我只想捕获在该视图的50%中可以看到的内容。
所以它看起来像这样:
这就是我在视野中看到的:
enter image description here
这就是我在拍摄后获得的图像:
enter image description here
这背后的代码是:

-(void) viewDidAppear:(BOOL)animated
{
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetMedium;

    CALayer *viewLayer = self.vImagePreview.view.layer;
    NSLog(@"viewLayer = %@", viewLayer);

    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    [captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    captureVideoPreviewLayer.frame = self.vImagePreview.view.bounds;
    [self.vImagePreview.view.layer addSublayer:captureVideoPreviewLayer];
    NSLog(@"Rect of self.view: %@",NSStringFromCGRect(self.vImagePreview.view.frame));
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];

    [session addOutput:stillImageOutput];

    [session startRunning];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Load camera
    vImagePreview = [[CameraViewController alloc]init];

    vImagePreview.view.frame = CGRectMake(10, 10, 300, 500);
    [self.view addSubview:vImagePreview.view];

    vImage = [[UIImageView alloc]init];
    vImage.frame = CGRectMake(10, 10, 300, 300);

    [self.view addSubview:vImage];

}

这是我尝试捕捉图像时的事件:

AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) { break; }
    }

    NSLog(@"about to request a capture from: %@", stillImageOutput);
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
         if (exifAttachments)
         {
             // Do something with the attachments.
             NSLog(@"attachements: %@", exifAttachments);
         }
         else
             NSLog(@"no attachments");

         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
         image = [[UIImage alloc] initWithData:imageData];

         UIAlertView *successAlert = [[UIAlertView alloc] init];
         successAlert.title = @"Review Picture";
         successAlert.message = @"";
         [successAlert addButtonWithTitle:@"Save"];
         [successAlert addButtonWithTitle:@"Retake"];

         [successAlert setDelegate:self];

         UIImageView *_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 40, 40)];
         _imageView.image = image;
         [successAlert addSubview:_imageView];
         [successAlert show];

         UIImage *_image = [self imageByScalingAndCroppingForSize:CGSizeMake(640,480) :_imageView.image];
         NSData *idata = [NSData dataWithData:UIImagePNGRepresentation(_image)];
         encodedImage = [self encodeBase64WithData:idata];
     }];

为什么我整个摄像机视图如何缩小摄像头捕捉的大小,以便我只能捕获摄像机视图中的内容?

1 个答案:

答案 0 :(得分:1)

您可以在捕获后裁剪图像。试试这个

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
// or use the UIImage wherever you like
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]]; 
CGImageRelease(imageRef);

或试试这个

- (UIImage *)crop:(CGRect)rect {
if (self.scale > 1.0f) {
    rect = CGRectMake(rect.origin.x * self.scale,
                      rect.origin.y * self.scale,
                      rect.size.width * self.scale,
                      rect.size.height * self.scale);
}

CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
CGImageRelease(imageRef);
return result;
}

请参阅此链接:Crop UIImage