Encoding Screen

For the Above UI, we will create a EncodeClass, file naming encode.java Class.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class EncodeClass {
int width, panelWidth, panelHeight;// =1280;
int height; // =720;
JFrame frame;
JPanel actionPanel;
JFrame encodingFrame;
JLabel outputImage;
JButton open = new JButton("Open"), embed = new JButton("Embed"), save = new JButton("Save into new file"),
reset = new JButton("Reset");
JTextArea message = new JTextArea(10, 3);
BufferedImage sourceImage = null, embeddedImage = null;
//Constructor
public EncodeClass(int width, int height, JFrame dashboardFrame) {
this.width = width;
this.height = height;
this.encodingFrame = dashboardFrame;
getEnodingView(width, height);
}
}
//Main Encoding View
private void getEnodingView(int width, int height) {
// frame with control options
frame = new JFrame();
Util.centeredFrame(frame, width, height, "Steganography");
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// loginFrame.setResizable(false);
ImageIcon icon = new ImageIcon("ic_lock_black.png");
frame.setIconImage(icon.getImage());
// frame.setLayout(null);
frame.setResizable(false);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
frame.dispose();
encodingFrame.setVisible(true);
}
});
Container frameContainer = frame.getContentPane();
frameContainer.setBackground(Color.green);
int frameControllerSize = (int) (height * 5.7 / 100);
// Changeable According to What size of screen You Want?
int containerWidth = width * 94 / 100;
int containerHeight = height * 90 / 100;
panelWidth = containerWidth * 98 / 100;
panelHeight = containerHeight * 96 / 100;
int posX = width / 2 - panelWidth / 2 - 8;
int posY = height / 2 - panelHeight / 2 - frameControllerSize / 2;
// frame without control options
JPanel panel = new JPanel();
panel.setLayout(null);// new BorderLayout());
// panel.setOpaque(false);
panel.setBackground(Color.decode("#FFFFFF"));
panel.setSize(new Dimension(panelWidth, panelHeight));
panel.setLocation(posX, posY);
frameContainer.add(panel);
// for Rounded Background
posX = width / 2 - containerWidth / 2 - 8;
posY = height / 2 - containerHeight / 2 - frameControllerSize / 2;
JComponent jcomponent = new Background(posX, posY, containerWidth, containerHeight);
jcomponent.setLayout(null);
frameContainer.add(jcomponent, BorderLayout.CENTER);
// for heading Layout
posY = panelHeight * 2 / 100;
Font headingFont = new Font("Serif", Font.PLAIN, 30);
// header
// borderline = BorderFactory.createLineBorder(Color.black);
JLabel heading_text = new JLabel("Steganography");
heading_text.setHorizontalAlignment(JLabel.CENTER);
heading_text.setVerticalAlignment(JLabel.CENTER);
heading_text.setBackground(Color.decode("#80c2b2"));
heading_text.setForeground(Color.black);
heading_text.setFont(headingFont);
heading_text.setBounds(0, 0, panelWidth, panelHeight / 9);
panel.add(heading_text);
headingFont = new Font("Serif", Font.PLAIN, 15);
JLabel heading_text_minor = new JLabel("Encoding");
heading_text_minor.setHorizontalAlignment(JLabel.CENTER);
heading_text_minor.setVerticalAlignment(JLabel.CENTER);
heading_text_minor.setBackground(Color.decode("#80c2b2"));
heading_text_minor.setForeground(Color.black);
heading_text_minor.setFont(headingFont);
heading_text_minor.setBounds(0, panelHeight * 6 / 100, panelWidth, panelHeight / 9);
panel.add(heading_text_minor);
panel.add(Util.getSeparator(0, panelHeight / 7, panelWidth, 5, "000000"));
///////////
int infoPanelWidth = panelWidth * 40 / 100;
JPanel infoPanel = new JPanel();
infoPanel.setLayout(null);
infoPanel.setBackground(Color.WHITE);
infoPanel.setBounds(panelWidth / 40, panelHeight * 22 / 100, infoPanelWidth, panelHeight * 70 / 100);
infoPanel.setBorder(Util.getTitledBorder(16, "Details"));
panel.add(infoPanel, BorderLayout.CENTER);
Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
TitledBorder secretMsgTitle = BorderFactory.createTitledBorder(loweredetched, "Secret Message");
secretMsgTitle.setTitleJustification(TitledBorder.LEFT);
int fieldWidth = panelWidth / 4;
// message = JTextArea(10,3);
message.setBounds((infoPanelWidth) / 2 - (fieldWidth) / 2, panelHeight * 10 / 100, fieldWidth,
panelHeight * 30 / 100);
message.setBorder(secretMsgTitle);
infoPanel.add(message);
int posy = panelHeight * 50 / 100;
open = new JButton(new ImageIcon(((new ImageIcon("images/btn.png")).getImage()).getScaledInstance(120, 30,
java.awt.Image.SCALE_SMOOTH)));
open.setBackground(Color.BLUE);
open.setBounds((infoPanelWidth * 20 / 100) - 60, posy, 120, 30);
open.setBorder(null);
open.setLayout(null);
open.setContentAreaFilled(false);
open.setOpaque(false);
infoPanel.add(open);
open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
openImage();
}
});
JLabel text = new JLabel("Open");
text.setHorizontalAlignment(JLabel.CENTER);
text.setBackground(Color.decode("#80c2b2"));
text.setForeground(Color.black);
text.setFont(new java.awt.Font("Serif", 0, 20));
text.setBounds(0, -2, 120, 30);
open.add(text);
save = new JButton(new ImageIcon(((new ImageIcon("images/btn.png")).getImage()).getScaledInstance(120, 30,
java.awt.Image.SCALE_SMOOTH)));
save.setBackground(Color.ORANGE);
save.setBounds((infoPanelWidth) / 2 - 60, posy, 120, 30);
save.setBorder(null);
save.setLayout(null);
save.setContentAreaFilled(false);
save.setOpaque(false);
infoPanel.add(save);
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
saveImage();
}
});
text = new JLabel("Save");
text.setHorizontalAlignment(JLabel.CENTER);
text.setBackground(Color.decode("#80c2b2"));
text.setForeground(Color.black);
text.setFont(new java.awt.Font("Serif", 0, 20));
text.setBounds(0, -2, 120, 30);
save.add(text);
reset = new JButton(new ImageIcon(((new ImageIcon("images/btn.png")).getImage()).getScaledInstance(120, 30,
java.awt.Image.SCALE_SMOOTH)));
reset.setBackground(Color.GRAY);
reset.setBorder(null);
reset.setLayout(null);
reset.setContentAreaFilled(false);
reset.setOpaque(false);
reset.setBounds(infoPanelWidth * 80 / 100 - 60, posy, 120, 30);
infoPanel.add(reset);
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
resetInterface();
}
});
text = new JLabel("Reset");
text.setHorizontalAlignment(JLabel.CENTER);
text.setBackground(Color.decode("#80c2b2"));
text.setForeground(Color.black);
text.setFont(new java.awt.Font("Serif", 0, 20));
text.setBounds(0, -2, 120, 30);
reset.add(text);
embed = new JButton(new ImageIcon(((new ImageIcon("images/encode_btn.jpg")).getImage()).getScaledInstance(120,
30, java.awt.Image.SCALE_SMOOTH)));
embed.setBackground(Color.GRAY);
embed.setFont(new java.awt.Font("Serif", 0, 50));
embed.setBounds((infoPanelWidth) / 2 - 60, posy + panelHeight * 10 / 100, 120, 30);
embed.setBorder(null);
embed.setLayout(null);
embed.setContentAreaFilled(false);
embed.setOpaque(false);
infoPanel.add(embed);
embed.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
embedMessage();
}
});
text = new JLabel("Encode");
text.setHorizontalAlignment(JLabel.CENTER);
text.setBackground(Color.decode("#80c2b2"));
text.setForeground(Color.black);
text.setFont(new java.awt.Font("Serif", 0, 20));
text.setBounds(0, -2, 120, 30);
embed.add(text);
///////////////// Action Layout
actionPanel = new JPanel();
actionPanel.setLayout(null);
actionPanel.setBackground(Color.WHITE);
actionPanel.setBounds(panelWidth * 3 / 4 - (panelWidth * 45 / 100) / 2, panelHeight * 20 / 100,
panelWidth * 45 / 100, panelHeight * 75 / 100);
actionPanel.setBorder(Util.getTitledBorder(16, "Output"));
panel.add(actionPanel, BorderLayout.CENTER);
text = new JLabel("Image for Encoding");
text.setHorizontalAlignment(JLabel.CENTER);
text.setBackground(Color.decode("#80c2b2"));
text.setForeground(Color.black);
text.setFont(new java.awt.Font("Serif", 0, 30));
text.setBounds((panelWidth * 45 / 100) / 2 - (panelWidth * 30 / 100) / 2, panelHeight * 5 / 100,
panelWidth * 30 / 100, 40);
actionPanel.add(text);
int imgW = panelWidth * 40 / 100;
int imgH = panelHeight * 50 / 100;
outputImage = new JLabel("Select image to encode");
outputImage.setHorizontalAlignment(JLabel.CENTER);
outputImage.setFont(new java.awt.Font("Serif", 0, 14));
outputImage.setBounds((panelWidth * 45 / 100) / 2 - imgW / 2, panelHeight * 18 / 100, imgW, imgH);
actionPanel.add(outputImage);
frame.setVisible(true);
}
Now, we will create the method for importing image from the file.
private void openImage() {
java.io.File f = showFileDialog(true);
try {
sourceImage = ImageIO.read(f);
int imgW = panelWidth * 40 / 100;
int imgH = panelHeight * 55 / 100;
ImageIcon backround_img1 = new ImageIcon(((new ImageIcon(sourceImage)).getImage()).getScaledInstance(imgW,
imgH, java.awt.Image.SCALE_SMOOTH));
Image img1 = backround_img1.getImage();
Image temp_img1 = img1.getScaledInstance(imgW, imgH, Image.SCALE_SMOOTH);
backround_img1 = new ImageIcon(temp_img1);
outputImage.setIcon(backround_img1);
outputImage.setText("");
frame.validate();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private java.io.File showFileDialog(final boolean open) {
JFileChooser fc = new JFileChooser("Open an image");
javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
public boolean accept(java.io.File f) {
String name = f.getName().toLowerCase();
if (open)
return f.isDirectory() || name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".png")
|| name.endsWith(".gif") || name.endsWith(".tiff") || name.endsWith(".bmp")
|| name.endsWith(".dib");
return f.isDirectory() || name.endsWith(".png") || name.endsWith(".bmp");
}
public String getDescription() {
if (open)
return "Image (*.jpg, *.jpeg, *.png, *.gif, *.tiff, *.bmp, *.dib)";
return "Image (*.png, *.bmp)";
}
};
fc.setAcceptAllFileFilterUsed(false);
fc.addChoosableFileFilter(ff);
java.io.File f = null;
if (open && fc.showOpenDialog(frame) == fc.APPROVE_OPTION)
f = fc.getSelectedFile();
else if (!open && fc.showSaveDialog(frame) == fc.APPROVE_OPTION)
f = fc.getSelectedFile();
return f;
}
///Embed Message Code
private void embedMessage() {
if (sourceImage != null) {
String mess = message.getText();
embeddedImage = sourceImage.getSubimage(0, 0, sourceImage.getWidth(), sourceImage.getHeight());
embedImageMessage(embeddedImage, mess);
JOptionPane.showMessageDialog(frame, "Your Secret message has been Encoded !", "Encoded Successfully",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(frame, "Select your Image first", "Failed", JOptionPane.INFORMATION_MESSAGE);
}
}
private void embedImageMessage(BufferedImage img, String mess) {
int messageLength = mess.length();
int imageWidth = img.getWidth(), imageHeight = img.getHeight(), imageSize = imageWidth * imageHeight;
if (messageLength * 8 + 32 > imageSize) {
JOptionPane.showMessageDialog(frame, "Message is too long for the chosen image", "Message too long!",
JOptionPane.ERROR_MESSAGE);
return;
}
embedInteger(img, messageLength, 0, 0);
byte b[] = mess.getBytes();
for (int i = 0; i < b.length; i++)
embedByte(img, b[i], i * 8 + 32, 0);
}
private void embedInteger(BufferedImage img, int n, int start, int storageBit) {
int maxX = img.getWidth(), maxY = img.getHeight(), startX = start / maxY, startY = start - startX * maxY,
count = 0;
for (int i = startX; i < maxX && count < 32; i++) {
for (int j = startY; j < maxY && count < 32; j++) {
int rgb = img.getRGB(i, j), bit = getBitValue(n, count);
rgb = setBitValue(rgb, storageBit, bit);
img.setRGB(i, j, rgb);
count++;
}
}
}
private void embedByte(BufferedImage img, byte b, int start, int storageBit) {
int maxX = img.getWidth(), maxY = img.getHeight(), startX = start / maxY, startY = start - startX * maxY,
count = 0;
for (int i = startX; i < maxX && count < 8; i++) {
for (int j = startY; j < maxY && count < 8; j++) {
int rgb = img.getRGB(i, j), bit = getBitValue(b, count);
rgb = setBitValue(rgb, storageBit, bit);
img.setRGB(i, j, rgb);
count++;
}
}
}
private int getBitValue(int n, int location) {
int v = n & (int) Math.round(Math.pow(2, location));
return v == 0 ? 0 : 1;
}
private int setBitValue(int n, int location, int bit) {
int toggle = (int) Math.pow(2, location), bv = getBitValue(n, location);
if (bv == bit)
return n;
if (bv == 0 && bit == 1)
n |= toggle;
else if (bv == 1 && bit == 0)
n ^= toggle;
return n;
}
//Save Embedded Image
private void saveImage() {
if (embeddedImage == null) {
JOptionPane.showMessageDialog(frame, "No message has been embedded!", "Nothing to save",
JOptionPane.ERROR_MESSAGE);
return;
}
java.io.File f = showFileDialog(false);
String name = f.getName();
String ext = name.substring(name.lastIndexOf(".") + 1).toLowerCase();
if (!ext.equals("png") && !ext.equals("bmp") && !ext.equals("dib")) {
ext = "png";
f = new java.io.File(f.getAbsolutePath() + ".png");
}
try {
if (f.exists())
f.delete();
ImageIO.write(embeddedImage, ext.toUpperCase(), f);
} catch (Exception ex) {
ex.printStackTrace();
}
JOptionPane.showMessageDialog(frame, "Your Encoded image has been saved !", "Saved Successfully",
JOptionPane.INFORMATION_MESSAGE);
}
After saving the embedded image, let's reset the all the data.
//Reset UI
private void resetInterface() {
outputImage.setIcon(null);
outputImage.setText("Select image to decode");
message.setText("");
sourceImage = null;
embeddedImage = null;
frame.validate();
}
Lastly, we will create a method for returning frame.
public Window getFrameRef() {
// TODO Auto-generated method stub
return frame;
}
We have successfully created the Encoding screen with functional code.
Programmer Mirta is for learning and training. Projects might be simple to improve learning. Projects are constantly reviewed to avoid errors, but we cannot assure full correctness of all content. While using Programmer Mitra, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2021 by Programmer Mitra. All Rights Reserved.