- 所需jar:https://download.csdn.net/download/zqq3436/10588117
- jdk版本:1.6
- 测试代码文件夹目录结构(pdf文件夹下有一些.pdf文件,转换好的图片,放在images目录下)
- pdf转图片,可实现将图片设置为黑白图片
- 循环File文件夹,批量pdf转图片
- 将本地目录下图片保存到另外位置,并转为黑白图片
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import com.itextpdf.text.pdf.PdfReader;
import sun.awt.image.ImageWatched.Link;
public class TestItext {
public static void main(String[] args) {
//遍历文件夹下所有pdf,并转为图片
iterateFolder("D:/测试pdf转图片");
//test.jpg图片保存到D:\根目录,并转为黑白片
Image2Gray(new File("C:\\Users\\zqq3436\\Desktop\\test.jpg"), "D:\\testdagl.jpg");
}
/**
* 遍历文件夹
*
* @param path
* 文件目录
*/
public static void iterateFolder(String path) {
File file = new File(path);
// 遍历path下的文件和目录,放在File数组中
File[] files = file.listFiles();
String appNo = "";
// 遍历files
for (File f : files) {
if (f.isDirectory()) {
// 不遍历iamges文件夹
if ("images".equals(f.getName())) {
continue;
}
// System.out.println("文件夹:" + f.getName());
// 递归获取文件
iterateFolder(path + "/" + f.getName());
} else {
System.out.println("文件:" + f.getName());
File fParent = f.getParentFile().getParentFile();
pdf2Image(f.getAbsolutePath(), f.getParentFile().getParent() + "/images", 300, fParent.getName());
}
}
}
/***
* PDF文件转PNG图片,全部页数
*
* @param PdfFilePath
* pdf完整路径
* @param imgFilePath
* 图片存放的文件夹
* @param dpi
* dpi越大转换后越清晰,相对转换速度越慢
* @param appNo
* 申请案号码,用于图片名称
* @return
*/
public static void pdf2Image(String PdfFilePath, String dstImgFolder, int dpi, String appNo) {
File file = new File(PdfFilePath);
PDDocument pdDocument;
try {
// 获取文件所在文件夹
String imgPDFPath = file.getParent();
int dot = file.getName().lastIndexOf('.');
// 获取图片文件名
String imagePDFName = file.getName().substring(0, dot);
// 获取图片存放文件夹
String imgFolderPath = null;
if (dstImgFolder.equals("")) {
// 图片基础路径+(图片文件名)
imgFolderPath = imgPDFPath;
// + File.separator + imagePDFName;
} else {
imgFolderPath = dstImgFolder;
// + File.separator + imagePDFName;
}
if (createDirectory(imgFolderPath)) {
pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
// dpi越大转换后越清晰,相对转换速度越慢
PdfReader reader = new PdfReader(PdfFilePath);
// 获取pdf的总页数
int pages = reader.getNumberOfPages();
StringBuffer imgFilePath = null;
for (int i = 0; i < pages; i++) {
// 每个pdf文件对应一个文件夹
String imgFilePathPrefix = appNo + "_" + imagePDFName;
// + File.separator + imagePDFName;
imgFilePath = new StringBuffer(imgFilePathPrefix);
imgFilePath.append("_");
imgFilePath.append(String.valueOf(i + 1));
imgFilePath.append(".jpg");
File dstFile = new File(imgFolderPath, imgFilePath.toString());
// 每页生成一个图片
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
/*//以下代码实现将图片转为黑白图片,有需求,可以将注释打开
image.getGraphics().drawImage(image, 0, 0, null);
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
Color pixel = new Color(image.getRGB(x, y));
image.setRGB(x, y, new Color(getGray(pixel), getGray(pixel), getGray(pixel)).getRGB());
}
}*/
ImageIO.write(image, "JPEG", dstFile);
}
System.out.println("PDF文档转jpg图片成功!");
pdDocument.close();
} else {
System.out.println("PDF文档转jpg图片失败:" + "创建" + imgFolderPath + "失败");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建目录
*
* @param folder
* @return
*/
private static boolean createDirectory(String folder) {
File dir = new File(folder);
if (dir.exists()) {
return true;
} else {
return dir.mkdirs();
}
}
/**
* 获取像素点的灰度
*
* @param pixel
* @return
*/
public static int getGray(Color color) {
return (color.getRed() * 30 + color.getGreen() * 60 + color.getBlue() * 10) / 100;
}
/**
* 读取文件,并保存
*
* @param originalFile
* 源文件
* @param result
* 目标文件
*/
public static void createImage(File originalFile, File result) {
try {
if (result.exists()) {// 校验该文件是否已存在
result.delete();// 删除对应的文件,从磁盘中删除
}
if (!result.exists()) {// 如果文件不存在
result.createNewFile();// 会在磁盘下创建文件,但此时大小为0K
}
FileInputStream in = new FileInputStream(originalFile);
FileOutputStream out = new FileOutputStream(result);// 指定要写入的图片
int n = 0;// 每次读取的字节长度
byte[] bb = new byte[1024];// 存储每次读取的内容
while ((n = in.read(bb)) != -1) {
out.write(bb, 0, n);// 将读取的内容,写入到输出流当中
}
// 执行完以上后,磁盘下的该文件才完整,大小是实际大小
out.close();// 关闭输入输出流
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取图片,并保存为黑白图片
*/
public static void Image2Gray(File file, String linkpath) {
try {
BufferedImage image = ImageIO.read(file);
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
Color pixel = new Color(image.getRGB(x, y));
image.setRGB(x, y, new Color(getGray(pixel), getGray(pixel), getGray(pixel)).getRGB());
}
}
ImageIO.write(image, "JPEG", new File(linkpath));
} catch (IOException e) {
e.printStackTrace();
}
}
}