gpt4 book ai didi

javascript - 在 Javascript 中转换和插入 Base64 数据到 Canvas

转载 作者:太空狗 更新时间:2023-10-29 13:33:22 26 4
gpt4 key购买 nike

我得到一个数据缓冲区,表示 Base64 格式的图像。我得到的数据(以base64表示图像)(部分数据)

193,109,51,74,182,71,212,38,78,62,211,48,81,145,244,39,244,250,215,192,113,46,101,136,203,149,44,6,90,147,197,215,109,66,251,69,47,138,111,202,43,239,122,45,108,125,22,6,149,44, 84,103,136,198,74,212,41,171,203,188,187,69,121,183,255,0,7,75,156,191,140,​​190,45,181,219,141,43,195,214,107,30,129,3,145,38,110,60,135,185,35,130,119,4,108,244,238,0,227,3,140,​​86,85,237,134,149,241,3,69,158,251,71,134,93,31,88,211, 72,82,1,30,100,76,70,65,12,9,12,141,207,94,184,32,140,​​215,45,47,196,111,130,177,187,34,120,79,197,65,84,224,8,175,93,20,929,120,929,1206 85,141,47,227,159,195,111,11,219,223,46,133,225,175,17,91,73,120,170,178,189,196,137,49,96,185,218,50,247,44,64,27,155,167,173,123,252,61,144,97,242,8,63,102,156,234,207,227,169,43,115,77,245,230,119,122,111,104,173,23,78,167,204,103,121,165,108,217,46,88,184,40,124... ..

解码成功

现在我尝试将图像添加到我的 Canvas ,但没有成功,如下所示:

function fillCanvasImage(x, y, width, height, image, pageID) {
if (image == "")
return;

var canvas = document.getElementById("AppPmainCanvas" + pageID);

if (canvas && canvas.getContext) {
var context = canvas.getContext('2d');
if (context) {
context.clearRect(0, 0, canvas.width, canvas.height);
var img = new Image();
img.src = base64_decode(image);
//img.onload = function () {
context.drawImage(img, 0, 0, canvas.width, canvas.height);
//}
}
}
}

我通过以下方式转换数据形式的 base64:

function base64_decode(data) {
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
ac = 0,
dec = "",
tmp_arr = [];

if (!data) {
return data;
}

data += '';

do { // unpack four hexets into three octets using index points in b64
h1 = b64.indexOf(data.charAt(i++));
h2 = b64.indexOf(data.charAt(i++));
h3 = b64.indexOf(data.charAt(i++));
h4 = b64.indexOf(data.charAt(i++));

bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;

o1 = bits >> 16 & 0xff;
o2 = bits >> 8 & 0xff;
o3 = bits & 0xff;

if (h3 == 64) {
tmp_arr[ac++] = String.fromCharCode(o1);
} else if (h4 == 64) {
tmp_arr[ac++] = String.fromCharCode(o1, o2);
} else {
tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
}
} while (i < data.length);

dec = tmp_arr.join('');
dec = utf8_decode(dec);

return dec;
};

// private method for UTF-8 decoding
function utf8_decode(utftext) {
var string = "";
var i = 0;
var c = 0,
c1 = 0,
c2 = 0;

while (i < utftext.length) {
c = utftext.charCodeAt(i);

if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if ((c > 191) && (c < 224)) {
c1 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c1 & 63));
i += 2;
} else {
c1 = utftext.charCodeAt(i + 1);
c2 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c1 & 63) << 6) | (c2 & 63));
i += 3;
}
}

return string;
};

它不起作用,我执行以下操作:在我的服务器端,我将图像转换如下:

public static string BitmapSourceToByteBase64(System.Windows.Media.Imaging.
{
var encoder = new System.Windows.Media.Imaging.JpegBitmapEncoder();
var frame = System.Windows.Media.Imaging.BitmapFrame.Create(source);
encoder.Frames.Add(frame);
var stream = new MemoryStream();

encoder.Save(stream);
return Convert.ToBase64String(stream.ToArray());
//I tired to do return "data:image/name_jpg;base64,"+Convert.ToBase64String(stream.ToArray());
//But got an exception on serialize base64 div 4 in the web client
}

在我的网站上,我获取了数据(base64 图像)并尝试执行以下操作: context.drawImage(0,0,'data:image/jpeg;base64,'+image); 我也试过: context.drawImage('data:image/jpeg;base64,'+image,0,0);

不工作!!!有什么想法吗?

最佳答案

您无需“解码”base64 字符串即可在 Canvas 中绘制它,只需将其分配给图像源并在 Canvas 中绘制该图像即可。

像这样:

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var img = new Image();

img.onload = function() {
context.drawImage(this, 0, 0, canvas.width, canvas.height);
}

img.src = "data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==";
<canvas id="canvas" width="50" height="50"></canvas>

确保您的 base64 字符串以 data:image/gif;base64, 部分开头。

image/jpegimage/png 等。取决于您的编码图像格式。

关于javascript - 在 Javascript 中转换和插入 Base64 数据到 Canvas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8473205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com