先将Base64位字符转成图片

/*** base64字符串转化成图片* @param imgStr Base64位编码* @return* @throws Exception*/public static File GenerateImage(String imgStr) throws Exception { // 对字节数组字符串进行Base64解码并生成图片if (imgStr == null) // 图像数据为空return null;imgStr = imgStr.substring(22, imgStr.length());BASE64Decoder decoder = new BASE64Decoder();// Base64解码byte[] b = decoder.decodeBuffer(imgStr);for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {// 调整异常数据b[i] += 256;}}//图片文件目录File imgDir = new File(PropertiesLoader.get("attachment.image.server.local") + "projectRevealReport/revealReportTemp");//如果图片目录不存在 则创建目录if(!imgDir.exists()) imgDir.mkdirs();// 生成jpeg图片String imgFilePath = PropertiesLoader.get("attachment.image.server.local") + "projectRevealReport/revealReportTemp/revealReportTemp.png";File imgFile = new File(imgFilePath);if (!imgFile.exists()) {imgFile.createNewFile();}OutputStream out = new FileOutputStream(imgFilePath);out.write(b);out.flush();out.close();// 拆分图片splidImg(imgFilePath, 3508);imgFile.delete();return imgDir;}

先按照A4纸的大小去截取图片;
如果高度上有内容,则截取高度+10px,直到无内容时,截取;

/*** 拆分图片* @param imgPath*/private static void splidImg(String imgPath, int height){try {String basePath = imgPath.substring(0,imgPath.lastIndexOf("/"));BufferedImage im = ImageIO.read(new File(imgPath));if(im.getHeight() > height){// 判断是否是纯色;每次高度可自定义if(isSimpleColorImg(im, 0, height, im.getWidth(), height + 10, 0.98f)){imageCut(0, 0, im.getWidth(), height, imgPath, basePath + "/1.png");imageCut(0, height, im.getWidth(), im.getHeight() - height, imgPath, basePath + "/2-Temp.png");// 操作第二页图片 File file2 = new File(basePath + "/2.png");   file2.createNewFile();File fileTemp2 = new File(basePath + "/2-Temp.png");BufferedImage bi = new BufferedImage(im.getWidth(), height, BufferedImage.TYPE_INT_RGB);Graphics2D g2 = (Graphics2D)bi.getGraphics();g2.setBackground(Color.WHITE);BufferedImage imTemp2 = ImageIO.read(fileTemp2);g2.clearRect(0, 0, imTemp2.getWidth(), height);   g2.drawImage(imTemp2, 0, 0, imTemp2.getWidth(), imTemp2.getHeight(), null);  g2.dispose();  ImageIO.write(bi, "png", file2);   // 删除第二页临时文件fileTemp2.delete();} else {//每次高度可自定义height += 10;splidImg(imgPath, height);}}} catch (IOException e) {e.printStackTrace();}}/*** 图片剪裁* @param x 距离左上角的x轴距离* @param y 距离左上角的y轴距离* @param width 宽度* @param height 高度* @param sourcePath 图片源* @param descpath 目标位置*/public static void imageCut(int x, int y, int width, int height, String sourcePath, String descpath) {FileInputStream is = null;ImageInputStream iis = null;try {is = new FileInputStream(sourcePath);String fileSuffix = sourcePath.substring(sourcePath.lastIndexOf(".") + 1);Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(fileSuffix);ImageReader reader = it.next();iis = ImageIO.createImageInputStream(is);reader.setInput(iis, true);ImageReadParam param = reader.getDefaultReadParam();java.awt.Rectangle rect = new java.awt.Rectangle(x, y, width, height);param.setSourceRegion(rect);BufferedImage bi = reader.read(0, param);ImageIO.write(bi, fileSuffix, new File(descpath));} catch (Exception ex) {ex.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}is = null;}if (iis != null) {try {iis.close();} catch (IOException e) {e.printStackTrace();}iis = null;}}}/*** 判断是否为纯色* @param imgPath 图片源* @param percent 纯色百分比,即大于此百分比为同一种颜色则判定为纯色,范围[0-1]* @return* @throws IOException*/public static boolean isSimpleColorImg(BufferedImage src,int x, int y, int width, int height, float percent) throws IOException{int count=0,pixTemp=0,pixel=0;for(int i=x;i<width;i++){for(int j=y;j<height;j++){pixel=src.getRGB(i, j);if(pixel==pixTemp) //如果上一个像素点和这个像素点颜色一样的话,就判定为同一种颜色count++;elsecount=0;if((float)count/((height-y)*(width-x))>=percent) //如果连续相同的像素点大于设定的百分比的话,就判定为是纯色的图片return true;pixTemp=pixel;}}return false;}

图片转PDF,图片过长智能截取相关推荐

  1. Python工具脚本,PDF文件批量转图片(pdf图片提取器)工具(exe)

    前面写(抄袭)了一下转图片(提取图片)的源码,包括PDF文件以及PPT文件,这里本渣渣抽空进行了exe打包,打包的是pdf文件转图片,当然使用的gui还是python的tkinter库,仅供参考和学习 ...

  2. 超简单!为图片和 PDF 上去掉水印

    文 | 某某白米饭 来源:Python 技术「ID: pythonall」 网上下载的 pdf 学习资料有一些会带有水印,非常影响阅读.比如下面的图片就是在 pdf 文件上截取出来的. 安装模块 PI ...

  3. Python 为图片和 PDF 上去掉水印,超简单快来学!

    python去水印 安装模块 导入需要用到的模块 获取图片的 RGB 图片去水印 PDF 去水印 图片转为 pdf 总结 网上下载的 pdf 学习资料有一些会带有水印,非常影响阅读. 比如下面的图片就 ...

  4. iText生成pdf(图片转PDF)

    前言 在学习的过程中笔者遇到过这种需求,就是将某个截取到的图片转化为PDF格式的.图片如何 转化为PDF格式的呢?首先想到的是PDF格式的文档是如何生成的,知道了这个就知道如何将图 片"插入 ...

  5. 用python一键去除图片、PDF水印

    嗨嗨,我是小圆,现在网络上的图片大多都有水印,我们每次找无水印的图片都有点难找. 但今天给大家分享一下 : 如何用python一键去除图片.PDF水印 看完学会后就不用担心 水印会影响阅读了 安装模块 ...

  6. 图片免费转pdf图片、图片免费转成word、图片免费转excel表格

    整个思想流程: ①.图片-->②.pdf图片-->③.word .excel: 这个流程是我所用方法基本转换的环节. 1.首先将jpg格式图片转换为pdf图片: 这里有许多工具可以使用: ...

  7. Linux入门(13)——Ubuntu16.04下将图片和pdf互转

    Ubuntu16.04下将图片和pdf互转 将图片转为PDF: convert 图片 PDF convert pic.jpg pic.pdf 将PDF转为图片: convert PDF 图片 conv ...

  8. 【办公软件都有哪些】万彩办公大师教程丨PDF图片抽取

    万彩办公大师的PDF 图片抽取工具能快速从Adobe PDF 文件中提取图片.提取后的图片可供重复编辑或使用.迅速抽取批量PDF文件中的图片并且可以以多种图片格式进行保存.更有图片尺寸过滤与预览设置, ...

  9. PDF转换图片,图片的切割,图片转换PDF以及PDF加水印等记录贴

    PDF转变为图片: 把图片进行切割: 把图片转变回PDF: 为PDF加水印文字: 为PDF加水印图片. 1,PDF转变为图片 /*** @author dalin*将PDF格式的文件转换成png文件* ...

  10. 免费图片转pdf的方法?学会图片转pdf很重要

    免费图片转pdf的方法?需要看网课的小伙伴们都知道,我们在看网课的时候,经常需要截取一些重要的图片以作复习之用.但是如果我们不将其整理成册的话,我们就需要一直翻阅相册,十分麻烦.其实我们完全可以将全部 ...

最新文章

  1. hibernate-session中的方法
  2. 好用的netcat工具
  3. 如何用一句话证明你是程序员?
  4. Python--网络编程-----基于UDP协议的套接字不会发生粘包
  5. 鼠标右键没有新建文件夹 右键没有新建选项怎办
  6. 如何让exe以管理员权限运行
  7. 第五章 机械臂的组装及控制
  8. 公网ip经常变动的解决方法
  9. Arquillian测试框架快速上手教程(四)- 使用Arquillian + Drone + Selenium + Graphene 进行Web自动化测试
  10. JAVA高效批量插入数据到数据库demo
  11. Python开发【十一章】:RabbitMQ队列
  12. 公司监事会的职责具体是什么
  13. 如何执行 Visual Paradigm 的静默安装丨安装教程
  14. smbclient访问海康威视H90个人网盘
  15. 单神经元PID控制(Simulink仿真+PLC代码)
  16. 当时的我很淘气openeim003
  17. 个人计算机中的防毒软件无法防御,win10系统中无法启动defender防御软件的处理办法...
  18. webofscience入口注册_Web of Science科研数据库平台
  19. TypeScript 命名空间整理
  20. 抖音上面用计算机解锁手机,抖音生命计算器

热门文章

  1. 因为专注所以成功 agile新版北京发布
  2. iOS15可以多开APP?其实是专注模式的功能
  3. web前端期末大作业网页设计与制作 ——汉口我的家乡旅游景点 5页HTML+CSS+JavaScript
  4. 程序猿的自救 从零备考NSCA/CSCS 2 阻力运动生物力学
  5. p牛java安全漫谈学习笔记(2)_反序列化与urldns链(基础)
  6. Non-Interactive Guessing Number
  7. 瘦客户端那些事 - 开篇
  8. vue 控制某个元素的显示或者隐藏之v-if属性
  9. elasticsearch配置告警方案问题记录
  10. ssm基于微信小程序的游泳馆管理系统+uinapp+java+计算机毕业设计