pdf转jpg
//filePath PDF物理路径
// dirPath生成的物理路径
public static List<String> pdf2Img(String filePath,String dirPath) {
float scale=1.5f//缩放比例
List<String> result = new ArrayList<String>();
try {
File tempFile= new File(filePath);//
String fileName = tempFile.getName();//文件名
String newPath=tempFile.getParent();//服务器文件父路径
File dirFile= new File(dirPath);
String newPath2=dirFile.getPath();//取到路径
String newFileName=fileName.substring(0, fileName.indexOf("."));
if (filePath.endsWith("pdf") || filePath.endsWith("PDF")) {
PDDocument document = null;
try {
document = PDDocument.load(new File(filePath));
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < 1; page++) {
BufferedImage bim =pdfRenderer.renderImage(page,scale);
String imgName = newFileName+ ".jpg";
String imgPath = newPath+ File.separator + newFileName+ ".jpg";
String imgPath2 = newPath2+ File.separator + newFileName+ ".jpg";
ImageIOUtil.writeImage(bim, imgPath, 200);
result.add(imgName);
result.add(imgPath2);
}
} finally {
if (null != document) {
document.close();
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
jpg转base64
@SuppressWarnings("restriction")
public String imageChangeBase64(String imagePath){
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imagePath);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}