使用PhoneGap从相机或相册选择图片

5
<script type="text/javascript" charset="utf-8">



var pictureSource;   // Picture source

var destinationType; // Sets the format of returned value 

// Wait for PhoneGap to connect with the device


document.addEventListener("deviceready", onDeviceReady, false);


// PhoneGap is ready to be used!

function onDeviceReady() 

{


   pictureSource = navigator.camera.PictureSourceType;

    destinationType = navigator.camera.DestinationType;


}

function capturePhoto() {

    navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 25, destinationType: 
Camera.DestinationType.FILE_URI });

}

function onPhotoURISuccess(imageURI) {

    createFileEntry(imageURI);
}

function createFileEntry(imageURI) {

    window.resolveLocalFileSystemURI(imageURI, copyPhoto, fail);    
}

function copyPhoto(fileEntry) {

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) { 
        fileSys.root.getDirectory("photos", {create: true, exclusive: false}, 

function(dir) { 

  fileEntry.copyTo(dir, "file.jpg", onCopySuccess, fail); 

            }, fail); 
    }, fail); 
}

function onCopySuccess(entry) {

    console.log(entry.fullPath)
}

function fail(error) {

    console.log(error.code);
}


    </script>

类似问题 - Trung Nguyen
1个回答

2
你应该使用PhoneGap 2.0.0相机对象。文档提供了一个完整的拍照示例。
此外,navigator.camera.getPicture( cameraSuccess, cameraError, [ cameraOptions ] );可以使用相机拍摄照片或从设备的相册中检索照片。图像以base64编码字符串或图像文件的URI返回。
希望这可以帮助到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接