首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用设备摄像头在iphone应用程序中编程拍摄图片?

如何使用设备摄像头在iphone应用程序中编程拍摄图片?
EN

Stack Overflow用户
提问于 2010-12-18 09:22:30
回答 3查看 6.7K关注 0票数 8

programmatically应用程序中,我们是否可以使用iPhone设备摄像头programmatically在特定的时间间隔内拍摄图片

如果是的话,请告诉我如何在iPhone应用程序中以编程方式拍摄照片?

请帮忙并提出建议。

谢谢,

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-12-18 09:47:06

UIImagePickerController有一个可以以编程方式调用的takePicture方法。

票数 8
EN

Stack Overflow用户

发布于 2012-11-20 05:26:15

您可以使用UIImagePickerController有一个takePicture方法来拍照。

要获得对图片的更多控制,可以使用包含avcapturestillimageoutput方法的AVFoundation头来捕获图像。更多信息

票数 0
EN

Stack Overflow用户

发布于 2016-07-20 11:01:01

在.h中导入此文件:

代码语言:javascript
复制
AVFoundation/AVCaptureInput.h
AVFoundation/AVCaptureDevice.h
AVFoundation/AVCaptureOutput.h
AVFoundation/AVMediaFormat.h    

放入.m:

代码语言:javascript
复制
- (AVCaptureDevice *)frontCamera
{
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices)
    {
        if ([device position] == AVCaptureDevicePositionFront)
        {
            return device;
        }
    }
    return nil;
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
{
    CGImageRef cgImage = [self imageFromSampleBuffer:sampleBuffer];
    self.theImage = [UIImage imageWithCGImage: cgImage ];
    CGImageRelease( cgImage );

    NSCalendar *sysCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    df.calendar = sysCalendar;
    [df setDateFormat:@"dd_MM_yyyy hh:mm:ss"];
    StrCapture = [NSString stringWithFormat:@"%@.jpeg",[df stringFromDate:[NSDate date]]];
    NSLog(@"StrCapture : %@",StrCapture);

    NSData *imageData = UIImageJPEGRepresentation(self.theImage,1);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:StrCapture];
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil];

    NSLog(@"ImagePAth : %@",fullPath);
}

- (CGImageRef) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer,0);
    uint8_t baseAddress = (uint8_t )CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef newImage = CGBitmapContextCreateImage(newContext);
    CGContextRelease(newContext);

    CGColorSpaceRelease(colorSpace);
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    return newImage;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4477464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档