Javafx 2 Java fx2我想在保存之前调整图像大小
我制作了一个javafx应用程序(JavaFX2,jdk 7u72),用户可以在其中导入(使用文件选择器)一个图像,并将其存储在一个外部文件夹中Javafx 2 Java fx2我想在保存之前调整图像大小,javafx-2,javax.imageio,filechooser,Javafx 2,Javax.imageio,Filechooser,我制作了一个javafx应用程序(JavaFX2,jdk 7u72),用户可以在其中导入(使用文件选择器)一个图像,并将其存储在一个外部文件夹中 import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Resourc
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.stage.FileChooser;
public class FXMLDocumentbilController implements Initializable {
@FXML
private void handleButtonAction(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialDirectory(
new File(System.getProperty("user.home")));
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("Icons JPG, PNG, GIF", "*.jpg", "*.gif", "*.png") );
File file = fileChooser.showOpenDialog(null);
Path sourceFile = Paths.get(file.toString());
Path destinationFile = Paths.get("c:\\librafolder\\fotomath", file.getName());
try {
Files.copy(sourceFile, destinationFile);
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentbilController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
我希望图像自动减少到25或30 kb或250X250,然后存储在文件夹中。我从这个网站找到了一个非常好的解决方案
这段代码是我的javafx应用程序的一部分。用户选择的每个图像文件(可能是2Mb或10MB)都会加载到应用程序中进行显示。这会消耗太多的ram内存。因此,在执行行代码:Files.copy(sourceFile,destinationFile)之前,必须将每个图像文件最小化为25或30 kb;我理解。但是到目前为止,您尝试过什么来实际减小图像/文件大小呢?另外,请记住stackoverflow不是代码工厂。:-)好的,我将尝试解决这个问题,但我没有JavaFX2的经验。然后我发布我找到的任何解决方案,供stackoverflow用户更正。
package bilpir;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.stage.FileChooser;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;
public class FXMLDocumentbilController implements Initializable {
@FXML
private Label label;
@FXML
private void handleButtonAction(ActionEvent event) {
try {
//Files.copy(sourceFile, destinationFile);
float quality = 0.2f;
Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter)iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialDirectory(
new File(System.getProperty("user.home")));
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("Icons JPG, PNG, GIF", "*.jpg", "*.gif", "*.png") );
File file = fileChooser.showOpenDialog(null);
Path sourceFile = Paths.get(file.toString());
Path destinationFile = Paths.get("c:\\bilpir", file.getName());
FileInputStream inputStream = new FileInputStream(sourceFile.toString());
BufferedImage originalImage = ImageIO.read(inputStream);
File file2 = new File(destinationFile.toString());
FileImageOutputStream output = new FileImageOutputStream(file2);
writer.setOutput(output);
IIOImage image = new IIOImage(originalImage, null, null);
writer.write(null, image, iwp);
writer.dispose();
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentbilController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}