捕获相机拍摄的照片并将其存储到本地数据库/PhoneGap/Cordova/iOS中

2023-11-29

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

有人能给我举个例子吗?


好的,这是解决方案。

  • 在 HTML 文件中

  • 我有一个用于显示相机拍摄的照片的图像标签:

  • 我有一个运行拍照功能的按钮: 拍摄照片

  • 拍摄照片的函数是(拍摄照片时,'smallImage'id的scr会填充照片的路径)

    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);
    }
    
  • 现在我想将图片移动到永久文件夹中,然后将链接保存到我的数据库中:

    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 的行前面

希望这有帮助。 J。

附: : - 非常感谢 Simon Mcdonald (http://hi.im/simonmacdonald) 在 google 文档上发表的文章。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

捕获相机拍摄的照片并将其存储到本地数据库/PhoneGap/Cordova/iOS中 的相关文章

随机推荐