捕获和存储用相机拍摄到本地数据库/ PhoneGap的/科尔多瓦/ iOS版图片捕获和存储用相机拍摄

2019-05-13 15:50发布

我目前正在为使用的PhoneGap /科尔多瓦和jQuerymobile的iOS应用程序。 我们的想法是拍照摄像头和存储所拍摄的图像以备将来使用。 我想的路径/文件名存储到我的本地数据库和图片文件移动到iPhone持久的地方。

可能有人给我提供一个例子吗?

Answer 1:

好吧,这里是解决方案。

  • 在HTML文件

  • 我有显示由摄影机拍摄的画面中的图像标签:

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

  • 捕捉照片的功能被(在拍摄照片的情况下,“smallImage”身份证的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); } 
  • 我的文件已保存在数据库中,以显示它,我把“文件://”包含图像的src列前

希望这有助于。 J.

PS: - 非常感谢西蒙·麦克唐纳(http://hi.im/simonmacdonald)为他的googledocs岗位。



Answer 2:

杰罗姆的答案就像一个魅力..唯一要指出的人,当他们要使用的文件,不使用entry.fullPath这样有时会造成问题,使用entry.toURL(),因为这会给你充分而不必“文件://”添加路径,路径以及绕过如SD卡存储问题..



Answer 3:

if (index == '0')
{
    var options = {
        quality: 50,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.CAMERA,
        allowEdit: false,
        encodingType: Camera.EncodingType.JPEG,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false,
        correctOrientation:true
    };

    $cordovaCamera.getPicture(options).then(movePic,function(imageData) {
        $rootScope.imageUpload=imageData;
    }, function(err) {
        console.error(err);
    });

    function movePic(imageData){
        console.log("move pic");
        console.log(imageData);
        window.resolveLocalFileSystemURL(imageData, resolveOnSuccess, resOnError);
    }

    function resolveOnSuccess(entry){
        console.log("resolvetosuccess");

        //new file name
        var newFileName = itemID + ".jpg";
        var myFolderApp = "ImgFolder";

        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
            console.log("folder create");

            //The folder is created if doesn't exist
            fileSys.root.getDirectory( myFolderApp,
                {create:true, exclusive: false},
                function(directory) {
                    console.log("move to file..");
                    entry.moveTo(directory, newFileName,  successMove, resOnError);
                    console.log("release");

                },
                resOnError);
        },
        resOnError);
    }

    function successMove(entry) {
        //I do my insert with "entry.fullPath" as for the path
        console.log("success");
        //this is file path, customize your path
        console.log(entry);
    }

    function resOnError(error) {
        console.log("failed");
    }

}


Answer 4:

我只是做了一个与承诺工作的基础上,杰罗姆的回答以上:

function moveFile(file){

    var deferred = $q.defer();

    window.resolveLocalFileSystemURL(file,
        function resolveOnSuccess(entry){

            var dateTime = moment.utc(new Date).format('YYYYMMDD_HHmmss');
            var newFileName = dateTime + ".jpg";
            var newDirectory = "photos";

            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {

                    //The folder is created if doesn't exist
                    fileSys.root.getDirectory( newDirectory,
                        {create:true, exclusive: false},
                        function(directory) {

                            entry.moveTo(directory, newFileName, function (entry) {
                                //Now we can use "entry.toURL()" for the img src
                                console.log(entry.toURL());
                                deferred.resolve(entry);

                            }, resOnError);
                        },
                        resOnError);
                },
                resOnError);
        }, resOnError);

    return deferred.promise;
}

function resOnError(error) {
    console.log('Awwww shnap!: ' + error.code);
}


Answer 5:

有3个步骤:

  1. 获取照片。 苹果有一个很好的例子在iPhone上开发站点
  2. 让您的文档目录,如下所示:
     NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [arrayPaths objectAtIndex:0]; 
  3. 最后,存储在步骤1中走出到磁盘的UIImage:
     NSString *filename = @"mypic.png"; NSString *fullpath = [docDir stringByAppendingPathComponent:filename]; [UIImagePNGRepresentation(image) writeToFile:fullpath atomically:YES]; 


文章来源: Capturing and storing a picture taken with the Camera into a local database / PhoneGap / Cordova / iOS