我需要通过 json 发送图像/媒体,因为需要将其转换为文本格式。我如何通过 jQuery/Javascript 实现这一点?
你可以在这篇文章中找到你的答案在javascript中获取图像数据 https://stackoverflow.com/questions/934012/get-image-data-in-javascript
function getBase64Image(img) {
// Create an empty canvas element
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
// Get the data-URL formatted image
// Firefox supports PNG and JPEG. You could check img.src to guess the
// original format, but be aware the using "image/jpg" will re-encode the image.
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
您需要通过img
标记到该函数。
欲了解更多详情,请参阅在javascript中将图像转换为二进制数据 https://stackoverflow.com/questions/5420384/convert-an-image-into-binary-data-in-javascript
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)