<
video
id=
"video"
controls=
"controls"
>
<
source
src=
"./movie.ogg"
>
</
video
>
<
div
id=
"output"
></
div
>
<
script
type=
"text/javascript"
>
console.
log(
111);
(
function(){
var
video,
output;
var
scale =
0.8;
var
initialize =
function() {
output =
document.
getElementById(
"output");
video =
document.
getElementById(
"video");
video.
addEventListener(
'loadeddata',
captureImage);
};
var
captureImage =
function() {
var
canvas =
document.
createElement(
"canvas");
canvas.
width =
video.
videoWidth *
scale;
canvas.
height =
video.
videoHeight *
scale;
canvas.
getContext(
'2d').
drawImage(
video,
0,
0,
canvas.
width,
canvas.
height);
var
img =
document.
createElement(
"img");
img.
src =
canvas.
toDataURL(
"image/png");
output.
appendChild(
img);
console.
log(
img);
};
initialize();
})();
<
/
script
>
在开发微信小程序的时候,一张图片需要通过WebSocket获取,WebSocket返回png图片的二进制格式的数据,然后小程序将ArrayBuffer转成base64并赋给image的src属性,如下:
const base64 = wx.arrayBufferToBase64(res.data);
that.setData({ QrCodeUrl: "data:;base64," + base64 });
这段代码在电脑上用开发工具里显示图片一直是正常的,但是发布到手机上就出错了,图片死活显示不出来,后来才发现,data:后面应该加上image/png才行,所以代码需要改成这样:
const base64 = wx.arrayBufferToBase64(res.data);
that.setData({ QrCodeUrl: "data:image/png;base64," + base64 });
问题就可以解决。