-
Error with ImageIO
This is my code for encryption and to load image. i got this error while compiling
ImageIO.write(images, "png", bin);
Code:
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
public class encrypt{
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
System.out.println ("Enter Message: ");
String msg = input.nextLine();
System.out.println ("Enter Key: ");
String key = input.nextLine();
byte[] bytes2 = msg.getBytes();
StringBuilder binary2 = new StringBuilder();
for (byte b : bytes2) {
int val = b;
for (int i = 0; i < 8; i++) {
binary2.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
}
if(key.length() == 25){
byte[] bytes = key.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes) {
int val = b;
for (int i = 0; i < 8; i++) {
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
}
//initializing the register using binary key
boolean[] binarykey = new boolean[200];
boolean[] ciphertext = new boolean[200];
boolean[] binarymsg = new boolean[binary2.length()];
//System.out.println("length = "+binary.length());
String tab = input.nextLine(); // just for tab, doesn't meaning anything
for (int i=0; i < binary2.length(); i++) {
if (binary2.charAt(i) == '1')
binarymsg[i] = true;
else if (binary2.charAt(i) == '0')
binarymsg[i] = false;
}
for (int i=0; i < binary.length();i++) { //the register will run based on length of key
if (binary.charAt(i) == '1')
binarykey[i] = true;
else if (binary.charAt(i) == '0')
binarykey[i] = false;
}
System.out.print("cipher = ");
for (int i = 0; i < binary2.length(); i++) {
// the tap use to XOR the binary
boolean bit = binarykey[199] ^ binarykey[173] ^ binarykey[149] ^ binarykey[124] ^ binarykey[98] ^
binarykey[73] ^ binarykey[49] ^ binarykey[24];
ciphertext[i] = binarykey[199] ^ binarymsg[i];
if (ciphertext[i] == true)
System.out.print('1');
else if(ciphertext[i] == false)
System.out.print('0');
}
System.out.println("");
System.out.println("");
}
else
{
System.out.println();
System.out.println("you have reached the length limit of the key");
System.out.println("Please enter key not exceed 25 Characters");
}
File images = new File("tux.png");
BufferedImage image = ImageIO.read(images);
ByteArrayOutputStream bin = new ByteArrayOutputStream();
ImageIO.write(images, "png", bin);
byte[] pngByteArray = bin.toByteArray();
StringBuilder sb = new StringBuilder();
for (byte by : pngByteArray)
sb.append(Integer.toBinaryString(by & 0xFF));
System.out.println(sb.toString());
}
}
Anyone can help????
-
what was the error message?
-
encrypt.java:128: cannot find symbol
symbol : method write(java.io.File,jav
location: class javax.imageio.ImageIO
-
it means the method 'write' doesn't exist, or maybe it does but you used it with incorrect parameters so Java didn't recognise the method signature.
Code:
ImageIO.write(images, "png", bin);
Code:
static boolean write(RenderedImage im, String formatName, File output)
Writes an image using an arbitrary ImageWriter that supports the given format to a File.
static boolean write(RenderedImage im, String formatName, ImageOutputStream output)
Writes an image using the an arbitrary ImageWriter that supports the given format to
an ImageOutputStream.
static boolean write(RenderedImage im, String formatName, OutputStream output)
Writes an image using an arbitrary ImageWriter that supports the given format to an OutputStream.
-
your variable 'images' is of the type java.io.File, but what you need is a RenderedImage type