如何在图像中嵌入文本?

我想在向用户显示该图像之前在图像中嵌入用户名。就像我有一个图像文件,现在我想把用户名放在图像里面,就像嵌入而不仅仅是放置文本一样。所以文字成为图像的实际部分?你们能不能建议我一个出路,输出schould是一个base64文件,这样我就可以通过api流式传输数据。谢谢。


子衿沉夜
浏览 108回答 1
1回答

手掌心

您没有指定您的API服务器正在运行的软件,所以我为您的客户端水印需求编写了此内容。它仅适用于没有透明度的图像。它是 LSB(最低有效位)编码的实现。索引.html<!DOCTYPE html><script src="index.js"></script><body></body><script>&nbsp; (async () => {&nbsp; &nbsp; const base64 = `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAWCSURBVGhD7Zl9aFVlHMe/U+da2SzdtOVbLjMtpcbKpNyUFiip2QtEUfZHSZQRRW9kL1R/VNAbBb2AEEQmFhQlZSRLWFqahW0yZBssC7EiZ7nletl0W9/vfZ4nz45n99xz7rnXKX3gx33O2T3nPN/ze3ueu4L+tejHCcAw+3nc87+QnHCY9hety1oEho4QiZiyBFjBlL3dWgQxQ0dIH630UjN26FyG5FeI3nqvGQbSd8gOLGdMTv99D/kTcpBWcT0nVz345AqG24Fl+l1DTMgB2k0/APPeA+asDo79kbQvHjNjR+/fdhBO7oVoLivagVFnmePRM4CaZ8x5P4X209HTaQfh5F6I3nRdjRk7zl/FxJ56dNikZuNZaFS9BPjSZjByL0Rhv68Z2HyNOXYs2w38YceOEbTvHjJjR4aVKz85chKt+SOgs8UcOxYEhFjRGDuwnELLQEx+hIiTaVt8XlGIjZt+JMQ0m32bzdhR9YIp2yEkK+Qf2mDlUiHWTo/UzTfHjoXb6Sk7Vmi1fGbGju7f7SA9yQlRSb1xLzD2AjMOElRE28033sVS7Bh5GnBL65GS7GslGMe+k7fQkusrWJmKJwBXNgI38M2XVASX2FG0t/k3LyUMr/EszxIvr/zs8c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XkkXrIf0i8KWZLEPTzEEzLMbTyyIIl7eIgeWsX8LF9AW8ik97byCEjELxtp9WbTdcyS3V+l4uA2XAkle7TQ0tpJ4rIVIXQP3SvD/3+EEc0jO1hl9FttkvQw6atYBR15Ca18kLPQKkzYAzkivUeEvCLLFzG8IcKFHCfEa4hDkBNECPAvXq3HBRZq23kAAAAASUVORK5CYII=`;&nbsp; &nbsp; const watermark = await createWatermark(base64, 'John Doe');&nbsp; &nbsp; const image = new Image();&nbsp; &nbsp; image.src = watermark;&nbsp; &nbsp; document.body.appendChild(image); // displays watermarked image on screen&nbsp; &nbsp; const hiddenText = await readWatermark(watermark, 8);&nbsp; &nbsp; console.log(hiddenText); // John Doe&nbsp; })();</script></html>索引.jsfunction stringToBits(string) {&nbsp; return string.split('')&nbsp; &nbsp; .map(char => char.charCodeAt(0))&nbsp; &nbsp; .map(int => (int).toString(2).padStart(8, '0').split(''))&nbsp; &nbsp; .flat()&nbsp; &nbsp; .map(n => +n);}function bitsToString(bits) {&nbsp; bits = [...bits.map(n => getBit(n, 0))];&nbsp; return String.fromCharCode(...Array(Math.ceil(bits.length / 8))&nbsp; &nbsp; .fill()&nbsp; &nbsp; .map(() => bits.splice(0, 8))&nbsp; &nbsp; .map(arr => parseInt(arr.join(''), 2)));}function setBit(number, bitPosition, sign) {&nbsp; if (sign) return number | (1 << bitPosition);&nbsp; const mask = ~(1 << bitPosition);&nbsp; return number & mask;}function getBit(number, bitPosition) {&nbsp; return (number & (1 << bitPosition)) === 0 ? 0 : 1;}function createWatermark(imageAsBase64, text) {&nbsp; return new Promise(resolve => {&nbsp; &nbsp; const image = new Image();&nbsp; &nbsp; image.src = imageAsBase64;&nbsp; &nbsp; image.onload = () => {&nbsp; &nbsp; &nbsp; const canvas = document.createElement('canvas');&nbsp; &nbsp; &nbsp; canvas.width = image.width;&nbsp; &nbsp; &nbsp; canvas.height = image.height;&nbsp; &nbsp; &nbsp; const context = canvas.getContext('2d');&nbsp; &nbsp; &nbsp; context.drawImage(image, 0, 0);&nbsp; &nbsp; &nbsp; const imageData = context.getImageData(0, 0, canvas.width, canvas.height);&nbsp; &nbsp; &nbsp; const bits = stringToBits(text);&nbsp; &nbsp; &nbsp; for (let i = 0; i < text.length * 8; i += 4) {&nbsp; &nbsp; &nbsp; &nbsp; imageData.data[i] = setBit(imageData.data[i], 0, bits[i]);&nbsp; &nbsp; &nbsp; &nbsp; imageData.data[i + 1] = setBit(imageData.data[i + 1], 0, bits[i + 1]);&nbsp; &nbsp; &nbsp; &nbsp; imageData.data[i + 2] = setBit(imageData.data[i + 2], 0, bits[i + 2]);&nbsp; &nbsp; &nbsp; &nbsp; imageData.data[i + 3] = setBit(imageData.data[i + 3], 0, bits[i + 3]);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; context.putImageData(imageData, 0, 0);&nbsp; &nbsp; &nbsp; resolve(canvas.toDataURL());&nbsp; &nbsp; }&nbsp; });}function readWatermark(base64, textLength) {&nbsp; return new Promise(resolve => {&nbsp; &nbsp; const image = new Image();&nbsp; &nbsp; image.src = base64;&nbsp; &nbsp; image.onload = () => {&nbsp; &nbsp; &nbsp; const canvas = document.createElement('canvas');&nbsp; &nbsp; &nbsp; canvas.width = image.width;&nbsp; &nbsp; &nbsp; canvas.height = image.height;&nbsp; &nbsp; &nbsp; const context = canvas.getContext('2d');&nbsp; &nbsp; &nbsp; context.drawImage(image, 0, 0);&nbsp; &nbsp; &nbsp; const imageData = context.getImageData(0, 0, canvas.width, canvas.height);&nbsp; &nbsp; &nbsp; const bits = imageData.data.slice(0, textLength * 8);&nbsp; &nbsp; &nbsp; resolve(bitsToString(bits));&nbsp; &nbsp; }&nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript