parent
f2cfb4cf4a
commit
33277bc457
|
@ -1,16 +1,17 @@
|
||||||
package cn.keking.utils;
|
package cn.keking.utils;
|
||||||
|
|
||||||
|
|
||||||
import com.lowagie.text.Document;
|
import cn.keking.config.ConfigConstants;
|
||||||
import com.lowagie.text.Image;
|
|
||||||
import com.lowagie.text.Rectangle;
|
|
||||||
import com.lowagie.text.pdf.PdfWriter;
|
|
||||||
import com.lowagie.text.pdf.RandomAccessFileOrArray;
|
|
||||||
import com.lowagie.text.pdf.codec.TiffImage;
|
|
||||||
import com.sun.media.jai.codec.*;
|
import com.sun.media.jai.codec.*;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import com.itextpdf.text.Document;
|
||||||
|
import com.itextpdf.text.Image;
|
||||||
|
import com.itextpdf.text.io.FileChannelRandomAccessSource;
|
||||||
|
import com.itextpdf.text.pdf.PdfWriter;
|
||||||
|
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
|
||||||
|
import com.itextpdf.text.pdf.codec.TiffImage;
|
||||||
import javax.media.jai.JAI;
|
import javax.media.jai.JAI;
|
||||||
import javax.media.jai.RenderedOp;
|
import javax.media.jai.RenderedOp;
|
||||||
import java.awt.image.RenderedImage;
|
import java.awt.image.RenderedImage;
|
||||||
|
@ -18,13 +19,14 @@ import java.awt.image.renderable.ParameterBlock;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.RandomAccessFile;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ConvertPicUtil {
|
public class ConvertPicUtil {
|
||||||
|
|
||||||
private final static Logger logger = LoggerFactory.getLogger(ConvertPicUtil.class);
|
private final static Logger logger = LoggerFactory.getLogger(ConvertPicUtil.class);
|
||||||
|
private final static String fileDir = ConfigConstants.getFileDir();
|
||||||
/**
|
/**
|
||||||
* Tif 转 JPG。
|
* Tif 转 JPG。
|
||||||
*
|
*
|
||||||
|
@ -42,25 +44,21 @@ public class ConvertPicUtil {
|
||||||
logger.info("找不到文件【" + strInputFile + "】");
|
logger.info("找不到文件【" + strInputFile + "】");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
strInputFile = strInputFile.replaceAll("\\\\", "/");
|
strInputFile = strInputFile.replaceAll("\\\\", "/");
|
||||||
strOutputFile = strOutputFile.replaceAll("\\\\", "/");
|
strOutputFile = strOutputFile.replaceAll("\\\\", "/");
|
||||||
|
|
||||||
FileSeekableStream fileSeekStream = null;
|
FileSeekableStream fileSeekStream = null;
|
||||||
try {
|
try {
|
||||||
JPEGEncodeParam jpegEncodeParam = new JPEGEncodeParam();
|
JPEGEncodeParam jpegEncodeParam = new JPEGEncodeParam();
|
||||||
|
|
||||||
TIFFEncodeParam tiffEncodeParam = new TIFFEncodeParam();
|
TIFFEncodeParam tiffEncodeParam = new TIFFEncodeParam();
|
||||||
tiffEncodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
|
tiffEncodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
|
||||||
tiffEncodeParam.setLittleEndian(false);
|
tiffEncodeParam.setLittleEndian(false);
|
||||||
|
String jpgname = strInputFile.replace(fileDir.replace("\\","/"), "");
|
||||||
String strFilePrefix = strInputFile.substring(strInputFile.lastIndexOf("/") + 1, strInputFile.lastIndexOf("."));
|
int index = jpgname.lastIndexOf(".");
|
||||||
|
String strFilePrefix = jpgname.substring(0, index);
|
||||||
fileSeekStream = new FileSeekableStream(strInputFile);
|
fileSeekStream = new FileSeekableStream(strInputFile);
|
||||||
ImageDecoder imageDecoder = ImageCodec.createImageDecoder("TIFF", fileSeekStream, null);
|
ImageDecoder imageDecoder = ImageCodec.createImageDecoder("TIFF", fileSeekStream, null);
|
||||||
int intTifCount = imageDecoder.getNumPages();
|
int intTifCount = imageDecoder.getNumPages();
|
||||||
logger.info("该tif文件共有【" + intTifCount + "】页");
|
logger.info("该tif文件共有【" + intTifCount + "】页");
|
||||||
|
|
||||||
String strJpgPath;
|
String strJpgPath;
|
||||||
String strJpgUrl;
|
String strJpgUrl;
|
||||||
if (intTifCount == 1) {
|
if (intTifCount == 1) {
|
||||||
|
@ -87,9 +85,7 @@ public class ConvertPicUtil {
|
||||||
strJpg = strJpgPath + "/" + i + ".jpg";
|
strJpg = strJpgPath + "/" + i + ".jpg";
|
||||||
strJpgUrl = strFilePrefix + "/" + i + ".jpg";
|
strJpgUrl = strFilePrefix + "/" + i + ".jpg";
|
||||||
}
|
}
|
||||||
|
|
||||||
File fileJpg = new File(strJpg);
|
File fileJpg = new File(strJpg);
|
||||||
|
|
||||||
// 如果文件不存在,则生成
|
// 如果文件不存在,则生成
|
||||||
if (!fileJpg.exists()) {
|
if (!fileJpg.exists()) {
|
||||||
RenderedImage renderedImage = imageDecoder.decodeAsRenderedImage(i);
|
RenderedImage renderedImage = imageDecoder.decodeAsRenderedImage(i);
|
||||||
|
@ -98,13 +94,11 @@ public class ConvertPicUtil {
|
||||||
pb.add(fileJpg.toString());
|
pb.add(fileJpg.toString());
|
||||||
pb.add("JPEG");
|
pb.add("JPEG");
|
||||||
pb.add(jpegEncodeParam);
|
pb.add(jpegEncodeParam);
|
||||||
|
|
||||||
RenderedOp renderedOp = JAI.create("filestore", pb);
|
RenderedOp renderedOp = JAI.create("filestore", pb);
|
||||||
renderedOp.dispose();
|
renderedOp.dispose();
|
||||||
|
|
||||||
logger.info("每页分别保存至: " + fileJpg.getCanonicalPath());
|
logger.info("每页分别保存至: " + fileJpg.getCanonicalPath());
|
||||||
} else {
|
} else {
|
||||||
logger.info("JPG文件已存在: " + fileJpg.getCanonicalPath());
|
// logger.info("JPG文件已存在: " + fileJpg.getCanonicalPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
listImageFiles.add(strJpgUrl);
|
listImageFiles.add(strJpgUrl);
|
||||||
|
@ -131,101 +125,45 @@ public class ConvertPicUtil {
|
||||||
* @param strJpgFile 输入的jpg的路径和文件名
|
* @param strJpgFile 输入的jpg的路径和文件名
|
||||||
* @param strPdfFile 输出的pdf的路径和文件名
|
* @param strPdfFile 输出的pdf的路径和文件名
|
||||||
*/
|
*/
|
||||||
public static void convertJpg2Pdf(String strJpgFile, String strPdfFile) {
|
private static final int FIT_WIDTH = 500;
|
||||||
Document document = new Document();
|
private static final int FIT_HEIGHT = 900;
|
||||||
// 设置文档页边距
|
public static boolean convertJpg2Pdf(String strJpgFile, String strPdfFile) {
|
||||||
document.setMargins(0, 0, 0, 0);
|
Document document= null;
|
||||||
|
RandomAccessFileOrArray rafa = null;
|
||||||
FileOutputStream fos = null;
|
|
||||||
try {
|
try {
|
||||||
fos = new FileOutputStream(strPdfFile);
|
document = new Document();
|
||||||
PdfWriter.getInstance(document, fos);
|
PdfWriter.getInstance(document, new FileOutputStream(strPdfFile));
|
||||||
// 打开文档
|
|
||||||
document.open();
|
document.open();
|
||||||
// 获取图片的宽高
|
rafa = new RandomAccessFileOrArray(new FileChannelRandomAccessSource(new RandomAccessFile(strJpgFile, "r").getChannel()));
|
||||||
Image image = Image.getInstance(strJpgFile);
|
int pages = TiffImage.getNumberOfPages(rafa);
|
||||||
float floatImageHeight = image.getScaledHeight();
|
Image image;
|
||||||
float floatImageWidth = image.getScaledWidth();
|
for (int i = 1; i <= pages; i++) {
|
||||||
// 设置页面宽高与图片一致
|
try {
|
||||||
Rectangle rectangle = new Rectangle(floatImageWidth, floatImageHeight);
|
image = TiffImage.getTiffImage(rafa, i);
|
||||||
document.setPageSize(rectangle);
|
image.scaleToFit(FIT_WIDTH, FIT_HEIGHT);
|
||||||
// 图片居中
|
document.add(image);
|
||||||
image.setAlignment(Image.ALIGN_CENTER);
|
} catch (Exception e) {
|
||||||
//新建一页添加图片
|
document.close();
|
||||||
document.newPage();
|
rafa.close();
|
||||||
document.add(image);
|
e.printStackTrace();
|
||||||
} catch (Exception ioe) {
|
}
|
||||||
ioe.printStackTrace();
|
}
|
||||||
} finally {
|
document.close();
|
||||||
//关闭文档
|
rafa.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
System.out.println("错误:"+ e.getMessage());
|
||||||
|
}
|
||||||
|
finally {
|
||||||
document.close();
|
document.close();
|
||||||
try {
|
try {
|
||||||
assert fos != null;
|
rafa.close();
|
||||||
fos.flush();
|
|
||||||
fos.close();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将Tif图片转换为Pdf文件(支持多页Tif)
|
|
||||||
*
|
|
||||||
* @param strTifFile 输入的tif的路径和文件名
|
|
||||||
* @param strPdfFile 输出的pdf的路径和文件名
|
|
||||||
* @return File
|
|
||||||
*/
|
|
||||||
public static File convertTif2Pdf(String strTifFile, String strPdfFile) {
|
|
||||||
try {
|
|
||||||
RandomAccessFileOrArray rafa = new RandomAccessFileOrArray(strTifFile);
|
|
||||||
|
|
||||||
Document document = new Document();
|
|
||||||
// 设置文档页边距
|
|
||||||
document.setMargins(0, 0, 0, 0);
|
|
||||||
|
|
||||||
PdfWriter.getInstance(document, new FileOutputStream(strPdfFile));
|
|
||||||
document.open();
|
|
||||||
int intPages = TiffImage.getNumberOfPages(rafa);
|
|
||||||
Image image;
|
|
||||||
File filePDF;
|
|
||||||
|
|
||||||
if (intPages == 1) {
|
|
||||||
String strJpg = strTifFile.substring(0, strTifFile.lastIndexOf(".")) + ".jpg";
|
|
||||||
File fileJpg = new File(strJpg);
|
|
||||||
List<String> listPic2Jpg = convertTif2Jpg(strTifFile, strJpg);
|
|
||||||
|
|
||||||
if (listPic2Jpg != null && fileJpg.exists()) {
|
|
||||||
convertJpg2Pdf(strJpg, strPdfFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
for (int i = 1; i <= intPages; i++) {
|
|
||||||
image = TiffImage.getTiffImage(rafa, i);
|
|
||||||
// 设置页面宽高与图片一致
|
|
||||||
Rectangle pageSize = new Rectangle(image.getScaledWidth(), image.getScaledHeight());
|
|
||||||
document.setPageSize(pageSize);
|
|
||||||
// 图片居中
|
|
||||||
image.setAlignment(Image.ALIGN_CENTER);
|
|
||||||
//新建一页添加图片
|
|
||||||
document.newPage();
|
|
||||||
document.add(image);
|
|
||||||
}
|
|
||||||
|
|
||||||
document.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
rafa.close();
|
|
||||||
|
|
||||||
filePDF = new File(strPdfFile);
|
|
||||||
|
|
||||||
return filePDF;
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue