|
| 1 | +// |
| 2 | +// CanvasCamera.js |
| 3 | +// PhoneGap iOS Cordova Plugin to capture Camera streaming into a HTML5 Canvas or an IMG tag. |
| 4 | +// |
| 5 | +// Created by Diego Araos <[email protected]> on 12/29/12. |
| 6 | +// |
| 7 | +// MIT License |
| 8 | + |
| 9 | +#import "CanvasCamera.h" |
| 10 | + |
| 11 | +@implementation CanvasCamera |
| 12 | + |
| 13 | +- (void)startCapture:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options |
| 14 | +{ |
| 15 | + // TODO: add support for options (fps, capture quality, capture format, etc.) |
| 16 | + self.session = [[AVCaptureSession alloc] init]; |
| 17 | + self.session.sessionPreset = AVCaptureSessionPreset352x288; |
| 18 | + |
| 19 | + self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; |
| 20 | + self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil]; |
| 21 | + |
| 22 | + self.output = [[AVCaptureVideoDataOutput alloc] init]; |
| 23 | + self.output.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]; |
| 24 | + |
| 25 | + dispatch_queue_t queue; |
| 26 | + queue = dispatch_queue_create("canvas_camera_queue", NULL); |
| 27 | + |
| 28 | + [self.output setSampleBufferDelegate:(id)self queue:queue]; |
| 29 | + |
| 30 | + [self.session addInput:self.input]; |
| 31 | + [self.session addOutput:self.output]; |
| 32 | + |
| 33 | + [self.session startRunning]; |
| 34 | +} |
| 35 | + |
| 36 | +- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection |
| 37 | +{ |
| 38 | + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 39 | + |
| 40 | + CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); |
| 41 | + CVPixelBufferLockBaseAddress(imageBuffer,0); |
| 42 | + uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer); |
| 43 | + size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); |
| 44 | + size_t width = CVPixelBufferGetWidth(imageBuffer); |
| 45 | + size_t height = CVPixelBufferGetHeight(imageBuffer); |
| 46 | + |
| 47 | + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
| 48 | + CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); |
| 49 | + |
| 50 | + CGImageRef newImage = CGBitmapContextCreateImage(newContext); |
| 51 | + |
| 52 | + CGContextRelease(newContext); |
| 53 | + CGColorSpaceRelease(colorSpace); |
| 54 | + |
| 55 | + UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationUp]; |
| 56 | + |
| 57 | + NSData *imageData = UIImageJPEGRepresentation(image, 1.0); |
| 58 | + NSString *encodedString = [imageData base64Encoding]; |
| 59 | + |
| 60 | + NSString *javascript = @"CanvasCamera.capture('data:image/jpeg;base64,"; |
| 61 | + javascript = [javascript stringByAppendingString:encodedString]; |
| 62 | + javascript = [javascript stringByAppendingString:@"');"]; |
| 63 | + [self.webView performSelectorOnMainThread:@selector(stringByEvaluatingJavaScriptFromString:) withObject:javascript waitUntilDone:YES]; |
| 64 | + |
| 65 | + CGImageRelease(newImage); |
| 66 | + CVPixelBufferUnlockBaseAddress(imageBuffer,0); |
| 67 | + [pool drain]; |
| 68 | +} |
| 69 | + |
| 70 | +@end |
0 commit comments