繁体   English   中英

如何从本地URL获取图像并将其转换为Javascript中的Base64

[英]How to get image from a local URL and convert to Base64 in Javascript

我有这样的URL-> blob:http%3A // localhost%3A4400 / a558ba4c-076b-490a-999d-92bc85f2bcee,但是该URL仅可以在我的浏览器中读取,而不能在另一个浏览器中读取。

.controller('cameraCtrl', function ($scope, Camera) { 
    $scope.takePhoto= function () { 
        Camera.takePhoto().then(function (urlImage) { 
            $scope.lastPicture = urlImagen; 
        }, 
        function (err) { 
            console.err(err); 
        }, 
        { 
            quality: 50, 
            targetWidth: 50, 
            targetHeight: 50, 
            saveToPhotoAlbum: true 
        }); 
    }; 
}

问题是:如何获取该URL的图像以将其转换为字符串Base64? 任何帮助都会很棒,谢谢。

您应该添加代码,这样人们就不必从头开始编写解决方案了。

function  ConvertBlobToDataUrl(blobUrl, complete){
   var canvas = document.createElement("canvas");
   var ctx = canvas.getContext("2d");
   var tempImg = new Image();

   tempImg.onload = function(){
      canvas.width = tempImg .width;
      canvas.height = tempImg .height;
      ctx.drawImage(tempImg , 0,0, tempImg.width, tempImg.height);
      complete(canvas.toDataURL())
   }

   tempImg.src = blobUrl;

}

然后的用法是

var dataUrl = "";
ConvertBlobToDataUrl(blobUrl, function(url){
    dataUrl = url;
}

从内存中写入,但是应该可以正常工作。

暂无
暂无

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

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