2016-06-22 7 views
0

結果の変換がnullのPNG画像で、plsは私変換キャンバス

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"); \t 
 
    ctx.drawImage(img, 0, 0, img.width, img.height); 
 
var dataurl = canvas.toDataURL().replace("data:image/jpeg;base64,", ""); 
 
    localStorage.setItem("img", dataurl); 
 
\t $('#bannerImg').attr('src', dataurl);
<img id="bannerImg" src="" alt="Banner Image" width="100%" height="200px" alt="Embbed Image"/> 
 
<canvas id="myCanvas"></canvas>

答えて

2

あなたは画像のonloadイベントにすべてのもの(キャンバスに描く+他の場所でこれを設定する+それからの読み取り)を含める必要があります。クロスドメインの問題により、このスニペット(toDataURLへの呼び出し)では機能しませんが、あなたのウェブサイト上で動作します。

An explanation of why it doesn't work here the call to 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'>

+0

id = "bannerImg" –

+0

でbase64の画像を表示する必要があります。これは '$( '#bannerImg')で得られます。attr( 'src'、dataurl); ' –

+0

base64の画像をエクスポートするのを助けてください –

0

使用document.getElementById('bannerImg').setAttribute('src', dataurl);代わりを助けます。

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(); 

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

+0

まだヌルPNG画像 –

+0

私は、ID = "bannerImg" にbase64で画像表示を必要とする。.. –

+0

は 'のdocument.getElementById( 'bannerImg')を使用するようにアップデートしましたありがとうございました.setAttribute( 'src'、dataurl);代わりに。 – fuzz

関連する問題