UI中的错误,无法在两个文本字段中传递数据,或者当单击Button以压缩图像时用户选择的路径。
以下是代码:
// 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选择图像,另一个按钮用于选择必须保存图像的目标文件夹。
还有第三个按钮,用于压缩图像文件。
发布于 2014-04-04 17:53:42
您的代码在部分
// this is the button for choosing an image file 和在部分
// this is the button for choosing the destination folder for compressed image to be saved 完全相同,jButton6应该更改,jtextfield1应该根据UI进行更改。
我想它能解决这个问题。
祝好运
https://stackoverflow.com/questions/22868978
复制相似问题