The values of the elements in the image array are null when not inside the

I need help converting an image into a Uint8Array for use in F5 steganography with the f5stego package.

After trying to implement this, I encountered an issue where the imageArray contains all zeroes when printed outside the function, but inside it holds the actual image data values.

var stegKey = [1, 2, 3, 4, 5, 6, 7, 8, 9];

var stegger = new f5stego(stegKey); // initialize stegger with key

var canvas = document.createElement('canvas');
ctx = canvas.getContext('2d'), base64 = 'cover.jpg';
canvas.width = 43; 
canvas.height = 34;
var image = new Image();
var imageArray = new Uint8Array(4624);
image.onload = (function (canvas, ctx) {
    return function () {
        ctx.drawImage(this, 0, 0);
        var imageData = ctx.getImageData(0, 0, 34, 34);
        // console.log(imageData.data);
        imageArray = imageData.data;
        console.log(imageArray);
     };
})(canvas, ctx);
image.src = base64;

console.log(imageArray);

// Data Array 
var message = "something secret ";
dataArray = new Uint8Array(message.length);
for (var i = 0, j = message.length; i < j; ++i) {
    dataArray[i] = message.charCodeAt(i);
}
console.log(dataArray);

//embed message into image
var secretImage = stegger.embed(imageArray, dataArray);
console.log(secretImage);

//extract message from image
var extractedMessage = stegger.extract(secretImage);
console.log(extractedMessage);

Answer №1

If you are trying to convert an image into a Uint8Array of pixels, just keep in mind that the f5stego.js library requires the actual JPEG file bytes, not just the pixels themselves.

For those working with a JPEG file that is encoded in base64 format, you will need a library that can help convert this string into a Uint8Array. One example of such a library can be found here.

If you decide to use the base64 library provided in the link above, your code should look something like this:

// Your JavaScript code here

In case you want to modify an image using the canvas element and then utilize it with f5stego.js, remember to use the toDataURL function instead of getImageData for converting the image data into Base64 encoding.

It is important to note that the capacity of the f5 algorithm is approximately 10% of the original file size. This means that embedding 1KB of data would require a minimum of a 10KB jpeg file.

Additionally, when converting a string into a Uint8Array, consider that using charCodeAt may only work effectively with ASCII characters and could fail when dealing with UTF-8 symbols.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Consistent Errors with AJAX POST Requests Despite CORS Enablement

Here is a function I have created for making an ajax post request: function POST(url, data) { $.ajax({ 'type' : "POST", 'url' : url, 'data' : data, headers : { 'Access-Cont ...

Instructions for adding an onfocus event listener to an input field in order to dynamically change the formatting of associated labels

I'm looking to change the style of my input labels to a more fancy look by implementing the .fancyclass style when using the onfocus event on the input field. I am curious to know how this can be achieved through event listeners in Javascript? ...

Encountering a type error when making an Ajax request in JavaScript/jQuery

Encountering an error while trying to extract a value within a loop using jQuery. The error message is displayed below. Uncaught TypeError: Cannot read property 'no_of_optional' of undefined Below is the code I am working with. var data = $.pa ...

Utilizing Asynchronous Functions within a Loop

I have a situation where I am working with an array and need to make API calls for each index of the array. Once the API call is resolved, I need to append the result in that specific element. My goal is to ultimately return the updated array once this pro ...

What is the most effective method for preserving RichText (WYSIWYG output)?

I am currently using a JavaScript-based rich text editor in my application. Could you suggest the most secure method to store the generated tags? My database is MySQL, and I have concerns about the safety of using mysql_real_escape_string($text);. ...

Extract the data from a deeply nested key within a JSON object

I'm currently working on a function that takes a key (specified as a string) and retrieves its corresponding values from a given JSON object. Here is the JSON data I am working with: [ { "some_key1": [ {"key": "va ...

Breaking down a statement into individual elements within an array, incorporating keys alongside the elements within the array

let condition = "[AccNum]==true&&[AccNum]==[ARID]&&[AccNum]==aaaa || [ARID]!=true&&[DOB]&gt;[ARID] || [DOB]&gt;bbb&&[DOS]&gt;=[ARID]&&[DOS]&lt;[Gender]&&[66642]&lt;=cccc&&[66642] I ...

Ways to transfer Material-UI FlatButton CSS to a different Button element

I've recently incorporated a loading button object into my website from (https://github.com/mathieudutour/react-progress-button), and now I want to customize it using the Material-UI FlatButton CSS. However, I'm not quite sure how to achieve this ...

Tips for modifying a request api through a select form in a REACT application

Apologies if this question seems a bit basic, but I need some help. I am working on creating a film catalog using The Movie Database API. I have successfully developed the home and search system, but now I am struggling to implement a way to filter the fi ...

Does the method in the superclass "not exist" within this type....?

Our TS application utilizes a JavaScript library, for which we crafted a .d.ts file to integrate it with TypeScript. Initially, the file resided in a "typings" directory within the project and everything operated smoothly. Subsequently, we opted to relocat ...

Trigger Google Analytics event when file is downloaded from server

Here is a helpful resource on sending events to Google Analytics via API server sided: Send event to Google Analytics using API server sided An email containing a download link has been sent to a customer: Hello, Please access your product through thi ...

Retrieving ng-pattern as a variable from a service

Hey there! I'm currently working on an application that requires extensive form validation across multiple pages. To streamline this process, I am attempting to extract validation patterns from a service used among the controllers. However, I've ...

Angular and Node.js Integration: Compiling Files into a Single File

I have a question, is it possible to include multiple require js files in one file? If so, how can I create objects from them? Calling 'new AllPages.OnePage()' doesn't seem to work. To provide some context, I'm looking for something sim ...

How can I display a Skeleton when pressing pagination buttons and then turn it off after 3 seconds?

Here is a snippet of my code featuring the use of a Skeleton as a placeholder for CardMedia: {loading ? ( <Skeleton variant="rectangular" width={308} height={420} animation="wave" /> ) : ( <CardMedia s ...

Having trouble triggering a click event on Ant Design menu button using jest and enzyme

Troubleshooting the simulation of a click event on the Menu component using Antd v4.3.1 Component: import React from 'react' import PropTypes from 'prop-types' import { Menu } from 'antd' import { SMALL_ICONS, PATHS } fro ...

I am encountering an issue where the POST data is not being successfully sent using XMLHttpRequest unless I include

I have a unique website where users can input a cost code, which is then submitted and POSTed to a page called 'process-cost-code.php'. This page performs basic validation checks and saves the information to a database if everything is correct. T ...

Encountering Issues with Accessing Property

Upon trying to run my code, the console is displaying an error that I am unable to resolve. The error specifically states: "TypeError: Cannot read property 'author' of undefined." View the StackBlitz project here The error seems to be coming fr ...

Pug Template Not Displaying Emojis or Special Characters in Sendgrid Email Subject Line

There seems to be an issue with rendering emojis or special characters in the subject header of the pug file, although they work perfectly in the body. I have compiled both the body and subject header separately using pug. const compileHtmlFunction = pug.c ...

Is the variable empty outside of the subscribe block after it's been assigned?

Why is a variable assigned inside subscribe empty outside subscribe? I understand that subscribe is asynchronous, but I'm not sure how to use await to render this variable. Can someone please help me and provide an explanation? I am attempting to retr ...

Could not find yarn command even after installation with npm

Following the guidelines for yarn v2 installation, I tried to install using npm install -g yarn. I executed sudo npm install -g yarn on Ubuntu 20.04. However, after running this command, it returned "command not found." ❯ sudo npm install -g yarn > ...