Cordova plugin that allows camera interaction from HTML code.
Show camera preview popup on top of the HTML.
Features:
- Start a camera preview from HTML code.
- Set a custom position for the camera preview box.
- Set a custom size for the preview box.
- Set a custom alpha for the preview box.
- Maintain HTML interactivity.
- Record Video
- Get Frames from Video
Installation:
cordova plugin add https://github.com/sami4064/CordovaCameraPreview.git
Methods:
startCamera( defaultCamera, desiredFps)
Starts the camera preview instance.
Javascript:
var desiredFps = 60.0;
cordova.plugins.camerapreview.startCamera("front",desiredFps);
stopCamera()
Stops the camera preview instance.
cordova.plugins.camerapreview.stopCamera(stopCameraCallback);
functoin stopCameraCallback(message){
console.log("message from stop camera call: "+message);
}
switchCamera()
Switch from the rear camera and front camera, if available.
cordova.plugins.camerapreview.switchCamera();
show()
Show the camera preview box.
cordova.plugins.camerapreview.show();
hide()
Hide the camera preview box.
cordova.plugins.camerapreview.hide();
startRecording()
Starts the recording of video, before that startCamera() must have been called.
var directoryPath="private/var/mobile/Containers/Data/Application/B8731A4E-623B-4345-890B-D0D3C26DFAC6/tmp/";
var uniqueFileNamePrefix="123456abcd"
cordova.plugins.camerapreview.startRecording(directoryPath,uniqueFileNamePrefix,startedRecordingCallBack,startRecordingFailedCallBack);
function startRecordingFailedCallBack(message){
console.log(message);
console.log("capture video failed");
}
function startedRecordingCallBack(recordingOnFilePath){
console.log("capture video start called and returned");
console.log("file path:" + recordingOnFilePath);
}
stopRecording()
Stops the recording of video, before that startCamera() and startRecording() must have been called.
cordova.plugins.camerapreview.stopRecording(getPathResultCallBack,stopRecordingFailedCallback);
function getPathResultCallBack(arr){
console.log("returned video path = "+arr);
var fps=30.0;
cordova.plugins.camerapreview.generateFramesFromVideo(fps,arr,generatingFramesSuccessCallBack,generatingFramesErrorCallBack);
}
function stopRecordingFailedCallback(message){
console.log("stop recording said : "+message);
}
generateFramesFromVideo()
Generates frames from mov and mp4 videos.
var fps=30.0;
var videoFilePath = [some video file path];
var directoryPath="private/var/mobile/Containers/Data/Application/B8731A4E-623B-4345-890B-D0D3C26DFAC6/tmp/";
var uniqueFileNamePrefix="123456abcd"
var quality = 0.9;
var width=450.0;
cordova.plugins.camerapreview.generateFramesFromVideo(fps,videoFilePath,directoryPath,uniqueFileNamePrefix,quality,width,generatingFramesSuccessCallBack,generatingFramesErrorCallBack);
function generatingFramesErrorCallBack(message){
console.log("error in generation : "+message);
}
function generatingFramesSuccessCallBack(arr){
for (i = 0; i < arr.length; i++) {
console.log("got path : "+arr[i]);
}
}
Base64 image:
Use the cordova-file in order to read the picture file and them get the base64.
Please, refer to this documentation: http://docs.phonegap.com/en/edge/cordova_file_file.md.html
Method readAsDataURL: Read file and return data as a base64-encoded data URL.
Sample:
Please see the CordovaCameraPreviewApp for a complete working example for Android and iOS platforms.
Android Screenshots: