我正在尝试使用文件选择器调整图片大小。似乎所有内容都是文件,但添加文件夹后无法打开它。
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());
}
但是当我尝试将其添加到文件夹中时,出现了一个错误。
答案 0 :(得分:1)
我要出来说,这都不是...
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();
}
很有道理。
您正在读取图像,将其写入ByteArrayOutputStream
,通过InputStream
用管道传输,然后将其用于通过FileOutputStream
将内容写入另一个文件。 ..为什么?!
类似...
File sourceFile = new File(fullPath);
try {
BufferedImage bufferedimage = ImageIO.read(sourceFile);
//scaleImage(bufferedimage, 220, 220);
// Beware, this is overwriting the existing file
try (FileOutputStream fileOutputStream = new FileOutputStream(sourceFile)) {
ImageIO.write(bufferedimage, "jpg", fileOutputStream);
}
} catch(Exception e) {
e.printStackTrace();
}
会做同样的事情,更容易阅读,而且可能更有效...
我怀疑这是否会回答您的问题,但这可能会减少一些困惑
答案 1 :(得分:0)
最后,我找到了一种在保存到文件夹之前缩放图像的方法。首先,我想为按钮添加一个侦听器,并使用文件选择器获取图像。
<div class="container">
<div class="content">
blah<br/>blah<br/>blah<br/>blah<br/>blah<br/>
blah<br/>blah<br/>blah<br/>blah<br/>blah<br/>
blah<br/>blah<br/>blah<br/>blah<br/>blah<br/>
blah<br/>blah<br/>blah<br/>blah<br/>blah<br/>
blah<br/>blah<br/>blah<br/>blah<br/>blah<br/>
blah<br/>blah<br/>blah<br/>blah<br/>blah<br/>
</div>
<canvas class="overlay">
</canvas>
</div>
.container {
background: #CCCCCC;
width: 200px;
height: 200px;
position: relative;
border: 4px black solid;
}
.content {
width: 100%;
height: 100%;
overflow: scroll;
}
.overlay {
border:1px solid black;
background: rgba(255,0,0,0.5);
position: absolute;
top: 0;
right: 0;
bottom: 0;
left:0;
height: 150px;
width: 150px;
}
在为另一个按钮添加侦听器之后,该按钮负责将图片添加到文件夹。这是我的代码:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser file = new JFileChooser();
file.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpeg", "jpg", "png");
file.addChoosableFileFilter(filter);
int result = file.showSaveDialog(null);
if(result ==JFileChooser.APPROVE_OPTION) {
File selectedFile = file.getSelectedFile();
//GET ABSOLUTE PATH OF PICTURES
jTextField1.setText(selectedFile.getAbsolutePath());
//addPicture.setText(selectedFile.getName());
//GET NAME OF PICTURES
//getPicName = selectedFile.getName();
} else if(result == JFileChooser.CANCEL_OPTION) {
System.out.println("File not found!");
}
}
最后,让我们添加两个功能:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
addPicture(jTextField1.getText());
}catch(Exception e) {
e.printStackTrace();
}
}
添加不要忘记重要的方法
public void addPicture(String fullPath) throws IOException {
File sourceFile = new File(fullPath);
try {
BufferedImage bufferedimage = ImageIO.read(sourceFile);
// add method scaleImage(bufferedimage, 220, 220) in ImageIO.write(scaleImage(bufferedimage, 220, 220), "jpg", fileOutputStream)
try (FileOutputStream fileOutputStream = new FileOutputStream("/my files/NetBeans IDE 8.2/NewDataBase/src/newdatabase/images/" + sourceFile.getName())) {
ImageIO.write(scaleImage(bufferedimage, 220, 220), "jpg", fileOutputStream);
}
} catch(Exception e) {
e.printStackTrace();
}
感谢所有人的帮助。我要感谢MadProgrammer。你真是个天才,老兄。