SlideShare uma empresa Scribd logo
1 de 32
Exploring the limits of HTML5
The Moustamera edition
What’s the goal?
+ + =
The contents
Accessing the camera
Using the Canvas & Haar.JS
Let’s fix that
Some useful tips
Part I
Accessing the Camera
Accessing the camera in pure HTML5
Just add this to your HTML
<video autoplay controls></video>
Accessing the camera in pure HTML5
And fire up the JavaScript!
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || false;
if(!!navigator.getUserMedia)
{
navigator.getUserMedia({audio: true, video: true}, function(stream) {
var video = document.querySelector('video');
video.src = ('webkitURL' in window)?
window.webkitURL.createObjectURL(stream):stream;
}, onFailSoHard);
}
else
{
alert("not supported"); // fallback.
}
Accessing the camera in pure HTML5
The results?
Attempt II: Invoking the native camera
Simple
blackberry.invoke.card.invokeCamera(
blackberry.invoke.card.CAMERA_MODE_PHOTO,
function(path)
{
console.log('Picture path is: ' + path);
}, function(reason)
{
console.log('Cancelled for reason: ' + reason);
}, function(error)
{
console.log('Invoke error: ' + error);
}
);
Attempt II: Invoking the native camera
But don’t forget to edit your config.xml file!
<rim:permissions>
<rim:permit>access_shared</rim:permit>
<rim:permit>use_camera</rim:permit>
</rim:permissions>
<feature id="blackberry.invoke" />
<feature id="blackberry.invoke.card" />
<feature id="blackberry.io" />
<access uri="file:///accounts/1000/" />
Part II
Using the Canvas & HAAR.js
What is the HTML5 canvas?
Allows for dynamic, scriptable
rendering of 2D shapes and
bitmap images.
Container for on the fly
generated graphics
How do I create a HTML5 canvas?
<canvas></canvas>
What is HAAR.js?
A feature detection library for
JavaScript
Detect various features using
existing OpenCV cascades
Lets combine them!
new
HAAR.Detector(haarcascade_frontalface_alt).image(image).complete(
function()
{
console.log('Detection finished: ' + this.objects.length + "
Objects found");
drawImageToCanvas(image);
processDetectionResults(this.objects);
}
).detect(1, 1.25, 0.1, 1, true);
Somewhat impressive code block
-baseScale
-scale_inc
-increment
-min_neighbors
-doCannyPruning
Drawing an Image to the Canvas
function drawImageToCanvas(image)
{
var canvas = document.querySelector('canvas');
canvas.width = image.width;
canvas.height = image.height;
canvas.getContext('2d').drawImage(image, 0, 0, image.width,
image.height);
}
function processDetectionResults(objects)
{
for(var i = 0; i < objects.length; i++)
{
var rect = objects[i];
if(rect.width > 200) //Filter out mistakes
{
var moustache = new Image();
moustache.onload = function()
{
var x = rect.x + rect.width/2.0 - moustache.width/2.0;
var y = rect.y + rect.height/8*5;
var scaleFactor = rect.width/2 / moustache.width;
var moustacheHeight = moustache.height * scaleFactor;
canvas.getContext('2d').drawImage(moustache, x, y, rect.width/
2, moustacheHeight);
}
moustache.src = 'images/moustaches/' + selectedMoustache + '.png';
}
}
}
The result?
Part III
Let’s fix that
Why?
Looping several time over all 8MP
In JavaScript...
Solution?
Resize the image before detection!
In JavaScript...
Procedure
Load the picture in an Image object
Draw the picture on a canvas with the approximate size of the screen
Save that picture to the file system
Loading the original image
var image = new Image();
image.onload = function()
{
resizeImage(image, 'file://' + path);
}
image.src = 'file://' + path;
Resizing the image
function resizeImage(img, imagePath)
{
//Calculate the appropriate size
//width = ...;
//height = ...;
var canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
saveCanvas(canvas);
}
Saving the canvas to a PNG file
<script type="text/javascript" src="js/canvas-toBlob.min.js"></script>
function saveCanvas(canvas)
{
var filePath = blackberry.io.home + '/temp.png'
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 5.0 * 1024 * 1024, function (fs)
{
console.log('Got FileSystem access...');
console.log('Trying to get a FileEntry object to ' + filePath + '...');
fs.root.getFile(filePath, {create: true}, function(fileEntry)
{
console.log('Got FileEntry access to ' + filePath + '...');
fileEntry.createWriter(function(fileWriter)
{
console.log('Got FileWriter access...');
fileWriter.onerror = onFileError;
fileWriter.onwriteend = function()
{
console.log('Done writing canvas to file ' + filePath);
};
//Save the image to the file system
canvas.toBlob(function (blob)
{
fileWriter.write(blob);
}, 'image/png');
}, onFileError);
}, onFileError);
}, onFileError);
}
function onFileError(e)
{
var msg = '';
switch (e.code)
{
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
};
console.log('Error: ' + msg);
}
The result?
Part IV
Tips and tricks
Some bugs in WebWorks
2013-02-23 13:41:37 GET /ripple/build_status/8265 200
out: [INFO] java.lang.NullPointerException	at com.qnx.bbt.packager.Asset.setSourcePath(Asset.java:88)	 at com.qnx.bbt.packager.Asset.<init>(Asset.java:75)	 at
com.qnx.bbt.xml.BbtExtensionXml.getAsset(BbtExtensionXml.java:571)	 at com.qnx.bbt.xml.BbtExtensionXml.getAssets(BbtExtensionXml.java:541)	at
com.qnx.bbt.packager.BbtBarValueProvider.getAssets(BbtBarValueProvider.java:202)	 at com.qnx.bbt.bar.BARPackager.getAssets(BARPackager.java:71)	 at
com.qnx.bbt.bar.BARPackager.findAsset(BARPackager.java:233)
out: [INFO] 	 at com.qnx.bbt.bar.BARPackager.associateSourceAssets(BARPackager.java:227)	at
com.qnx.bbt.packager.AbstractPackager.parseDescriptorAndCreateBarManifest(AbstractPackager.java:577)	 at
com.qnx.bbt.packager.AbstractPackager.doRun(AbstractPackager.java:238)	 at com.qnx.bbt.packager.AbstractPackager.runPackager(AbstractPackager.java:164)	 at
com.qnx.bbt.nativepackager.BarNativePackager.main(BarNativePackager.java:61)
out: [ERROR] Error: null
out: [ERROR] Native Packager exception occurred
2013-02-23 13:41:38 GET /ripple/build_status/8265 200
2013-02-23 13:41:38 GET /ripple/build_status/8265 200
out: [INFO] java.lang.NullPointerException
out: [INFO] 	 at com.qnx.bbt.packager.Asset.setSourcePath(Asset.java:88)	 at com.qnx.bbt.packager.Asset.<init>(Asset.java:75)	 at
com.qnx.bbt.xml.BbtExtensionXml.getAsset(BbtExtensionXml.java:571)	 at com.qnx.bbt.xml.BbtExtensionXml.getAssets(BbtExtensionXml.java:541)	at
com.qnx.bbt.packager.BbtBarValueProvider.getAssets(BbtBarValueProvider.java:202)	 at com.qnx.bbt.bar.BARPackager.getAssets(BARPackager.java:71)
[INFO] 	 at com.qnx.bbt.bar.BARPackager.findAsset(BARPackager.java:233)
[INFO] 	 at com.qnx.bbt.bar.BARPackager.associateSourceAssets(BARPackager.java:227)
[INFO] 	 at com.qnx.bbt.packager.AbstractPackager.parseDescriptorAndCreateBarManifest(AbstractPackager.java:577)
[INFO] 	 at com.qnx.bbt.packager.AbstractPackager.doRun(AbstractPackager.java:238)
[INFO] 	 at com.qnx.bbt.packager.AbstractPackager.runPackager(AbstractPackager.java:164)
[INFO] 	 at com.qnx.bbt.nativepackager.BarNativePackager.main(BarNativePackager.java:61)
out: [ERROR] Error: null
out: [ERROR] Native Packager exception occurred
Done build
error response - {"code":1,"msg":"[ERROR] Error: nulln[ERROR] Native Packager exception occurredn[INFO] java.lang.NullPointerExceptionn[INFO] tat
com.qnx.bbt.packager.Asset.setSourcePath(Asset.java:88)tat com.qnx.bbt.packager.Asset.<init>(Asset.java:75)tat com.qnx.bbt.xml.BbtExtensionXml.getAsset(BbtExtensionXml.j
Solution?
Don’t use a folder named src in your WebWorks project!
Conclusion
Too early for HTML5 Camera (even on BB10)
JavaScript is not without limits
Everybody looks cool with a moustache!

Mais conteúdo relacionado

Mais procurados

What the heck went wrong?
What the heck went wrong?What the heck went wrong?
What the heck went wrong?
Andy McKay
 
The Open Web and what it means
The Open Web and what it meansThe Open Web and what it means
The Open Web and what it means
Robert Nyman
 
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2KZepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Thomas Fuchs
 
JavaScript para Graficos y Visualizacion de Datos - BogotaJS
JavaScript para Graficos y Visualizacion de Datos - BogotaJSJavaScript para Graficos y Visualizacion de Datos - BogotaJS
JavaScript para Graficos y Visualizacion de Datos - BogotaJS
philogb
 
HTML5 (and friends) - History, overview and current status - jsDay Verona 11....
HTML5 (and friends) - History, overview and current status - jsDay Verona 11....HTML5 (and friends) - History, overview and current status - jsDay Verona 11....
HTML5 (and friends) - History, overview and current status - jsDay Verona 11....
Patrick Lauke
 

Mais procurados (20)

Brave new world of HTML5
Brave new world of HTML5Brave new world of HTML5
Brave new world of HTML5
 
Praktik Pengembangan Konten E-Learning HTML5 Sederhana
Praktik Pengembangan Konten E-Learning HTML5 SederhanaPraktik Pengembangan Konten E-Learning HTML5 Sederhana
Praktik Pengembangan Konten E-Learning HTML5 Sederhana
 
Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?
 
фабрика Blockly
фабрика Blocklyфабрика Blockly
фабрика Blockly
 
JavaScript & HTML5 - Brave New World
JavaScript & HTML5 - Brave New WorldJavaScript & HTML5 - Brave New World
JavaScript & HTML5 - Brave New World
 
YUI 3
YUI 3YUI 3
YUI 3
 
What the heck went wrong?
What the heck went wrong?What the heck went wrong?
What the heck went wrong?
 
Sperimentazioni lezione6 from_designtoapplication copy
Sperimentazioni lezione6 from_designtoapplication copySperimentazioni lezione6 from_designtoapplication copy
Sperimentazioni lezione6 from_designtoapplication copy
 
YouDrup_in_Drupal
YouDrup_in_DrupalYouDrup_in_Drupal
YouDrup_in_Drupal
 
To Err Is Human
To Err Is HumanTo Err Is Human
To Err Is Human
 
The Open Web and what it means
The Open Web and what it meansThe Open Web and what it means
The Open Web and what it means
 
jQuery: Events, Animation, Ajax
jQuery: Events, Animation, AjaxjQuery: Events, Animation, Ajax
jQuery: Events, Animation, Ajax
 
Prototype UI
Prototype UIPrototype UI
Prototype UI
 
RicoLiveGrid
RicoLiveGridRicoLiveGrid
RicoLiveGrid
 
Web APIs you (probably) didn't know existed
Web APIs you (probably) didn't know existedWeb APIs you (probably) didn't know existed
Web APIs you (probably) didn't know existed
 
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2KZepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
 
What the FUF?
What the FUF?What the FUF?
What the FUF?
 
JavaScript para Graficos y Visualizacion de Datos - BogotaJS
JavaScript para Graficos y Visualizacion de Datos - BogotaJSJavaScript para Graficos y Visualizacion de Datos - BogotaJS
JavaScript para Graficos y Visualizacion de Datos - BogotaJS
 
HTML5 (and friends) - History, overview and current status - jsDay Verona 11....
HTML5 (and friends) - History, overview and current status - jsDay Verona 11....HTML5 (and friends) - History, overview and current status - jsDay Verona 11....
HTML5 (and friends) - History, overview and current status - jsDay Verona 11....
 
WebXR if X = how?
WebXR if X = how?WebXR if X = how?
WebXR if X = how?
 

Semelhante a Moustamera

Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02
PL dream
 
JavaScript APIs - The Web is the Platform - .toster conference, Moscow
JavaScript APIs - The Web is the Platform - .toster conference, MoscowJavaScript APIs - The Web is the Platform - .toster conference, Moscow
JavaScript APIs - The Web is the Platform - .toster conference, Moscow
Robert Nyman
 
WebAPIs & WebRTC - Spotify/sthlm.js
WebAPIs & WebRTC - Spotify/sthlm.jsWebAPIs & WebRTC - Spotify/sthlm.js
WebAPIs & WebRTC - Spotify/sthlm.js
Robert Nyman
 
Creating the interfaces of the future with the APIs of today
Creating the interfaces of the future with the APIs of todayCreating the interfaces of the future with the APIs of today
Creating the interfaces of the future with the APIs of today
gerbille
 
JavaScript para Graficos y Visualizacion de Datos
JavaScript para Graficos y Visualizacion de DatosJavaScript para Graficos y Visualizacion de Datos
JavaScript para Graficos y Visualizacion de Datos
philogb
 
JavaScript APIs - The Web is the Platform
JavaScript APIs - The Web is the PlatformJavaScript APIs - The Web is the Platform
JavaScript APIs - The Web is the Platform
Robert Nyman
 
webinale2011_Chris Mills_Brave new world of HTML5Html5
webinale2011_Chris Mills_Brave new world of HTML5Html5webinale2011_Chris Mills_Brave new world of HTML5Html5
webinale2011_Chris Mills_Brave new world of HTML5Html5
smueller_sandsmedia
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
Robert Nyman
 
HTML pour le web mobile, Firefox OS - Devfest Nantes - 2014-11-07
HTML pour le web mobile, Firefox OS - Devfest Nantes - 2014-11-07HTML pour le web mobile, Firefox OS - Devfest Nantes - 2014-11-07
HTML pour le web mobile, Firefox OS - Devfest Nantes - 2014-11-07
Frédéric Harper
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, ChileJavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
Robert Nyman
 

Semelhante a Moustamera (20)

Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02
 
How to implement camera recording for USB webcam or IP camera in C#.NET
How to implement camera recording for USB webcam or IP camera in C#.NETHow to implement camera recording for USB webcam or IP camera in C#.NET
How to implement camera recording for USB webcam or IP camera in C#.NET
 
HTML5: where flash isn't needed anymore
HTML5: where flash isn't needed anymoreHTML5: where flash isn't needed anymore
HTML5: where flash isn't needed anymore
 
Intro to HTML5
Intro to HTML5Intro to HTML5
Intro to HTML5
 
JavaScript APIs - The Web is the Platform - .toster conference, Moscow
JavaScript APIs - The Web is the Platform - .toster conference, MoscowJavaScript APIs - The Web is the Platform - .toster conference, Moscow
JavaScript APIs - The Web is the Platform - .toster conference, Moscow
 
Android workshop
Android workshopAndroid workshop
Android workshop
 
WebAPIs & WebRTC - Spotify/sthlm.js
WebAPIs & WebRTC - Spotify/sthlm.jsWebAPIs & WebRTC - Spotify/sthlm.js
WebAPIs & WebRTC - Spotify/sthlm.js
 
Web versus Native: round 1!
Web versus Native: round 1!Web versus Native: round 1!
Web versus Native: round 1!
 
HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?
 
Creating the interfaces of the future with the APIs of today
Creating the interfaces of the future with the APIs of todayCreating the interfaces of the future with the APIs of today
Creating the interfaces of the future with the APIs of today
 
Cutting edge HTML5 API you can use today (by Bohdan Rusinka)
 Cutting edge HTML5 API you can use today (by Bohdan Rusinka) Cutting edge HTML5 API you can use today (by Bohdan Rusinka)
Cutting edge HTML5 API you can use today (by Bohdan Rusinka)
 
Webauthn Tutorial
Webauthn TutorialWebauthn Tutorial
Webauthn Tutorial
 
JavaScript para Graficos y Visualizacion de Datos
JavaScript para Graficos y Visualizacion de DatosJavaScript para Graficos y Visualizacion de Datos
JavaScript para Graficos y Visualizacion de Datos
 
JavaScript APIs - The Web is the Platform
JavaScript APIs - The Web is the PlatformJavaScript APIs - The Web is the Platform
JavaScript APIs - The Web is the Platform
 
Getting Touchy Feely with the Web
Getting Touchy Feely with the WebGetting Touchy Feely with the Web
Getting Touchy Feely with the Web
 
Vue JS @ MindDoc. The progressive road to online therapy
Vue JS @ MindDoc. The progressive road to online therapyVue JS @ MindDoc. The progressive road to online therapy
Vue JS @ MindDoc. The progressive road to online therapy
 
webinale2011_Chris Mills_Brave new world of HTML5Html5
webinale2011_Chris Mills_Brave new world of HTML5Html5webinale2011_Chris Mills_Brave new world of HTML5Html5
webinale2011_Chris Mills_Brave new world of HTML5Html5
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
 
HTML pour le web mobile, Firefox OS - Devfest Nantes - 2014-11-07
HTML pour le web mobile, Firefox OS - Devfest Nantes - 2014-11-07HTML pour le web mobile, Firefox OS - Devfest Nantes - 2014-11-07
HTML pour le web mobile, Firefox OS - Devfest Nantes - 2014-11-07
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, ChileJavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

Moustamera

  • 1. Exploring the limits of HTML5 The Moustamera edition
  • 3. The contents Accessing the camera Using the Canvas & Haar.JS Let’s fix that Some useful tips
  • 5. Accessing the camera in pure HTML5 Just add this to your HTML <video autoplay controls></video>
  • 6. Accessing the camera in pure HTML5 And fire up the JavaScript! navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || false; if(!!navigator.getUserMedia) { navigator.getUserMedia({audio: true, video: true}, function(stream) { var video = document.querySelector('video'); video.src = ('webkitURL' in window)? window.webkitURL.createObjectURL(stream):stream; }, onFailSoHard); } else { alert("not supported"); // fallback. }
  • 7. Accessing the camera in pure HTML5 The results?
  • 8. Attempt II: Invoking the native camera Simple blackberry.invoke.card.invokeCamera( blackberry.invoke.card.CAMERA_MODE_PHOTO, function(path) { console.log('Picture path is: ' + path); }, function(reason) { console.log('Cancelled for reason: ' + reason); }, function(error) { console.log('Invoke error: ' + error); } );
  • 9. Attempt II: Invoking the native camera But don’t forget to edit your config.xml file! <rim:permissions> <rim:permit>access_shared</rim:permit> <rim:permit>use_camera</rim:permit> </rim:permissions> <feature id="blackberry.invoke" /> <feature id="blackberry.invoke.card" /> <feature id="blackberry.io" /> <access uri="file:///accounts/1000/" />
  • 10. Part II Using the Canvas & HAAR.js
  • 11. What is the HTML5 canvas? Allows for dynamic, scriptable rendering of 2D shapes and bitmap images. Container for on the fly generated graphics
  • 12. How do I create a HTML5 canvas? <canvas></canvas>
  • 13. What is HAAR.js? A feature detection library for JavaScript Detect various features using existing OpenCV cascades
  • 14. Lets combine them! new HAAR.Detector(haarcascade_frontalface_alt).image(image).complete( function() { console.log('Detection finished: ' + this.objects.length + " Objects found"); drawImageToCanvas(image); processDetectionResults(this.objects); } ).detect(1, 1.25, 0.1, 1, true); Somewhat impressive code block -baseScale -scale_inc -increment -min_neighbors -doCannyPruning
  • 15. Drawing an Image to the Canvas function drawImageToCanvas(image) { var canvas = document.querySelector('canvas'); canvas.width = image.width; canvas.height = image.height; canvas.getContext('2d').drawImage(image, 0, 0, image.width, image.height); }
  • 16. function processDetectionResults(objects) { for(var i = 0; i < objects.length; i++) { var rect = objects[i]; if(rect.width > 200) //Filter out mistakes { var moustache = new Image(); moustache.onload = function() { var x = rect.x + rect.width/2.0 - moustache.width/2.0; var y = rect.y + rect.height/8*5; var scaleFactor = rect.width/2 / moustache.width; var moustacheHeight = moustache.height * scaleFactor; canvas.getContext('2d').drawImage(moustache, x, y, rect.width/ 2, moustacheHeight); } moustache.src = 'images/moustaches/' + selectedMoustache + '.png'; } } }
  • 19. Why? Looping several time over all 8MP In JavaScript...
  • 20. Solution? Resize the image before detection! In JavaScript...
  • 21. Procedure Load the picture in an Image object Draw the picture on a canvas with the approximate size of the screen Save that picture to the file system
  • 22. Loading the original image var image = new Image(); image.onload = function() { resizeImage(image, 'file://' + path); } image.src = 'file://' + path;
  • 23. Resizing the image function resizeImage(img, imagePath) { //Calculate the appropriate size //width = ...; //height = ...; var canvas = document.createElement("canvas"); canvas.width = width; canvas.height = height; var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0, width, height); saveCanvas(canvas); }
  • 24. Saving the canvas to a PNG file <script type="text/javascript" src="js/canvas-toBlob.min.js"></script>
  • 25. function saveCanvas(canvas) { var filePath = blackberry.io.home + '/temp.png' window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; window.requestFileSystem(window.TEMPORARY, 5.0 * 1024 * 1024, function (fs) { console.log('Got FileSystem access...'); console.log('Trying to get a FileEntry object to ' + filePath + '...'); fs.root.getFile(filePath, {create: true}, function(fileEntry) { console.log('Got FileEntry access to ' + filePath + '...'); fileEntry.createWriter(function(fileWriter) { console.log('Got FileWriter access...'); fileWriter.onerror = onFileError; fileWriter.onwriteend = function() { console.log('Done writing canvas to file ' + filePath); }; //Save the image to the file system canvas.toBlob(function (blob) { fileWriter.write(blob); }, 'image/png'); }, onFileError); }, onFileError); }, onFileError); }
  • 26. function onFileError(e) { var msg = ''; switch (e.code) { case FileError.QUOTA_EXCEEDED_ERR: msg = 'QUOTA_EXCEEDED_ERR'; break; case FileError.NOT_FOUND_ERR: msg = 'NOT_FOUND_ERR'; break; case FileError.SECURITY_ERR: msg = 'SECURITY_ERR'; break; case FileError.INVALID_MODIFICATION_ERR: msg = 'INVALID_MODIFICATION_ERR'; break; case FileError.INVALID_STATE_ERR: msg = 'INVALID_STATE_ERR'; break; default: msg = 'Unknown Error'; break; }; console.log('Error: ' + msg); }
  • 29. Some bugs in WebWorks 2013-02-23 13:41:37 GET /ripple/build_status/8265 200 out: [INFO] java.lang.NullPointerException at com.qnx.bbt.packager.Asset.setSourcePath(Asset.java:88) at com.qnx.bbt.packager.Asset.<init>(Asset.java:75) at com.qnx.bbt.xml.BbtExtensionXml.getAsset(BbtExtensionXml.java:571) at com.qnx.bbt.xml.BbtExtensionXml.getAssets(BbtExtensionXml.java:541) at com.qnx.bbt.packager.BbtBarValueProvider.getAssets(BbtBarValueProvider.java:202) at com.qnx.bbt.bar.BARPackager.getAssets(BARPackager.java:71) at com.qnx.bbt.bar.BARPackager.findAsset(BARPackager.java:233) out: [INFO] at com.qnx.bbt.bar.BARPackager.associateSourceAssets(BARPackager.java:227) at com.qnx.bbt.packager.AbstractPackager.parseDescriptorAndCreateBarManifest(AbstractPackager.java:577) at com.qnx.bbt.packager.AbstractPackager.doRun(AbstractPackager.java:238) at com.qnx.bbt.packager.AbstractPackager.runPackager(AbstractPackager.java:164) at com.qnx.bbt.nativepackager.BarNativePackager.main(BarNativePackager.java:61) out: [ERROR] Error: null out: [ERROR] Native Packager exception occurred 2013-02-23 13:41:38 GET /ripple/build_status/8265 200 2013-02-23 13:41:38 GET /ripple/build_status/8265 200 out: [INFO] java.lang.NullPointerException out: [INFO] at com.qnx.bbt.packager.Asset.setSourcePath(Asset.java:88) at com.qnx.bbt.packager.Asset.<init>(Asset.java:75) at com.qnx.bbt.xml.BbtExtensionXml.getAsset(BbtExtensionXml.java:571) at com.qnx.bbt.xml.BbtExtensionXml.getAssets(BbtExtensionXml.java:541) at com.qnx.bbt.packager.BbtBarValueProvider.getAssets(BbtBarValueProvider.java:202) at com.qnx.bbt.bar.BARPackager.getAssets(BARPackager.java:71) [INFO] at com.qnx.bbt.bar.BARPackager.findAsset(BARPackager.java:233) [INFO] at com.qnx.bbt.bar.BARPackager.associateSourceAssets(BARPackager.java:227) [INFO] at com.qnx.bbt.packager.AbstractPackager.parseDescriptorAndCreateBarManifest(AbstractPackager.java:577) [INFO] at com.qnx.bbt.packager.AbstractPackager.doRun(AbstractPackager.java:238) [INFO] at com.qnx.bbt.packager.AbstractPackager.runPackager(AbstractPackager.java:164) [INFO] at com.qnx.bbt.nativepackager.BarNativePackager.main(BarNativePackager.java:61) out: [ERROR] Error: null out: [ERROR] Native Packager exception occurred Done build error response - {"code":1,"msg":"[ERROR] Error: nulln[ERROR] Native Packager exception occurredn[INFO] java.lang.NullPointerExceptionn[INFO] tat com.qnx.bbt.packager.Asset.setSourcePath(Asset.java:88)tat com.qnx.bbt.packager.Asset.<init>(Asset.java:75)tat com.qnx.bbt.xml.BbtExtensionXml.getAsset(BbtExtensionXml.j
  • 30. Solution? Don’t use a folder named src in your WebWorks project!
  • 32. Too early for HTML5 Camera (even on BB10) JavaScript is not without limits Everybody looks cool with a moustache!