如何在iPhone应用程序中使用设备摄像头以编程方式拍照?

8
在iPhone应用中,我们可以通过使用iPhone设备相机来编程以一定的时间间隔拍摄照片吗?
如果可以,请告知如何在iPhone应用中以程序方式拍照?
请帮忙提建议。
谢谢,

同样的问题在这里。我一直对一些能够做到这一点的摄影应用程序感兴趣... - Di Wu
3个回答

8

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


通过调用takePicture方法,我可以使用iPhone设备相机拍摄新的静态图像,还是只能访问图片库并从中获取图片? - ios
@Prerak,takePicture旨在通过使用设备相机“拍摄新的静态图像”。如果您只获取图片库,请确保将UIImagePickerController的sourceType属性设置为UIImagePickerControllerSourceTypeCamera。这将为您提供相机界面。 - Matt Wilding
@Prerak:我认为你可以做到,我在我的iPhone上使用一个名为TimerCamera的免费应用程序,它可以连续拍照。 - Tien Do

0

在 .h 文件中导入此文件:

AVFoundation/AVCaptureInput.h
AVFoundation/AVCaptureDevice.h
AVFoundation/AVCaptureOutput.h
AVFoundation/AVMediaFormat.h    

放在 .m 文件中:

- (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

您可以使用UIImagePickerController的takePicture方法来拍照。

如果您需要更多对图片的控制,可以使用AVFoundation头文件,其中包含avcapturestillimageoutput方法来捕获图像。了解更多信息


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接