package biz;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import util.FileUtil;
import util.TransImage;
public class MyImageScaler {
static TransThread transThread;
static JTextField txtSrc;
static JTextField txtDes;
static JTextField txtWidth;
static JTextField txtHeight;
static JComboBox<String> jcb;
static JButton start;
static JButton stop;
static JProgressBar jpb;
static JLabel txtStatus;
public static void main(String[] args) {
final JFrame mainFrm = new JFrame("我的图片缩放器");
mainFrm.setLayout(new BorderLayout(5, 5));
initFrame(mainFrm, 570, 155);
Font msyh = new Font("微软雅黑", Font.BOLD, 15);
// 设置窗体关闭事件
mainFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(2, 0, 2, 0);
gbc.gridwidth = 1;
gbc.gridheight = 1;
JPanel jpPath = new JPanel(gbl);
JLabel dirPath = new JLabel("图片文件夹路径:");
dirPath.setFont(msyh);
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(dirPath, gbc);
jpPath.add(dirPath);
txtSrc = new JTextField(33);
gbc.gridx = 1;
gbc.gridy = 0;
gbl.setConstraints(txtSrc, gbc);
jpPath.add(txtSrc);
JButton btnSrc = new JButton("浏览");
btnSrc.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
String path = getFolderPath(mainFrm);
if (path != null) {
txtSrc.setText(path);
}
}
}
});
gbc.gridx = 2;
gbc.gridy = 0;
gbl.setConstraints(btnSrc, gbc);
jpPath.add(btnSrc);
JLabel desPath = new JLabel("输出文件夹路径:");
desPath.setFont(msyh);
gbc.gridx = 0;
gbc.gridy = 1;
gbl.setConstraints(desPath, gbc);
jpPath.add(desPath);
txtDes = new JTextField(33);
gbc.gridx = 1;
gbc.gridy = 1;
gbl.setConstraints(txtDes, gbc);
jpPath.add(txtDes);
JButton btnDes = new JButton("浏览");
btnDes.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
String path = getFolderPath(mainFrm);
if (path != null) {
txtDes.setText(path);
}
}
}
});
gbc.gridx = 2;
gbc.gridy = 1;
gbl.setConstraints(btnDes, gbc);
jpPath.add(btnDes);
JPanel jpBtn = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 2));
JLabel lblWidth = new JLabel("缩放宽度:");
lblWidth.setFont(msyh);
jpBtn.add(lblWidth);
txtWidth = new JTextField(5);
jpBtn.add(txtWidth);
JLabel lblHeight = new JLabel("缩放高度:");
lblHeight.setFont(msyh);
jpBtn.add(lblHeight);
txtHeight = new JTextField(5);
jpBtn.add(txtHeight);
final JLabel lblFileType = new JLabel("文件类型:");
lblFileType.setFont(msyh);
jpBtn.add(lblFileType);
jcb = new JComboBox<String>(new String[] { "jpg", "png" });
jpBtn.add(jcb);
start = new JButton("转换");
start.addMouseListener(StartListener.getInstance());
jpBtn.add(start);
stop = new JButton("停止");
stop.setEnabled(false);
jpBtn.add(stop);
JPanel jpStatus = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 2));
jpb = new JProgressBar(0, 100);
jpb.setVisible(false);
jpStatus.add(jpb);
txtStatus = new JLabel();
txtStatus.setFont(new Font("微软雅黑", Font.BOLD, 12));
jpStatus.add(txtStatus);
mainFrm.add(jpPath, BorderLayout.NORTH);
mainFrm.add(jpBtn, BorderLayout.CENTER);
mainFrm.add(jpStatus, BorderLayout.SOUTH);
mainFrm.setVisible(true); // setVisible 设置窗体的可见性。
}
// 获取屏幕的分辨率 设置窗体在屏幕的居中位置。
public static void initFrame(JFrame frame, int width, int height) {
Toolkit toolkit = Toolkit.getDefaultToolkit(); // 获取一个与系统相关工具类对象
// 获取屏幕的分辨率
Dimension d = toolkit.getScreenSize();
int x = (int) d.getWidth();
int y = (int) d.getHeight();
frame.setBounds((x - width) / 2, (y - height) / 2, width, height);
}
public static String getFolderPath(Component parent) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setDialogTitle("选择路径");
fc.showDialog(parent, "确定");
File sf = fc.getSelectedFile();
if (sf != null) {
return sf.getPath();
} else {
return null;
}
}
}
class StartListener extends MouseAdapter {
private static final StartListener me = new StartListener();
public static StartListener getInstance() {
return me;
}
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
MyImageScaler.start.removeMouseListener(me);
MyImageScaler.start.setEnabled(false);
MyImageScaler.transThread = new TransThread();
MyImageScaler.transThread.start();
}
}
}
class StopListener extends MouseAdapter {
private static final StopListener me = new StopListener();
public static StopListener getInstance() {
return me;
}
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
MyImageScaler.transThread.stopFlag=true;
}
}
}
class TransThread extends Thread {
public boolean stopFlag=false;
public void run() {
try {
MyImageScaler.stop.setEnabled(true);
MyImageScaler.stop.addMouseListener(StopListener.getInstance());
MyImageScaler.start.setEnabled(false);
MyImageScaler.start.removeMouseListener(StartListener.getInstance());
List<String> fileList = new ArrayList<String>();
FileUtil.getAllSubFiles(MyImageScaler.txtSrc.getText(), MyImageScaler.jcb.getSelectedItem().toString(), fileList);
int width = Integer.parseInt(MyImageScaler.txtWidth.getText());
int height = Integer.parseInt(MyImageScaler.txtHeight.getText());
String outputPath = MyImageScaler.txtDes.getText();
MyImageScaler.jpb.setMaximum(fileList.size() - 1);
MyImageScaler.jpb.setVisible(true);
MyImageScaler.txtStatus.setVisible(true);
for (int i = 0; i < fileList.size(); i++) {
if (stopFlag) {
break;
}
String filePath=fileList.get(i);
File file = new File(filePath);
MyImageScaler.txtStatus.setText("正在处理"+file.getName()+"……"+(i+1)+"/"+fileList.size());
MyImageScaler.jpb.setValue(i);
BufferedImage image = ImageIO.read(new File(filePath));
if (image.getWidth() > image.getHeight()) {
TransImage.resizeImage(filePath, outputPath, width, 0);
} else {
TransImage.resizeImage(filePath, outputPath, 0, height);
}
}
MyImageScaler.jpb.setVisible(false);
MyImageScaler.txtStatus.setText("处理完成!");
MyImageScaler.stop.setEnabled(false);
MyImageScaler.stop.removeMouseListener(StopListener.getInstance());
MyImageScaler.start.setEnabled(true);
MyImageScaler.start.addMouseListener(StartListener.getInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
评论0