Skip to content

Commit add1acd

Browse files
committed
initial commit
0 parents  commit add1acd

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

CanvasCamera/CanvasCamera.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 <Cordova/CDVPlugin.h>
10+
#import <UIKit/UIKit.h>
11+
#import <CoreMedia/CoreMedia.h>
12+
#import <CoreVideo/CoreVideo.h>
13+
#import <AVFoundation/AVFoundation.h>
14+
15+
@interface CanvasCamera : CDVPlugin
16+
17+
@property (nonatomic, strong) AVCaptureSession *session;
18+
@property (nonatomic, strong) AVCaptureDevice *device;
19+
@property (nonatomic, strong) AVCaptureDeviceInput *input;
20+
@property (nonatomic, strong) AVCaptureVideoDataOutput *output;
21+
22+
- (void)startCapture:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
23+
24+
@end

CanvasCamera/CanvasCamera.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
CanvasCamera = {
10+
start: function(options) {
11+
// TODO: add support for options (fps, capture quality, capture format, etc.)
12+
cordova.exec(false, false, "CanvasCamera", "startCapture", [""]);
13+
},
14+
capture: function(data) {
15+
// LAZYME: Override this function with something like...
16+
// CanvasCamera.capture = function(data) {
17+
// document.getElementById('camera').src = data;
18+
// }
19+
}
20+
};

CanvasCamera/CanvasCamera.m

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

README.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [CanvasCamera](http://github.com/daraosn/Cordova-CanvasCamera)
2+
### for iOS PhoneGap / Cordova
3+
***
4+
5+
Plugin to capture Camera streaming into a HTML5 Canvas or an IMG tag.
6+
7+
It uses *AVCaptureVideoDataOutput* to create a capture session. Each frame is base64 encoded and passed to the Cordova *UIWebView*. Please note this is mostly experimental and is slower than any native approach.
8+
9+
### How to Install
10+
***
11+
12+
1. Copy **CanvasCamera.h** and **CanvasCamera.m** to *Plugins* directory inside your PhoneGap project.
13+
2. Edit your *Cordova.plist* and add **CanvasCamera** key into your *Plugins* array.
14+
3. Copy **CanvasCamera.js** into your *www* directory and add a script tag for it in your *index.html*.
15+
16+
### How to Use
17+
***
18+
19+
Configuration for ````<img>````:
20+
21+
<img id="camera" width="352" height="288" />
22+
<script>
23+
CanvasCamera.capture = function(data) {
24+
document.getElementById('camera').src = data;
25+
}
26+
</script>
27+
28+
Configuration for ````<canvas>````:
29+
30+
<canvas id="camera" width="352" height="288"></canvas>
31+
<script>
32+
var context = document.getElementById('camera').getContext("2d");
33+
var camImage = new Image();
34+
camImage.onload = function() {
35+
context.drawImage(camImage, 0, 0);
36+
};
37+
CanvasCamera.capture = function(data) {
38+
camImage.src = data;
39+
};
40+
</script>
41+
42+
Start capture:
43+
44+
<script>
45+
document.addEventListener("deviceready", function() {
46+
CanvasCamera.start();
47+
});
48+
</script>
49+
50+
51+
### Known Issues & Roadmap/TODO
52+
***
53+
54+
* Capture has some delay. This could be improved using another approach instead of base64 JPEG encoding.
55+
* Capture runs at low quality (352x288), but this can be changed using a different *sessionPreset*.
56+
* Missing configuration options for fps, quality, orientation, size, formats, etc.
57+
58+
59+
### Author & Contributions
60+
***
61+
62+
Diego Araos <[email protected]>
63+
64+
Feel free to fork & contribute!
65+
66+
### License
67+
***
68+
69+
MIT

0 commit comments

Comments
 (0)