将使用相机拍摄的照片捕获并存储到本地数据库/PhoneGap/Cordova/iOS [英] Capturing and storing a picture taken with the Camera into a local database / PhoneGap / Cordova / iOS

查看:13
本文介绍了将使用相机拍摄的照片捕获并存储到本地数据库/PhoneGap/Cordova/iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Phonegap/Cordova 和 jQuerymobile 为 iOS 构建一个应用程序.这个想法是用相机拍照并存储捕获的图像以供将来使用.我想将路径/文件名存储到我的本地数据库中,并将图片文件移动到 iPhone 中的一个持久位置.

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.

谁能给我一个例子?

推荐答案

好的,解决方法就到这里了.

Ok, here is the solution.

  • 在 Html 文件中

  • 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

拍照功能是(拍照时,'smallImage' id的scr填充照片的路径)

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);
    }
    

  • 我的文件已经保存在数据库中显示它,我把file://"放在包含图像src的行的前面

  • My file has been saved in the database to display it, i put "file://" front of the row that contains the image src

    希望对您有所帮助.J.

    Hope this help. J.

    附::- 非常感谢 Simon Mac Donald (http://hi.im/simonmacdonald) 在 googledocs 上的帖子.

    P.S. : - many thanks to Simon Mac Donald (http://hi.im/simonmacdonald) for his post on googledocs.

    这篇关于将使用相机拍摄的照片捕获并存储到本地数据库/PhoneGap/Cordova/iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆