Java高清晰高品質 圖片壓縮
阿新 • • 發佈:2019-02-10
網上搜索了很多,壓縮出來的效果實在不能令人滿意,研究了一些程式碼,自己寫了一個,壓縮出來的效果很好。但是有一個缺點,可能也是java的缺點吧,呵呵。
在jdk1.6以下的版本環境下,壓縮部分圖片會很慢,經過我測試,如果圖片的DPI越高,速度越慢,一般WEB使用圖片DPI都是72,速度很快。大家可以試下。我測試了幾張DPI為300,印刷品質的圖片,大概要35秒左右,當然還和機器記憶體有關。
在jdk1.6環境下,速度能令人滿意,從之前的35秒減少到了1秒多一點點。提升了這麼多,jdk1.6改進實在是大。
經過我除錯發現,慢的部分主要是在g2.drawImage(image, 0, 0,imageWidth, imageHeight, Color.white,null);,這一句。
主要的壓縮程式碼是public static void ImageScale(String path,String fileName,String toFileName)
以下是我的程式碼,大家可以參考改進。
- package sundy.img;
- /**
- * ImageCompress 提供使用者將大圖片按比例壓縮為小圖片,支援JPG
- * Please refer to: <BR>
- * <P>
- * @author feng_sunddy <[email protected]>
- * @version 1.0
- * @see java.awt.image.BufferedImage
- **/
- import java.awt.BorderLayout;
-
import
- import java.awt.TextField;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.FileDialog;
- import java.awt.Font;
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Insets;
-
import java.awt.Label;
- import java.awt.MediaTracker;
- import java.awt.Panel;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.awt.image.BufferedImage;
- import java.awt.image.ConvolveOp;
- import java.awt.image.Kernel;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- publicclass ImageCompress extends Frame {
- privatestaticfinallong serialVersionUID = 48L;
- publicstaticvoid main(String[] args){
- String fileName = "F:/share/bigimages/b-4.jpg";
- String gui = "";
- if (args.length > 0) fileName = args[0];
- if (args.length > 1) gui = "gui";
- if (gui.equals("gui")){
- new ImageCompress(fileName);
- }else{
- long c = System.currentTimeMillis();
- ImageCompress.ImageScale(getFilePath(fileName), getFileFullName(fileName), getFileName(fileName) + "-s." + getFileExt(fileName).toLowerCase());
- System.out.println("elapse time:" + (System.currentTimeMillis() - c)/1000.0f + "s");
- }
- }
- privatestaticfinal String version = "ImageCompress v1.0";
- public ImageCompress(String fileName) {
- super(version);
- file = fileName;
- createUI();
- loadImage(fileName);
- setVisible(true);
- }
- /**
- * A Hashtable member variable holds the image processing
- * operations, keyed by their names.
- **/
- private Panel mControlPanel;
- private BufferedImage mBufferedImage;
- private Label labelWidth = new Label("width:");
- private TextField textWidth = new TextField(7);
- private Label labelHeight = new Label("height:");
- private TextField textHeight = new TextField(7);
- private String file;
- /**
- * createUI() creates the user controls and lays out the window.
- * It also creates the event handlers (as inner classes) for
- * the user controls.
- **/
- privatevoid createUI() {
- setFont(new Font("Serif", Font.PLAIN, 12));
- // Use a BorderLayout. The image will occupy most of the window,
- // with the user controls at the bottom.
- setLayout(new BorderLayout());
- // Use a Label to display status messages to the user.
- final Label statusLabel = new Label("Welcome to " + version + ".");
- textWidth.setText("160");
- textHeight.setText("160");
- // Create a Button for loading a new image.
- Button loadButton = new Button("Load...");
- // Add a listener for the button. It pops up a file dialog
- // and loads the selected image file.
- loadButton.addActionListener(new ActionListener() {
- publicvoid actionPerformed(ActionEvent ae) {
- FileDialog fd = new FileDialog(ImageCompress.this);
- fd.setVisible(true);
- if (fd.getFile() == null) return;
- String path = fd.getDirectory() + fd.getFile();
- file = path;
- loadImage(path);
- }
- });
- Button buttonResize = new Button("Resize");
- buttonResize.addActionListener(new ActionListener(){
- publicvoid actionPerformed(ActionEvent ae){
- resizeImage(file);
- }
- });
- // Add the user controls at the bottom of the window.
- mControlPanel = new Panel();
- mControlPanel.add(loadButton);
- mControlPanel.add(labelWi