Text Encoding /Decoding int Image -
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.xml.ws.http.HTTPException;
public class BinaryWrite {
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] data = null;
ByteArrayOutputStream bas = null;
try {
BufferedImage imgToServe=null;
imgToServe=getImageIO("C:\\imgguna.jpg");
encodeImage(imgToServe);
byte[][] pixels = new byte[imgToServe.getWidth()][];
for (int x = 0; x < imgToServe.getWidth(); x++) {
pixels[x] = new byte[imgToServe.getHeight()];
for (int y = 0; y < imgToServe.getHeight(); y++) {
pixels[x][y] = (byte) (imgToServe.getRGB(x, y) == 0xFFFFFFFF ? 0 : 1);
}
}
BlackWhiteEncode (pixels);
/*
bas = new ByteArrayOutputStream();
ImageIO.write(imgToServe, "jpg", bas);
File f = new File("C:\\imgguna.jpg");
ImageIO.write(imgToServe, "jpg", f);
data = bas.toByteArray();
String str = "";
for (int i = 0; i < data.length; i++) {
str = str + toBinary(data[i]);
}
System.out.println(str);
*/
}
catch(Exception ex)
{
}
}
private static BufferedImage getImageIO(String sFileName)
{
BufferedImage image=null;
File f = new File(sFileName);
//assume file is the image file
try {
image = ImageIO.read(f);
}
catch (IOException e) {
System.out.println("Invalid image file: " + sFileName);
}
return image;
}
private static BufferedImage getImageURL(String sURL)
{
BufferedImage imgToServe = null;
try {
URL u = new URL(sURL);
HttpURLConnection con1 = (HttpURLConnection) u.openConnection();
con1.setAllowUserInteraction(true);
con1.setRequestMethod("GET");
con1.connect();
InputStream is = con1.getInputStream();
if (is != null)
imgToServe = ImageIO.read(is);
}
catch (HTTPException he) {
} catch (IOException ioe) {
}
return imgToServe;
}
private static void BlackWhiteEncode(byte[][] pixels)
{
for(int row =0 ; row < pixels.length && row<8 p="" row=""> {
for(int column =0; column
System.out.print(pixels[row][column]);
}
}
}
private static String toBinary(byte b) {
StringBuilder sb = new StringBuilder("00000000");
for (int bit = 0; bit < 8; bit++) {
if (((b >> bit) & 1) > 0) {
sb.setCharAt(7 - bit, '1');
}
}
return (sb.toString());
}
private static int[] byteToInt(String str) {
byte[] data = str.getBytes();
int[] ints = new int[data.length];
for (int i = 0; i < data.length; i++) {
ints[i] = data[i] & 0xFFFFFFFF;
}
return ints;
}
private static int[] getBinary()
{
String str = "Guna";
String binary ="";
for(int i = 0; i < str.length(); i++) {
int number = Integer.parseInt(String.valueOf(str.charAt(i)), 16);
binary = binary+ Integer.toBinaryString(number);
System.out.print(binary);
}
int[] intArray = new int[binary.length()];
for(int i=0;i
}
return intArray;
}
private static void encodeImage(BufferedImage image)
{
image = resizeImage(image,80,80);
Color myblock=new Color(0,0,0);//Color Black
Color myWhite = new Color(255, 255, 255); // Color white
int[] text= getBinary();
try
{
int count=0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 10; j++) {
if(count < text.length)
{
if(text[count] == 0)
image.setRGB(i, j, Color.BLACK.getRGB());
else
image.setRGB(i, j, Color.WHITE.getRGB());
}
else
{
image.setRGB(i, j, Color.WHITE.getRGB());
}
count++;
}
}
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", bas);
File f = new File("C:\\imgguna.jpg");
ImageIO.write(image, "jpg", f);
}
catch (Exception ex)
{
System.out.print(ex);
}
}
private static BufferedImage resizeImage(BufferedImage originalImage, Integer img_width, Integer img_height)
{
int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizedImage = new BufferedImage(img_width, img_height, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, img_width, img_height, null);
g.dispose();
return resizedImage;
}
private static void readBinaryString()
{
String s = "0110100001100101011011000110110001101111";
String str = "";
for (int i = 0; i < s.length()/8; i++) {
int a = Integer.parseInt(s.substring(8*i,(i+1)*8),2);
str += (char)(a);
}
System.out.println(str);
}
}
No comments:
Post a Comment