Decoding Screen

We have successfully created our Encoding screen encode.java previously. Now let us Create the decoding screen.

Decoding UI

For the Above UI, we will create a DecodeClass, file naming decode.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 DecodeClass  {
		int width,panelWidth,panelHeight;//=1280;
		int height;//=720;
		JFrame frame;
		JPanel actionPanel;
		JFrame decodingFrame;
		JLabel outputImage;
		
		JButton open = new JButton("Open"), decoButton = new JButton("Decode"),
				reset = new JButton("Reset");
		JLabel message = new JLabel();
		BufferedImage image = null;

		//Constructor
		public DecodeClass(int width, int height, JFrame dashboardFrame) {
			this.width = width;
			this.height = height;
			this.decodingFrame=dashboardFrame;	
			getDecodingView(width,height);
			
			
		}
	}


Now, Let us create the getDecodingView(width, height); method, main decoding UI inside the DecodeClass.

	
	// Decoding Screen UI
	 private void getDecodingView(int width, int height) {
		
		// frame with control options
				frame = new JFrame();
				Util.centeredFrame(frame, width, height, "Steganography");
				//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();
			            	 decodingFrame.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("Decoding");
				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.setHorizontalAlignment(JLabel.CENTER);
		        message.setFont(new java.awt.Font("Serif",0, 15));
		        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);
				
		        
		        
		       		        
		        
				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);
		        
		      
		        
		        decoButton=new JButton(new ImageIcon(((new ImageIcon("images/encode_btn.jpg")).getImage()).getScaledInstance(120,30, java.awt.Image.SCALE_SMOOTH)));
		        decoButton.setBackground(Color.GRAY);		       
		        decoButton.setFont(new java.awt.Font("Serif",0, 50));
		        decoButton.setBounds((infoPanelWidth)/2-60,posy+panelHeight*10/100 ,120,30);  
		        decoButton.setBorder(null);
		        decoButton.setLayout(null);
		        decoButton.setContentAreaFilled(false);
		        decoButton.setOpaque(false);
		        infoPanel.add(decoButton);
  
		        decoButton.addActionListener(new ActionListener() {				
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						decodeMessage();
					}
				});
		        text = new JLabel("Decode");
		        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);
		        decoButton.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("Decoded Image");
		        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 decode");
		        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.

	

//Import Image code
	private void openImage() {
	java.io.File f = showFileDialog(true);
	try {   
		image = ImageIO.read(f);
		
		int imgW=panelWidth*40/100;
		int imgH=panelHeight*55/100;

		
		ImageIcon backround_img1 = new ImageIcon(image.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(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();
				return f.isDirectory() ||   name.endsWith(".png") || name.endsWith(".bmp");
				}
			public String getDescription() {
				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;
}
		
	

After importing the image. we have to decode the embedded message from the image. so lets write the methods that will help us for decoding the message.

	

	//Decode Image code
	 private void decodeMessage() {
		 if(image!=null)
		 {
	    int len = extractInteger(image, 0, 0);
	    byte b[] = new byte[len];
	    for(int i=0; i< len; i++)
	       b[i] = extractByte(image, i*8+32, 0);
	    message.setText(new String(b));
	    JOptionPane.showMessageDialog(frame, "Your Secret message has been Decoded !", 
		         "Decoded Successfully", JOptionPane.INFORMATION_MESSAGE);
		 }
		 else {
			 JOptionPane.showMessageDialog(frame, "Select your Image first", 
			         "Failed", JOptionPane.INFORMATION_MESSAGE);
		 }
	    }
	 
	 private int extractInteger(BufferedImage img, int start, int storageBit) {
	   int maxX = img.getWidth(), maxY = img.getHeight(), 
	       startX = start/maxY, startY = start - startX*maxY, count=0;
	    int length = 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(rgb, storageBit);
	          length = setBitValue(length, count, bit);
	          count++;
	          }
	       }
	    return length;
	    }

	 private byte extractByte(BufferedImage img, int start, int storageBit) {
	    int maxX = img.getWidth(), maxY = img.getHeight(), 
	       startX = start/maxY, startY = start - startX*maxY, count=0;
	    byte b = 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(rgb, storageBit);
	          b = (byte)setBitValue(b, count, bit);
	          count++;
	          }
	       }
	    return b;
	    }
	 
	 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;
		    }

	

After we have decoded the image, let's reset the all the data.

	
	 // Reset UI code
	 private void resetInterface() {
	    message.setText("");
	    image = null;
	    outputImage.setIcon(null);
	    outputImage.setText("Select image to decode");
	    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 Decoding 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.