java缩放文件夹中的图像
我正在尝试使用文件选择器调整图片大小。似乎所有的东西都是文件,但在添加到文件夹后我无法打开它
public void metodAddpath(String fullPath) {
try {
File sourceFile = new File(fullPath);
BufferedImage bufferedimage = ImageIO.read(sourceFile);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedimage, "jpg", os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
FileOutputStream fileOutputStream = new FileOutputStream(
sourceFile);
int bufferSize;
byte[] bufffer = new byte[512];
while ((bufferSize = is.read(bufffer)) > 0) {
fileOutputStream.write(bufffer, 0, bufferSize);
}
is.close();
fileOutputStream.close();
//scaleImage(bufferedimage, 220, 220);
} catch(Exception e) {
e.printStackTrace();
}
} 之后我按下按钮将图像保存到文件夹中
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Database base = new Database();
metodAddpath(jTextField1.getText());
base.addPictureResource(jTextField1.getText());
}
但当我试图将其添加到文件夹中时,出现了一个错误
# 1 楼答案
最后,我找到了在保存到文件夹之前缩放图像的方法。首先,我想为按钮添加一个侦听器,并使用文件选择器获取图像
在我为另一个按钮添加侦听器之后,该按钮负责将图片添加到文件夹中。这是我的代码:
最后,让我们添加两个函数:
别忘了重要的方法
谢谢大家的帮助。我想对Mad程序员说声谢谢。你真是个天才