public static void main(String[] args) {
//创建该对象则调用构造方法,对象实现ActionListener则自动调用actionPerformed()方法
new PicdealMain();
}
重点需要注意的如下:
vm处可以自己设置大小
修改jvm参数可以写这个:
-Xmn128m -Xms1512m -Xmx1512m
下方路径就是jre的路径,如果和应用同级可以直接写 .\bin (我是将jre文件夹名字改为了bin的看起好点)
package org.example;
import com.alibaba.excel.EasyExcel;
import lombok.SneakyThrows;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.example.entity.HeadInfo;
import org.example.entity.PicInfo;
import org.example.FileUtil.ReadFile;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @ClassName: Picdeal
* @Package: org.example
* @Author: TangMei
* @CreateTime: 17:10 2023/3/3
* @Version 1.0
* @Description: 这是一个相当有用的类
**/
public class PicdealMain extends JFrame implements ActionListener {
private String inpath;
private String outpath;
//窗口
JFrame jf = new JFrame("计算图片大小(下方路径不可手动输入)");
//画板
JPanel jp = new JPanel();
//布局卡选项
JTabbedPane tabPane = new JTabbedPane();
//布局1
Container con = new Container();
//标签
JLabel jl1 = new JLabel("选择数据源");
JLabel jl3 = new JLabel("选择输出目录");
//按钮
JButton jb1 = new JButton("···");
JButton jb3 = new JButton("···");
JButton jb4 = new JButton("启动程序");
//文本
JTextArea jt1 = new JTextArea();
JTextArea jt3 = new JTextArea();
//文件选择器
JFileChooser chooser = new JFileChooser();
public PicdealMain() {
//将画板加到窗口上
//选择当前程序根目录
chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
jf.add(jp);
//布局,将布局卡加到面板上
jf.setContentPane(tabPane);
//设置标签,按钮,文本框的位置及大小,x,y,width,height
jl1.setBounds(20, 20, 100, 20);
jb1.setBounds(640, 20, 50, 20);
jt1.setBounds(120, 20, 500, 20);
jl3.setBounds(20, 80, 100, 20);
jb3.setBounds(640, 80, 50, 20);
jt3.setBounds(120, 80, 500, 20);
jb4.setBounds(300, 140, 100, 30);
//设置按钮事件处理 this代表本身这个对象,意思是监听这个对象,所里该类必须实现ActionListener
jb1.addActionListener(this);
// jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
//将按钮加到布局1上
con.add(jl1);
con.add(jt1);
con.add(jb1);
con.add(jl3);
con.add(jt3);
con.add(jb3);
con.add(jb4);
//将布局加到布局卡内,并设置该布局的名称
tabPane.add("计算图片大小", con);
//设置窗口属性---------------------
//窗口可见
jf.setVisible(true);
//窗口大小
jf.setSize(800, 400);
//关闭窗口时关闭程序
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
//设置窗口位置
jf.setLocation(400, 150);
}
public static void main(String[] args) {
//创建该对象则调用构造方法,对象实现ActionListener则自动调用actionPerformed()方法
new PicdealMain();
}
//事件处理,所有事件处理必须写在该方法,该方法重写自ActionListener
@SneakyThrows
@Override
public void actionPerformed(ActionEvent e) {
//点击按钮后,判断是哪一个按钮
if (e.getSource() == jb1) {
//设置文件选择器只能选择0(文件),1(文件夹)
chooser.setFileSelectionMode(1);
//打开文件浏览器,点击取消则返回1
int status = chooser.showOpenDialog(null);
if (status == 1) {
return;
} else {
//读取选择器选择到的文件
File file = chooser.getSelectedFile();
//获取文件绝对路径并写入到文本框内
jt1.setText(file.getAbsolutePath());
inpath = file.getAbsolutePath();
}
}
if (e.getSource() == jb3) {
chooser.setFileSelectionMode(1);
int status = chooser.showOpenDialog(null);
if (status == 1) {
return;
} else {
File file = chooser.getSelectedFile();
if (file == null) {
file = new File(System.getProperty("user.dir"));
}
jt3.setText(file.getAbsolutePath());
outpath = file.getAbsolutePath();
}
}
if (e.getSource() == jb4) {
new Thread() {
@Override
public void run() {
JOptionPane.showMessageDialog(null, "...", "提示-正在计算运行", JOptionPane.INFORMATION_MESSAGE);
}
}.start();
if (outpath == null) {
outpath = System.getProperty("user.dir");
}
System.err.println("inpath:" + inpath);
System.err.println("outpath:" + outpath);
try {
//读取本地文件所有图片
File file = new File(inpath);
//数据持久化到D盘
writeAllFile(file);
JOptionPane.showMessageDialog(null, "计算完毕,已将结果输出到应用根目录或指定文件夹。。。", "提示", JOptionPane.PLAIN_MESSAGE);
} catch (Exception ee) {
JOptionPane.showMessageDialog(null, ee.getClass().getName() + ",请确定输入输出路径无异常", "提示", JOptionPane.YES_NO_OPTION);
ee.printStackTrace();
}
}
}
public static void writeToXlsx2(List<PicInfo> picInfoList, String outPath) throws IOException, InvalidFormatException {
boolean isExits = true;
String filename = outPath + "\\result.xlsx";
File file = new File(filename);
if (!file.exists()) {
isExits = false;
file.createNewFile();
}
if (!isExits) {
List<HeadInfo> headInfos = new ArrayList<>();
FileOutputStream out = new FileOutputStream(filename);
EasyExcel.write(out, HeadInfo.class)
.autoCloseStream(Boolean.TRUE)
.sheet("Sheet1")
.doWrite(headInfos);
}
FileInputStream fileInputStream = new FileInputStream(filename);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream);
XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
FileOutputStream fileOutputStream = new FileOutputStream(filename);
for (PicInfo picInfo : picInfoList) {
XSSFRow row = sheet.createRow(sheet.getLastRowNum() + 1);//新增一行
XSSFCell cell1 = row.createCell(0);//行中的第“0+1”列
cell1.setCellValue(picInfo.getName());//列中放的值
XSSFCell cell2 = row.createCell(1);
cell2.setCellValue(picInfo.getWidthCm());
XSSFCell cell3 = row.createCell(2);
cell3.setCellValue(picInfo.getHeightCm());
XSSFCell cell4 = row.createCell(3);
cell4.setCellValue(picInfo.getA401());
XSSFCell cell5 = row.createCell(4);
cell5.setCellValue(picInfo.getA402());
XSSFCell cell6 = row.createCell(5);
cell6.setCellValue(System.currentTimeMillis());
}
fileOutputStream.flush();
xssfWorkbook.write(fileOutputStream);
fileOutputStream.close();
}
private void writeAllFile(File file) throws IOException, InvalidFormatException {
File[] files = file.listFiles();
for (File f : files) {
if (f.isDirectory()) {
writeAllFile(f);
} else {
countSize(f.getAbsolutePath());
}
}
}
private void countSize(String fileNames) throws IOException, InvalidFormatException {
List<PicInfo> PicList = new ArrayList<>();
BufferedImage sourceImg;
double width = 0.0;
double height = 0.0;
String suffix = ".JPG.JPEG.PNG.BMP";
String s = fileNames;
if (s.indexOf(".") == -1) {
return;
}
String pos = s.substring(s.lastIndexOf(".")).toUpperCase();
if (suffix.indexOf(pos) == -1) {
return;
}
File picture = new File(s);
PicInfo picInfo = new PicInfo();
picInfo.setName(s);
// 源图大小
sourceImg = ImageIO.read(new FileInputStream(picture));
//获取图片像素
try {
width = sourceImg.getWidth();
height = sourceImg.getHeight();
} catch (Exception e) {
System.err.println("此扫描文件有异常已跳过:"+s);
return;
}
double area = 21 * 29.7;
//获取图片的长宽
picInfo.setWidthInch(width / 300);
picInfo.setHeightInch(height / 300);
picInfo.setWidthCm(picInfo.getWidthInch() * 2.54);
picInfo.setHeightCm(picInfo.getHeightInch() * 2.54);
PicList.add(picInfo);
//等比计算纸张大小(A4) --按长宽
if (width < height) {
int a41 = (int) (Math.ceil(picInfo.getWidthCm() / 21) * Math.ceil(picInfo.getHeightCm() / 29.7));
int a42 = (int) Math.ceil(picInfo.getWidthCm() * picInfo.getHeightCm() / area);
picInfo.setA401(a41);
picInfo.setA402(a42);
} else {
int a41 = (int) (Math.ceil(picInfo.getWidthCm() / 29.7) * Math.ceil(picInfo.getHeightCm() / 21));
int a42 = (int) Math.ceil(picInfo.getWidthCm() * picInfo.getHeightCm() / area);
picInfo.setA401(a41);
picInfo.setA402(a42);
}
PicdealMain.writeToXlsx2(PicList, outpath);
}
}
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
资源下载地址
https://download.csdn.net/download/weixin_44873668/87855674
点击下载-》exe4j