无法在jar中读取图像 [英] Cannot read image in jar

查看:202
本文介绍了无法在jar中读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个程序来加密Netbeans中的图像。该程序从netbeans运行时工作正常,但是当我将其构建成.jar文件时,它不起作用,即使我将图像文件放在与.jar文件相同的文件夹中也无法读取图像。

i have written a program to encrypt an image in Netbeans. The program works fine when running from netbeans but when i build it into a .jar file its not working, it cannot read the image even though i placed the image file in the same folder as the .jar file.

    package test;

    import java.io.IOException;
    import java.io.File;
    /**
     *
 * @author AMaR
*/
public class Test {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException, Exception {


    File EnImage = new File("encrypted.png");
    File DeImage = new File("decrypted.png");
    int[] pixels;

    LoadImage l = new LoadImage();
    l.load();
    pixels= l.getImagePixels();

    RC4New rc4 = new RC4New();
    int key[]= {13,2,4,6,};
  //  int data[]={5,10,90,5};
    rc4.KSA(key);
    int[] text = rc4.PRNG(pixels);
    l.write((int)512,(int)512,text,EnImage);

    //RC4New rc41 = new RC4New();
    rc4.KSA(key);
    int[] text1 = rc4.PRNG(text);
    l.write((int)512,(int)512,text1,DeImage);



 /*   for(int i=0;i<text.length;i++){
        System.out.println(text[i]);
    }

     RC4New rc41 = new RC4New();
     rc4.KSA(key);
    int[] text1 = rc4.PRNG(text); 
     for(int i=0;i<text1.length;i++){
        System.out.println(text1[i]);
    }

   */ 
    System.out.println("length:"+pixels.length);
  //  l.write((int)512,(int)512,text);

    // TODO code application logic here
}
    } 

//加密

 package test;

 /**
     *
 * @author AMaR
 */
 public class RC4New {
 int state[] = new int[256];

 int j;
 /**
 *
 * @param key
 */
public void KSA(int[] key){ 

    int tmp;
    for (int i=0; i < 256; i++) {
        state[i] = i;
    }

    j=0;

    for (int i=0; i < 256; i++) {

       j = (j + state[i] + key[i % key.length]) % 256;
        tmp = state[i];
        state[i] = state[j];
        state[j] = tmp;
    }
}
    public int[] PRNG(int[] data){
    int tmp,k;
    int i=0;
    j=0;
    int[] cipherText = new int[data.length];
    for(int x=0;x<data.length;x++){
        i = (i + 1) % 256;
        j = (j + state[i]) % 256;
        tmp = state[i];
        state[i] = state[j];
        state[j] = tmp;
        k = state[(state[i] + state[j]) % 256];
        cipherText[x]=  (data[x] ^ k);
    }
    return cipherText;



    }


    }

//加载/写入图像

    package test;

    import java.awt.Dimension;
    import java.awt.image.BufferedImage;
    import java.awt.image.Raster;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import java.io.File;
    import java.awt.image.WritableRaster;
    /**
    *
    * @author AMaR
    */


    public class LoadImage {
      BufferedImage image;
      void load()throws Exception { 

   //  FIle newfile = new File("lena.png)

     image = ImageIO.read(getClass().getResourceAsStream("lena.png"));
    }
    public Dimension getImageSize() {
    return new Dimension(image.getWidth(), image.getHeight());
    }

   public int[] getImagePixels() {
    int [] dummy = null;
    int wid, hgt;

   // compute size of the array
    wid = image.getWidth();
    hgt = image.getHeight();

   // start getting the pixels
   Raster pixelData;
   pixelData = image.getData();
   return pixelData.getPixels(0, 0, wid, hgt, dummy);


   }
    @SuppressWarnings("empty-statement")
   public void write(int width ,int height, int[] pixels,File outputfile) {

       try {
   // retrieve image
    BufferedImage writeImage = new BufferedImage(512, 512, BufferedImage.TYPE_BYTE_GRAY);;
     // File outputfile = new File("encrypted.png");
    WritableRaster raster = (WritableRaster) writeImage.getData();
    raster.setPixels(0,0,width,height,pixels);
    writeImage.setData(raster);
    ImageIO.write(writeImage, "png", outputfile);

    } catch (IOException e) {
      }

     }
    }


推荐答案

不清楚下面哪一个触发您的错误。这个

It's not clear which of the below is triggering your error. This

 File EnImage = new File("encrypted.png");

将从当前目录中读取,这不一定与您的jar文件所在的目录相同。

will read from the current directory, which is not necessarily the same directory as that your jar file is in.

这个

image = ImageIO.read(getClass().getResourceAsStream("lena.png"));

将从您所在类的jar文件中的目录读取。请注意,您正在阅读从的jar文件,而不是目录。

will read from the directory in the jar file that your class is in. Note that you're reading from the jar file, not the directory.

鉴于上述代码,我会:


  1. 确定或明确指定 File()操作的工作目录。您的工作目录是您调用 java 的工作目录,并且可能会在IDE内部/ />>
  2. 将lena.png打包为您的.jar文件中的资源。

  1. determine or explicitly specify the working directory for the File() operations. Your working directory is the one you invoke java from, and this may differ within/without the IDE
  2. package the lena.png as a resource within your .jar file.

这篇关于无法在jar中读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆