image读写java.lang.IllegalArgumentException

时间:2014-09-04 18:44:57

标签: java file-io javax.imageio

我想创建一个非常简单的加密/解密项目。但首先我想读取jpg文件并将其写入具有给定密码的文件,然后再次读取该文件并检查文件中的密码和提供的密码,但我得到:

Exception in thread "main" java.lang.IllegalArgumentException: im == null!
    at javax.imageio.ImageIO.write(Unknown Source)
    at javax.imageio.ImageIO.write(Unknown Source)
    at GSM.AES.deccryption(AES.java:105)
    at GSM.AES.main(AES.java:27)

我的代码:

public static void main(String args[]) 
        {
            myWrite();
            String encryptedFilePath = System.getProperty("user.dir")+ "\\"+"Encrypted"+".mmlooloo";
            String destinationFilePath = System.getProperty("user.dir") + "\\";
            try {
                myRead(encryptedFilePath,destinationFilePath,"123456");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

我的加密:

 public static void myWrite() {

            try {

                System.out.println("Plesase Enter Number Of Pages !!!!!");
                BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
                int numberOfPage = Integer.valueOf(bufferRead.readLine().toString());

                String dirName=  System.getProperty("user.dir")+"\\";


                byte[] base64StringEnc;
                ByteArrayOutputStream baos=new ByteArrayOutputStream(1000);
                FileOutputStream  myMatLabFileEnc = null;

                String filePath = System.getProperty("user.dir")+ "\\"+"Encrypted"+".mmlooloo";
                myMatLabFileEnc = new FileOutputStream (filePath);
                String imagFileName;
                String imgPathString;
                String password = "123456";
                myMatLabFileEnc.write(password.getBytes());
                myMatLabFileEnc.write("\n".getBytes());

               for(int i = 1 ; i<=numberOfPage ;i++)
               {    

                     imagFileName = Integer.toString(i) +".jpg";         
                     BufferedImage img=ImageIO.read(new File(dirName,imagFileName));
                     ImageIO.write(img, "jpg", baos);
                     baos.flush();

                     myMatLabFileEnc.write(baos.toByteArray());
                     myMatLabFileEnc.write("\n".getBytes());

                     baos.reset();
                     imgPathString = dirName + imagFileName;
                     File  f = new File(imgPathString);
                     f.delete();

               }
                     myMatLabFileEnc.close();
                     baos.close();
                     return;

                } catch (FileNotFoundException ex) {
                    System.out.println(ex.toString());
            }catch(IOException ex){
                System.out.println(ex.toString());
            }
        }

和我的解密:

public static int myRead(String encryptedfilePath,String encryptedFileDir,String inputPassword) throws FileNotFoundException, IOException{

            FileReader encryptedFile=new FileReader(encryptedfilePath);
            BufferedReader reader = new BufferedReader(encryptedFile);
            String encryptedImag;
            String encryptedSavesdPassword = reader.readLine();
            byte []encryptedInputPassword = inputPassword.getBytes();
            byte []temp = encryptedSavesdPassword.getBytes();
            if(!Arrays.equals(temp,encryptedInputPassword)){
                return -1;
            }
            int i = 1;

            while((encryptedImag = reader.readLine()) != null){

                byte[] bytearray = encryptedImag.getBytes();
                BufferedImage imagRecover=ImageIO.read(new ByteArrayInputStream(bytearray));
                String outputRecoverdFileName = Integer.toString(i)+"_recoverd.jpg";
                ImageIO.write(imagRecover, "jpg", new File(encryptedFileDir,outputRecoverdFileName));
                ++i;
            }

            return 1;

        }

和AES.java:105是:

ImageIO.write(imagRecover, "jpg", new File(encryptedFileDir,outputRecoverdFileName));

我检查imagRecover为空,但我不知道为什么?我想你可以试试它只是命名你的图像文件,如1.jpg,2.jpg等......

1 个答案:

答案 0 :(得分:2)

免责声明:这不是完整的答案,但评论时间太长了。

这就是我对评论的意思:

  

首先不要对图像进行解码/编码。只需复制字节。

使用此代码,您不会重新压缩JPEG,因此不会丢失质量。

而不是以下代码:

imagFileName = Integer.toString(i) +".jpg";         
BufferedImage img=ImageIO.read(new File(dirName,imagFileName));
ImageIO.write(img, "jpg", baos);
baos.flush();

只需将文件中的字节复制到baos,如下所示:

imagFileName = Integer.toString(i) +".jpg";         
InputStream input = new FileInputStream(new File(dirName, imagFileName));
try {
    copy(input, baos);
}
finally {
    input.close();
}

复制方法:

public void copy(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[1024];
    int len;

    while ((len = input.read(buffer, 0, buffer.length)) >= 0) {
        output.write(buffer, 0, len);
    }
}

同样,在解密部分中,替换:

byte[] bytearray = encryptedImag.getBytes();
BufferedImage imagRecover=ImageIO.read(new ByteArrayInputStream(bytearray));
String outputRecoverdFileName = Integer.toString(i)+"_recoverd.jpg";
ImageIO.write(imagRecover, "jpg", new File(encryptedFileDir,outputRecoverdFileName));

使用:

byte[] bytearray = encryptedImag.getBytes();
InputStream input = new ByteArrayInputStream(bytearray));
String outputRecoverdFileName = Integer.toString(i) + "_recoverd.jpg";
OutputStream output = new FileOutputStream(new File(encryptedFileDir, outputRecoverdFileName)));
try {
    copy(input, output);
}
finally {
    output.close();
}