首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图像压缩应用

图像压缩应用
EN

Stack Overflow用户
提问于 2014-04-04 17:12:25
回答 1查看 361关注 0票数 0

UI中的错误,无法在两个文本字段中传递数据,或者当单击Button以压缩图像时用户选择的路径。

以下是代码:

代码语言:javascript
复制
    // this is the button when clicked should compress the image 
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {              
        File imageFile = new File("myimage.jpg");
        File compressedImageFile = new File("myimage_compressed.jpg");

         try
         {
            InputStream is = new FileInputStream(imageFile);
            OutputStream os = new FileOutputStream(compressedImageFile);
            float quality = 0.5f;

            // create a BufferedImage as the result of decoding the supplied InputStream
            BufferedImage image = ImageIO.read(is);

            // get all image writers for JPG format
            Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");

            if (!writers.hasNext())
                throw new IllegalStateException("No writers found");
            ImageWriter writer = (ImageWriter) writers.next();
            ImageOutputStream ios = ImageIO.createImageOutputStream(os);
            writer.setOutput(ios);

            ImageWriteParam param = writer.getDefaultWriteParam();

            // compress to a given quality
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            param.setCompressionQuality(quality);

            // appends a complete image stream containing a single image and
            //associated stream and image metadata and thumbnails to the output
            writer.write(null, new IIOImage(image, null, null), param);

            // close all streams
            is.close();
            os.close();
            ios.close();
            writer.dispose();
     }
     catch(IOException e)
     {
         System.out.println(e);
     }

} 


// this is the  button for choosing an image file 
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {   
    JFileChooser chooser=new JFileChooser();
    FileNameExtensionFilter imgfilter=new FileNameExtensionFilter("Image files", "jpg");

    chooser.setFileFilter(imgfilter);
    chooser.showOpenDialog(null);

    File f=chooser.getSelectedFile();
    String filename=f.getAbsolutePath();

    jTextField1.setText(filename); 
}



// this is the button  for choosing the destination folder for compressed  image to be saved                                      

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JFileChooser chooser=new JFileChooser();
    FileNameExtensionFilter imgfilter=new FileNameExtensionFilter("Image files", "jpg");
    chooser.setFileFilter(imgfilter);
    chooser.showOpenDialog(null);
    File f=chooser.getSelectedFile();
    String filename=f.getAbsolutePath();
    jTextField1.setText(filename);
}   

在这里,我有两个文本字段和两个按钮,一个按钮用于从m/c选择图像,另一个按钮用于选择必须保存图像的目标文件夹。

还有第三个按钮,用于压缩图像文件。

EN

回答 1

Stack Overflow用户

发布于 2014-04-04 17:53:42

您的代码在部分

代码语言:javascript
复制
// this is the  button for choosing an image file 

和在部分

代码语言:javascript
复制
// this is the button  for choosing the destination folder for compressed  image to be saved                                    

完全相同,jButton6应该更改,jtextfield1应该根据UI进行更改。

我想它能解决这个问题。

祝好运

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22868978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档