Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java program that can embed and extract a simple text message into a chosen PNG image. The program should also allow extraction of

Write a Java program that can embed and extract a simple text message into a chosen PNG image. The program should also allow extraction of the message from the stegotext image. In particular, the program should do the following:

1. Prompts user to enter a choice of PNG image to hide a text message.

2. Prompts user to enter the text message that is to be hidden.

3. Create a modified image after hiding the text message (as explained above) and save the image as a new PNG picture.

4. Display the hidden message when a PNG image is supplied. [For testing purpose, the user may choose to use a different image file that already has a message hidden into it following the above scheme.]

-------------------------------------

A sample Java code (PixelData.java) is provided to give an idea how to get R, G, B values of a pixel in a PNG image. You can build on that, but you need to explore BufferedImage class, Color class, ImageIO package in JavaFX, getRGB(), setRGB() methods etc.from Java API.

-------------------------------------

Here's the PixelData.java code:

import java.util.*; import java.io.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage;

public class PixelBits {

public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the input image file name (with .png extension): "); String file = input.next();

BufferedImage img;

try { img = ImageIO.read(new File(file)); // There is a method ImageIO.write() to create a // picture file from a BufferedImage object int h = img.getHeight(); int w = img.getWidth(); int totalpixel = h*w; System.out.println(" Image height = " + h + " and Image width = " + w); System.out.println(" Total number of pixels = " + totalpixel); System.out.println(" In each pixel you have 3 components, R-G-B. Hence you can embed 3 bits in each pixel."); System.out.println(" Total number of bits that can be embedded in the picture = " + totalpixel*3); System.out.println(" Total number of ASCII characters that can be embedded = " + (totalpixel*3)/8); // store each pixel's RGB values int[][] pixelData = new int[h*w][3]; int[] rgb;

int numberOfPixels = 0; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ rgb = PixelData(img, j, i);

for(int k = 0; k < rgb.length; k++){ pixelData[numberOfPixels][k] = rgb[k]; }

numberOfPixels++; } }

} catch (IOException e) { e.printStackTrace(); }

}

private static int[] PixelData(BufferedImage img, int x, int y) { int argb = img.getRGB(x, y); int[] rgb = new int[3]; rgb[0] = (argb >> 16) & 0xff; //Red rgb[1] = (argb >> 8) & 0xff; // Green rgb[2] = (argb) & 0xff; // Blue // 0xff is Hex for 255 and used as a mask to get last 8 bits of a bit string // Remove the comment from the next line if you want to display RGB values for all pixels. // Caution: There can be too many pixels to display. So use it if absolutely necessary. //System.out.println("Red: " + rgb[0] + ", Green: " + rgb[1] + ", Blue: " + rgb[2]); return rgb; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions