简体   繁体   English

如何显示使用cordova-plugin-media-capture捕获的图像

[英]How to display an image captured with cordova-plugin-media-capture

I am using this plugin which has allowed me to capture an image with my Android phone.我正在使用这个插件,它允许我用我的 Android 手机捕捉图像。

Now how am I supposed to display the image just captured in my app?现在我应该如何显示刚刚在我的应用程序中捕获的图像? The following didn't work.以下没有奏效。 It said: "TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'."它说: “TypeError:无法在‘FileReader’上执行‘readAsDataURL’:参数 1 不是‘Blob’类型。”

function capturePhoto(){
    navigator.device.capture.captureImage(
        file=>{
            getBase64(file).then(b64=>{
                document.getElementById('fsPhotoI').src=b64;
            }).catch(e=>alert(e));
        },
        error=>{alert(error);},
        {limit:1}
    );
}

function getBase64(f) {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(f);
        reader.onload = () => resolve(reader.result);
        reader.onerror = error => reject(error);
    });
}

I solved it:我解决了:

function capturePhoto(){
    navigator.device.capture.captureImage(
        file=>{
            document.getElementById('fsPhotoI').src=file[0].fullPath;
        },
        e=>{alert(JSON.stringify(e));},
        {limit:1}
    );
}

One thing I overlooked was that the 'file' variable returned is an array instead of a single file, even when only one image is captured.我忽略的一件事是,返回的“文件”变量是一个数组而不是单个文件,即使只捕获了一张图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM