You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

425 lines
17 KiB
Java

2 years ago
package com.docus.sw.souyin;
2 years ago
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.FileUtils;
2 years ago
import com.docus.sw.Config;
import com.docus.sw.fenpan.Document;
2 years ago
import com.docus.sw.fenpan.FileTypeEnum;
import com.docus.sw.fenpan.Pieces;
2 years ago
import com.docus.sw.fenpan.Roll;
import com.docus.sw.word.GetPicsDocx;
import com.docus.sw.word.PdfBoxUtils;
import com.docus.sw.word.ReadImgDoc;
import com.drew.tools.FileUtil;
2 years ago
import com.google.gson.Gson;
import org.apache.commons.imaging.ImageInfo;
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.Imaging;
2 years ago
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
2 years ago
import java.io.File;
2 years ago
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
2 years ago
import java.util.ArrayList;
2 years ago
import java.util.HashMap;
2 years ago
import java.util.List;
2 years ago
import java.util.Map;
2 years ago
public class SuoyinService {
//读取分盘后的结果。
//宗,盘,卷,件
//输出目录文件
//生成片头,片尾
//统计出需要使用的 胶片量
2 years ago
public static void main(String[] args) throws IOException {
Config.loadConfig();
String path = "C:\\Users\\zhanghai\\Desktop\\桌面\\test\\ppp\\ppp";
2 years ago
new SuoyinService().index(path);
2 years ago
}
public void index(String path) throws IOException {
2 years ago
Map<String,IndexPlate> map = new HashMap<>();
findAllDir(path, map);
2 years ago
//计算出宗
//计算出每个盘的目录。
2 years ago
for (IndexPlate indexPlate : map.values()) {
String absolutePath = indexPlate.getAbsolutePath();
File file = new File(absolutePath);
File[] files = file.listFiles();
List<Roll> rollList = new ArrayList<>();
//卷
for (File rollFile : files) {
if (rollFile.getName().equals("片头")) {
2 years ago
//填充卷
2 years ago
Roll roll = new Roll(rollFile.getName(), rollFile.getAbsolutePath());
rollList.add(roll);
} else if (rollFile.getName().equals("片尾")) {
2 years ago
//填充卷
2 years ago
Roll roll = new Roll(rollFile.getName(), rollFile.getAbsolutePath());
rollList.add(roll);
} else {
//
List<Pieces> pieceList = new ArrayList<>();
File[] piecesFile = rollFile.listFiles();
for(File piece:piecesFile){
//区分word和pdf
if(piece.getName().endsWith(".doc")){
Pieces pieces = new Pieces(FileTypeEnum.DOC, piece.getAbsolutePath(), piece.getName());
File temp = new File("temp");
if(!temp.exists()){
temp.mkdirs();
}
ReadImgDoc.readPicture(piece.getAbsolutePath(),temp.getAbsolutePath());
try {
PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath());
File[] listFiles = temp.listFiles();
2 years ago
genPieces(pieces, listFiles,FileTypeEnum.DOC);
pieceList.add(pieces);
} catch (IOException e) {
}
//删除对应的temp 文件
2 years ago
FileUtils.delete(temp);
}else if(piece.getName().endsWith(".docx")){
2 years ago
Pieces pieces = new Pieces(FileTypeEnum.DOCX, piece.getAbsolutePath(), piece.getName());
File temp = new File("temp");
if(!temp.exists()){
temp.mkdirs();
}
GetPicsDocx.getPics(piece.getAbsolutePath(),temp.getAbsolutePath());
try {
PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath());
File[] listFiles = temp.listFiles();
2 years ago
genPieces(pieces, listFiles,FileTypeEnum.DOCX);
pieceList.add(pieces);
} catch (IOException e) {
}
//删除对应的temp 文件
2 years ago
FileUtils.delete(temp);
}else if(piece.getName().endsWith(".pdf")){
Pieces pieces = new Pieces(FileTypeEnum.DOC, piece.getAbsolutePath(), piece.getName());
File temp = new File("temp");
if(!temp.exists()){
temp.mkdirs();
2 years ago
}
try {
PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath());
File[] listFiles = temp.listFiles();
2 years ago
genPieces(pieces, listFiles,FileTypeEnum.DOC);
pieceList.add(pieces);
} catch (IOException e) {
}
//删除对应的temp 文件
2 years ago
// temp.delete();
FileUtils.delete(temp);
}else{
Pieces pieces = new Pieces(FileTypeEnum.JPG, piece.getAbsolutePath(), piece.getName());
File[] listFiles = piece.listFiles();
2 years ago
genPieces(pieces, listFiles,FileTypeEnum.JPG);
pieceList.add(pieces);
2 years ago
}
}
//填充卷
Roll roll = new Roll(rollFile.getName(), rollFile.getAbsolutePath());
roll.putAll(pieceList);
rollList.add(roll);
2 years ago
2 years ago
}
2 years ago
2 years ago
}
indexPlate.putAll(rollList);
}
//生成索引目录
for(IndexPlate indexPlate : map.values()){
List<Roll> rollList = indexPlate.getRollList();
//计算出总size
Double totalPage = 0d;
for(Roll roll:rollList){
List<Pieces> piecesList = roll.getPiecesList();
totalPage+=piecesList.size();
}
Integer pianPageNum = 0;
Integer totalSize = 0;
for(Roll roll:rollList){
//跳过片头
if(roll.getName().equals("片头")){
continue;
}
//跳过片尾
if(roll.getName().equals("片尾")){
continue;
}
List<Pieces> piecesList = roll.getPiecesList();
for(Pieces pieces:piecesList){
Integer size = pieces.getDocumentList().size();
totalSize+=size;
}
}
List<IndexPageRow> indexPageRows= indexGen(indexPlate, rollList, pianPageNum, totalSize);
//修正数据
System.out.println(new Gson().toJson(indexPageRows));
//生成excel
List<MesEasyExcel> mesEasyExcels = new ArrayList<>();
for(IndexPageRow indexPageRowTemp:indexPageRows){
mesEasyExcels.add(MesEasyExcel.create(indexPageRowTemp));
}
2 years ago
EasyExcel.write(indexPlate.getAbsolutePath()+"/"+indexPlate.getName()+".xls", MesEasyExcel.class).sheet("test").doWrite(mesEasyExcels);
2 years ago
2 years ago
ExcelUtil.toPdf(indexPlate.getAbsolutePath()+"/"+indexPlate.getName()+".xls",indexPlate.getAbsolutePath()+"/"+indexPlate.getName()+".pdf");
2 years ago
2 years ago
Integer page = PdfUtil.getPage(indexPlate.getAbsolutePath() +"/"+ indexPlate.getName()+".pdf");
2 years ago
indexPageRows= indexGen(indexPlate, rollList, page, totalSize);
//修正数据
System.out.println(new Gson().toJson(indexPageRows));
//生成excel
mesEasyExcels = new ArrayList<>();
for(IndexPageRow indexPageRowTemp:indexPageRows){
mesEasyExcels.add(MesEasyExcel.create(indexPageRowTemp));
}
2 years ago
EasyExcel.write(indexPlate.getAbsolutePath()+"/"+indexPlate.getName()+".xls", MesEasyExcel.class).sheet("test").doWrite(mesEasyExcels);
2 years ago
2 years ago
ExcelUtil.toPdf(indexPlate.getAbsolutePath()+"/"+indexPlate.getName()+".xls",indexPlate.getAbsolutePath()+"/"+indexPlate.getName()+".pdf");
2 years ago
//写入文件夹。
PdfToPic.toPic(indexPlate.getAbsolutePath()+"/"+indexPlate.getName()+".pdf",indexPlate.getAbsolutePath()+"/片头","01","jpg");
2 years ago
PdfToPic.toPicDesc(indexPlate.getAbsolutePath()+"/"+indexPlate.getName()+".pdf",indexPlate.getAbsolutePath()+"/片尾","00","jpg");
2 years ago
File pdf = new File(indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".pdf");
2 years ago
//再重算一次索引,以及将文件进行替换。
File parentFile = pdf.getParentFile().getParentFile();
File file = new File(parentFile.getAbsolutePath() + "/" + indexPlate.getName() + ".pdf");
Files.move(pdf.toPath(),file.toPath(), StandardCopyOption.ATOMIC_MOVE);
File xls = new File(indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".xls");
//再重算一次索引,以及将文件进行替换。
File xlsParentFile = xls.getParentFile().getParentFile();
File file2 = new File(xlsParentFile.getAbsolutePath() + "/" + indexPlate.getName() + ".xls");
Files.move(xls.toPath(),file2.toPath());
2 years ago
}
2 years ago
//读取片头
//读取片尾
//生成excel
//生成图片
2 years ago
2 years ago
//
}
2 years ago
private static void genPieces(Pieces pieces, File[] listFiles,FileTypeEnum fileTypeEnum) {
List<Document> documentList = new ArrayList<>();
2 years ago
for (File docfile : listFiles) {
//非图片模式,跳过。
if(!(docfile.getName().endsWith(".jpg") || docfile.getName().endsWith(".png")
|| docfile.getName().endsWith(".jpeg") || docfile.getName().endsWith(".tif")
|| docfile.getName().endsWith(".tiff")|| docfile.getName().endsWith(".jp2") || docfile.getName().endsWith(".jpm"))|| docfile.getName().endsWith(".gif")){
continue;
}
2 years ago
if (docfile.getName().endsWith(".jp2") || docfile.getName().endsWith(".jpm")) {
// 读取 JPEG 2000 图像文件
try {
BufferedImage image = ImageIO.read(docfile);
int height = image.getHeight();
int width = image.getWidth();
Document document = new Document(width, height, 300);
documentList.add(document);
} catch (IOException e) {
throw new RuntimeException(e);
}
2 years ago
} else {
try {
ImageInfo imageInfo = Imaging.getImageInfo(docfile);
int height = imageInfo.getHeight();
int width = imageInfo.getWidth();
int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
Document document = new Document(width, height, physicalHeightDpi);
documentList.add(document);
} catch (IOException e) {
throw new RuntimeException("非图片格式", e);
} catch (ImageReadException e) {
throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
}
}
2 years ago
pieces.put(documentList);
}
2 years ago
}
2 years ago
private List<IndexPageRow> indexGen(IndexPlate indexPlate, List<Roll> rollList, Integer pianPageNum, Integer totalSize) {
List<IndexPageRow> indexPageRows = new ArrayList<>();
int start = 1;
//添加片头
for(Roll roll: rollList){
//跳过片头
if(roll.getName().equals("片头")){
File file = new File(roll.getAbsolutePath());
int fileasize = file.listFiles().length;
fileasize+=pianPageNum;
IndexPageRow indexPageRow = new IndexPageRow(indexPlate.getZongName(), indexPlate.getName(), roll.getName(), "", fileasize, start);
start+=fileasize;
indexPageRows.add(indexPageRow);
start+=1;
}
}
//处理偏中
for(Roll roll: rollList){
//跳过片头
if(roll.getName().equals("片头")){
continue;
}
//跳过片尾
if(roll.getName().equals("片尾")){
continue;
}
List<Pieces> piecesList = roll.getPiecesList();
for(Pieces pieces:piecesList){
IndexPageRow indexPageRow = new IndexPageRow(indexPlate.getZongName(), indexPlate.getName(), roll.getName(), pieces.getName(), pieces.getDocumentList().size(), start);
start+=pieces.getDocumentList().size();
indexPageRows.add(indexPageRow);
}
start+=1;
}
//添加片尾
for(Roll roll: rollList){
//跳过片头
if(roll.getName().equals("片尾")){
File file = new File(roll.getAbsolutePath());
int fileasize = file.listFiles().length;
fileasize+=pianPageNum;
IndexPageRow indexPageRow = new IndexPageRow(indexPlate.getZongName(), indexPlate.getName(), roll.getName(), "", fileasize, start);
start+=fileasize;
indexPageRows.add(indexPageRow);
start+=1;
}
}
//加上统计
IndexPageRow indexPageRow = new IndexPageRow("", "", "", "有效画幅数", totalSize, null);
indexPageRows.add(indexPageRow);
return indexPageRows;
}
private void findAllDir(String absolutePath, Map<String,IndexPlate> map) {
2 years ago
File sourceFile = new File(absolutePath);
File[] files = sourceFile.listFiles();
for (File o : files) {
if (o.isDirectory()) {
2 years ago
findAllDir(o.getAbsolutePath(), map);
2 years ago
}
if (o.isFile()) {
//判断是word,pdf,pic
if (o.getName().endsWith(".pdf")) {
2 years ago
File rofile = o.getParentFile().getParentFile();
2 years ago
// new Plate()
2 years ago
if(map.get(rofile.getAbsolutePath())==null){
IndexPlate indexPlate = new IndexPlate(rofile.getAbsolutePath(), rofile.getName(),rofile.getParentFile().getName());
map.put(rofile.getAbsolutePath(),indexPlate);
}
2 years ago
} else if (o.getName().endsWith(".docx") || o.getName().endsWith(".doc")) {
2 years ago
File rofile = o.getParentFile().getParentFile();
// new Plate()
if(map.get(rofile.getAbsolutePath())==null){
IndexPlate indexPlate = new IndexPlate(rofile.getAbsolutePath(), rofile.getName(),rofile.getParentFile().getName());
map.put(rofile.getAbsolutePath(),indexPlate);
}
2 years ago
} else if (o.getName().endsWith(".jpg") || o.getName().endsWith(".png")
|| o.getName().endsWith(".jpeg") || o.getName().endsWith(".tif")
|| o.getName().endsWith(".tiff")) {
2 years ago
if(o.getParentFile().getName().equals("片头")||o.getParentFile().getName().equals("片尾")){
continue;
}
//件
2 years ago
File parentFile = o.getParentFile()
2 years ago
//卷
.getParentFile()
//盘
.getParentFile();
if(map.get(parentFile.getAbsolutePath())==null){
IndexPlate indexPlate = new IndexPlate(parentFile.getAbsolutePath(), parentFile.getName(),parentFile.getParentFile().getName());
map.put(parentFile.getAbsolutePath(),indexPlate);
}
2 years ago
} else {
2 years ago
System.out.println("请删除无效的文件:" + o.getAbsolutePath());
2 years ago
}
break;
}
}
}
}