Java 无法读取jar中的图像
我已经编写了一个程序来加密Netbeans中的图像。该程序在从netbeans运行时运行良好,但当我将其构建到.jar文件中时,它无法读取图像,即使我将图像文件与.jar文件放在同一文件夹中Java 无法读取jar中的图像,java,image-processing,netbeans,encryption,grayscale,Java,Image Processing,Netbeans,Encryption,Grayscale,我已经编写了一个程序来加密Netbeans中的图像。该程序在从netbeans运行时运行良好,但当我将其构建到.jar文件中时,它无法读取图像,即使我将图像文件与.jar文件放在同一文件夹中 package test; import java.io.IOException; import java.io.File; /** * * @author AMaR */ public class Test { /** * @param args the
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
}
}
不清楚以下哪项触发了您的错误。这个
File EnImage = new File("encrypted.png");
将从当前目录中读取,该目录不一定与jar文件所在的目录相同
这个
将从类所在的jar文件中的目录中读取。注意,您读取的是jar文件,而不是目录
鉴于上述代码,我将:
文件()
操作的工作目录。您的工作目录是您从中调用java
的目录,在IDE内部/外部这可能有所不同您得到的错误是什么?在哪一行?你得到了什么异常?有日志吗?据我所知,你没有从外部资源加载任何图像,你只是从Jar中加载它们
getClass()。getResourceAsStream(“lena.png”)
将从Jar中获取一个文件。要从文件夹中获取文件,应使用IO.no errors。从netbeans运行时,它工作正常。但是在将它构建到.jar中之后,它并没有像预期的那样工作。在更改为“image=ImageIO.read(newfile);”之后,它工作得很好
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) {
}
}
}
File EnImage = new File("encrypted.png");
image = ImageIO.read(getClass().getResourceAsStream("lena.png"));