在programmatically应用程序中,我们是否可以使用iPhone设备摄像头programmatically在特定的时间间隔内拍摄图片?
如果是的话,请告诉我如何在iPhone应用程序中以编程方式拍摄照片?
请帮忙并提出建议。
谢谢,
发布于 2010-12-18 09:47:06
UIImagePickerController有一个可以以编程方式调用的takePicture方法。
发布于 2012-11-20 05:26:15
您可以使用UIImagePickerController有一个takePicture方法来拍照。
要获得对图片的更多控制,可以使用包含avcapturestillimageoutput方法的AVFoundation头来捕获图像。更多信息
发布于 2016-07-20 11:01:01
在.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;
}https://stackoverflow.com/questions/4477464
复制相似问题