【发布时间】:2012-04-06 09:44:25
【问题描述】:
我是新来的,对 java 有点陌生。 我遇到了一个问题。 我有一个非常简单的程序,它尝试创建 png 并将它们保存在用户选择的文件夹中。 byteimage 是一个私有 byte[]:
byteimage = bcd.createPNG(300, 140, ColorSpace.TYPE_RGB, Color.BLACK, Color.BLACK);
setPath() 在浏览按钮的动作监听器中被调用
private void setPath() {
JFileChooser pathchooser = new JFileChooser();
pathchooser.setMultiSelectionEnabled(false);
pathchooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
pathchooser.setApproveButtonMnemonic(KeyEvent.VK_ENTER);
pathchooser.showDialog(this, "OK");
File f = pathchooser.getSelectedFile();
if (f != null) {
filepath = f.getAbsolutePath();
pathfield.setText(filepath);
}
}
字节转png方法如下:
public void byteToPNG(String filename) {
try {
InputStream in = new ByteArrayInputStream(byteimage);
BufferedImage bufferedimg = ImageIO.read(in);
ImageIO.write(bufferedimg, "png", new File(filename));
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
这个方法是这样调用的:
byteToPNG(pathfield.getText() + System.getProperty("file.separator") + textfield.getText() + ".png");
textfield.getText() 设置 png 的实际名称。 在构造函数内部,设置了默认文件路径:
filepath = System.getProperty("user.dir");
pathfield.setText(filepath);
代码在 Eclipse 中运行良好,并在所需位置生成 png 图像。 不幸的是,在导出为 jar 之后,它会启动,但是当按下生成 png 的按钮时,什么也没有发生。我在想 InputStream 或 BufferedImage 有问题,但我有点不解。
【问题讨论】:
标签: java eclipse jar inputstream