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();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
图片像素调整jar包,可实现自定义像素,自定义转换类型
共16个文件
class:8个
java:3个
classpath:1个
需积分: 12 0 下载量 185 浏览量
2022-03-29
21:49:47
上传
评论
收藏 37KB ZIP 举报
温馨提示
图片像素调整jar包,可实现自定义像素,自定义转换类型
资源详情
资源评论
资源推荐
收起资源包目录
图片调整相关jar.zip (16个子文件)
MyImageScaler.jar 18KB
这个还可以打开程序
感兴趣玩玩
打开jar程序.bat 27B
Project
bin
biz
MyImageScaler$1.class 1001B
MyImageScaler.class 5KB
StartListener.class 1KB
TransThread.class 3KB
MyImageScaler$2.class 1001B
StopListener.class 837B
util
FileUtil.class 2KB
TransImage.class 3KB
.settings
org.eclipse.jdt.core.prefs 598B
src
biz
MyImageScaler.java 8KB
util
TransImage.java 2KB
FileUtil.java 1KB
.project 390B
.classpath 301B
共 16 条
- 1
问题一箩筐
- 粉丝: 96
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0