【问题标题】:Java fx2 I want to resizing image before save thisJava fx2 我想在保存之前调整图像大小
【发布时间】:2015-03-26 20:54:43
【问题描述】:

我制作了一个 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,然后存储在文件夹中。

【问题讨论】:

  • 这段代码是我的 javafx 应用程序的一部分。用户选择的每个图像文件(可能是 2Mb 或 10Mb)都加载到应用程序上进行显示。那会花费太多的ram内存。为此,必须在执行以下代码之前将每个图像文件最小化为 25 或 30 kb:Files.copy(sourceFile, destinationFile);
  • 我明白了。但是到目前为止,您尝试过什么来实际减小图像/文件的大小?另外,请记住,stackoverflow 不是代码工厂。 :-)
  • 好的,我会尝试解决这个问题,但我没有使用 javafx 2 的经验。然后我发布我找到的任何解决方案,以供 stackoverflow 用户更正。

标签: javafx-2 javax.imageio filechooser


【解决方案1】:

我从这个网站找到了一个很好的解决方案 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
    }    

}

【讨论】:

    猜你喜欢
    • 2012-12-14
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 2013-07-26
    • 2013-05-09
    • 2011-07-31
    相关资源
    最近更新 更多