简体   繁体   English

将canvas转换为base64错误

[英]Convert canvas to base64 error

Result convert is null png image, pls help me 结果转换为null png图像,请帮助我

 var img = new Image(); img.src = 'http://advs.jp/cp/barcode/code128.cgi?nt=1&height=80&c=Z103B5-190-311378&.png'; var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0, img.width, img.height); var dataurl = canvas.toDataURL().replace("data:image/jpeg;base64,", ""); localStorage.setItem("img", dataurl); $('#bannerImg').attr('src', dataurl); 
  <img id="bannerImg" src="" alt="Banner Image" width="100%" height="200px" alt="Embbed Image"/> <canvas id="myCanvas"></canvas> 

You have to include everything (drawing to canvas + reading from it + setting this elsewhere) in the onload event of the image. 你必须在图像的onload事件中包含所有内容(绘制到画布+从中读取+在其他地方设置)。 It doesn't work on this snippet (the call to toDataURL ) due to a cross-domain issue, but it shall work on your website. 由于跨域问题,它不适用于此代码段(对toDataURL的调用),但它可以在您的网站上运行。

An explanation of why it doesn't work here the call to toDataURL 解释为什么它在这里无法调用toDataURL

 var imgCanvas = function() { var img = new Image(); //Wait for image to load before doing something with content img.onload = function() { var canvas = document.getElementById("myCanvas"); //Set canvas size to fit the image canvas.style.height = img.height + 'px'; canvas.style.width = img.width + 'px'; //Draw the image in canvas var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0); //Get a dataurl representation of the image where the image itself is in BASE64 var dataurl = canvas.toDataURL(); //Store it in localStorage localStorage.setItem("img", dataurl); //Show image from localStorage //Need jQuery to use this line instead of next one : $('#bannerImg').attr('src', localStorage.getItem("img")); document.getElementById('bannerImg').setAttribute('src', localStorage.getItem("img")); }; img.src = 'http://advs.jp/cp/barcode/code128.cgi?nt=1&height=80&c=Z103B5-190-311378&.png'; }; imgCanvas(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> Canvas:<br> <canvas id='myCanvas'></canvas> <br><br> Image from dataurl:<br> <img id='bannerImg'> 

Use document.getElementById('bannerImg').setAttribute('src', dataurl); 使用document.getElementById('bannerImg').setAttribute('src', dataurl); instead. 代替。

var showImage = function() {     
    var img = new Image;
    img.onload = function() {
        var canvas = document.getElementById("myCanvas");  
        var ctx = canvas.getContext("2d");    
        ctx.drawImage(img, 0, 0);  
        var dataurl = canvas.toDataURL();
        localStorage.setItem("img", dataurl);  
        document.getElementById('bannerImg').setAttribute('src', dataurl); 
    };
    img.src = 'http://advs.jp/cp/barcode/code128.cgi?nt=1&height=80&c=Z103B5-190-311378&.png';
};

showImage();

Example: https://jsfiddle.net/atg5m6ym/6152/ 示例: https//jsfiddle.net/atg5m6ym/6152/

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

相关问题 将fabricjs canvas转换为base64图像 - Convert fabricjs canvas to base64 image 希望将画布转换为base64图像 - Looking to convert canvas to base64 image 错误:操作不安全。 图像无法在Amazon Server URL的canvas base64中转换 - Error: The operation is insecure. image not convert in canvas base64 in amazon server url 如何等待多个图像加载到画布中并转换为base64? - How to wait for the multiple images to load in the canvas and convert to base64? 在不使用HTML5画布的情况下将图像转换为base64 - Convert an image to base64 without using HTML5 Canvas 在Javascript中将Base64数据转换并插入Canvas - Convert and insert Base64 data to Canvas in Javascript html2canvas不会生成toDataUrl()以转换为base64 - html2canvas does not produce toDataUrl() to convert to base64 如何使用画布将多个图像转换为base64 - How to convert multiple images to base64 with canvas 如何在canvasjs中使用嵌入式图像base64将画布转换为SVG - How to convert canvas to SVG with embedded image base64 in fabricjs 什么更有效地调整图像大小并将其转换为 base64 或转换为 base64 然后调整其大小? (node.js - 没有画布) - What is more efficiente resize an image and the convert to base64 or convert to base64 and then resize it? (node.js - no canvas)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM