将本相机拍摄的照片捕获并存储到本地数据库/ PhoneGap / Cordova / iOS中 [英] Capturing and storing a picture taken with the Camera into a local database / PhoneGap / Cordova / iOS
问题描述
我目前正在使用Phonegap / Cordova和jQuerymobile建立iOS应用程式。这个想法是用相机拍照并存储捕获的图像以备将来使用。我想把路径/文件名存储到我的本地数据库,并将图片文件移动到iPhone中的一个永久的地方。
有人可以给我一个例子吗?
我有一个按钮运行拍摄照片的功能:
捕获照片
捕获照片的功能是(拍摄照片时,smallImageID的scr用照片的路径填充)
function capturePhoto(){
//使用设备相机拍摄图片,并以base64编码字符串检索图片
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{quality:50});
}
//成功获取图片后的回调函数
function onPhotoDataSuccess(imageData){
//获取图片句柄
var smallImage = document.getElementById('smallImage');
//取消隐藏图片元素
smallImage.style.display ='block';
smallImage.src = imageData;
}
//当图片未成功获取时的回调函数
function onFail(message){
alert('无法加载图片因为:'+信息);
}
现在我要将图片移到永久文件夹保存链接到我的数据库:
function movePic(file){
window.resolveLocalFileSystemURI(file,resolveOnSuccess,resOnError );
}
//文件系统uri已解析时的回调函数
function resolveOnSuccess(entry){
var d = new Date();
var n = d.getTime();
//新文件名
var newFileName = n +.jpg;
var myFolderApp =EasyPacking;
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,function(fileSys){
//如果不存在则创建文件夹
fileSys.root.getDirectory(myFolderApp,
{create:true,exclusive:false},
function(directory){
entry.moveTo(directory,newFileName,successMove,resOnError);
},
resOnError);
},
resOnError);
}
//文件成功移动时的回调函数 - 插入完整路径
function successMove(entry){
//我插入entry.fullPath和路径
}
函数resOnError(error){
alert(error.code);
}
我的档案已储存在资料库中将file://放在包含图像的行前面src
J。
:
- 非常感谢Simon Mac Donald(http://hi.im/simonmacdonald)在googledocs的帖子。
I'm currently building an app for iOS using Phonegap/Cordova and jQuerymobile. The idea is to take photos with camera and store the captured image for future use. I would like to store the path/filename into my local database and to move the picture file to a persistent place in the iPhone.
Could someone provide me with an example ?
Ok, here is the solution.
in the Html file
I have an image tag for displaying the picture taken by the camera :
I have a button that runs a function for taking photo : Capture Photo
The function to capture photo is (when the photo is taken, the scr of the 'smallImage' id is populated with the path of the photo)
function capturePhoto() { // Take picture using device camera and retrieve image as base64-encoded string navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 }); } //Callback function when the picture has been successfully taken function onPhotoDataSuccess(imageData) { // Get image handle var smallImage = document.getElementById('smallImage'); // Unhide image elements smallImage.style.display = 'block'; smallImage.src = imageData; } //Callback function when the picture has not been successfully taken function onFail(message) { alert('Failed to load picture because: ' + message); }
Now I want to move the picture in a permanent folder and then save the link into my database :
function movePic(file){ window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError); } //Callback function when the file system uri has been resolved function resolveOnSuccess(entry){ var d = new Date(); var n = d.getTime(); //new file name var newFileName = n + ".jpg"; var myFolderApp = "EasyPacking"; window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) { //The folder is created if doesn't exist fileSys.root.getDirectory( myFolderApp, {create:true, exclusive: false}, function(directory) { entry.moveTo(directory, newFileName, successMove, resOnError); }, resOnError); }, resOnError); } //Callback function when the file has been moved successfully - inserting the complete path function successMove(entry) { //I do my insert with "entry.fullPath" as for the path } function resOnError(error) { alert(error.code); }
My file has been saved in the database to display it, i put "file://" front of the row that contains the image src
Hope this help. J.
P.S. : - many thanks to Simon Mac Donald (http://hi.im/simonmacdonald) for his post on googledocs.
这篇关于将本相机拍摄的照片捕获并存储到本地数据库/ PhoneGap / Cordova / iOS中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!