我创建了一个javafx应用程序(javafx 2,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.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,然后存储在文件夹中。
答案 0 :(得分:0)
我从这个网站找到了一个非常好的解决方案 http://blog.codejava.net/nam/reduce-image-quality-without-resizing/
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
}
}