因此,我正在处理的程序接收图像,压缩它并将其发送到JPEG。我的程序运行正常,我只是想让它看起来不错。我是通过Java Swing做的,我是一个完全新手。
这是我的主要内容:
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.JFrame;
import javax.swing.JFileChooser;
import com.sun.media.jai.widget.DisplayJAI;
public class JPEGCompression {
public static void main(String[] args) throws IOException {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("./tmp"));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".png")
|| f.isDirectory();
}
public String getDescription() {
return "PNG Images";
}
});
int r = chooser.showOpenDialog(new JFrame());
if (r == JFileChooser.APPROVE_OPTION) {
BufferedImage n = ImageIO.read(chooser.getSelectedFile());
showOriginalImage("Original Image", n);
compressAndShow(n, 0.7f);
}
}
public static void compressAndShow(BufferedImage image, float quality)
throws IOException {
Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("jpeg");
if (!writers.hasNext())
throw new IllegalStateException("No writers found");
ImageWriter writer = (ImageWriter) writers.next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(bos);
writer.setOutput(ios);
writer.write(null, new IIOImage(image, null, null), param);
ios.flush();
ByteArrayInputStream in = new ByteArrayInputStream(bos.toByteArray());
RenderedImage out = ImageIO.read(in);
int size = bos.toByteArray().length;
showCompressedImage("Compressed to " + quality + ": " + size + " bytes", out);
File file = new File("compressed. " + quality + ".jpeg");
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
writer.write(null, new IIOImage(image, null, null), param);
}
private static void showOriginalImage(String title, RenderedImage img) {
JFrame frame = new JFrame(title);
frame.getContentPane().add(new DisplayJAI(img));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static void showCompressedImage(String title, RenderedImage img) {
JFrame frame = new JFrame(title);
frame.getContentPane().add(new DisplayJAI(img));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
目前两种展示图像方法完全相同。我想要的是showOriginalImage()在框架的顶部有一个文本字段,用户可以在其中输入压缩质量(目前硬编码为0.7f),其值介于0-1之间。然后,一旦他们选择了质量,compressAndShow()就会运行。
我将非常感谢任何关于如何做到这一点的建议/帮助, 感谢。
答案 0 :(得分:1)
您应该创建一个JPanel并向其添加DisplayJAI和JTextField。然后将此面板添加到内容窗格。可能是默认面板布局(FlowLayout)不适合您的需要。在这种情况下,请尝试使用BorderLayout或BoxLayout。