簡體   English   中英

如何在iPhone中僅捕獲相機的可見視圖

[英]How to capture camera only visible view in iPhone

我創建了一個視圖控制器,視圖的50%是攝影機視圖,另外50%是按鈕等。
我面臨的問題是,當我捕獲圖像時,會捕獲到更大的圖像,並且只想捕獲在那50%的視圖中看到的圖像。
所以看起來像這樣:
這是我所看到的:
在此處輸入圖片說明
這就是我捕獲后得到的圖像:
在此處輸入圖片說明
其背后的代碼是:

-(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];
     }];

為什么要獲得整個攝像機視圖,如何縮小攝像機捕獲的大小,以便僅捕獲攝像機視圖中看到的內容?

您可以在捕獲后裁剪圖像。 嘗試這個

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;
}

另請參閱此鏈接: 裁剪UIImage

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM