Created
March 21, 2024 10:46
-
-
Save techsin/be952fa5e548469e337e7d8cc8d24c18 to your computer and use it in GitHub Desktop.
encoding into image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function to encode secret data into image pixels | |
function encodeDataIntoImage(imageData, secretData) { | |
// Assuming imageData is a Uint8ClampedArray representing pixel data of the image | |
// and secretData is the string to be hidden | |
// Convert secretData into binary | |
let binarySecret = secretData.split('').map(char => char.charCodeAt(0).toString(2)).join(''); | |
// Embed binary data into least significant bits of image pixel values | |
for (let i = 0; i < binarySecret.length; i++) { | |
imageData[i] = (imageData[i] & 0xFE) | Number(binarySecret[i]); | |
} | |
return imageData; | |
} | |
// Usage example | |
let canvas = document.createElement('canvas'); | |
let ctx = canvas.getContext('2d'); | |
let image = new Image(); | |
image.onload = function() { | |
canvas.width = image.width; | |
canvas.height = image.height; | |
ctx.drawImage(image, 0, 0); | |
let imageData = ctx.getImageData(0, 0, canvas.width, canvas.height).data; | |
let secretData = "This is a secret message."; | |
// Embed secret data into image pixels | |
let encodedImageData = encodeDataIntoImage(imageData, secretData); | |
// Display the modified image | |
ctx.putImageData(new ImageData(encodedImageData, canvas.width, canvas.height), 0, 0); | |
// Append the canvas to the document or send it as part of an HTTP request | |
}; | |
image.src = 'image.jpg'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment