You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					260 lines
				
				9.0 KiB
			
		
		
			
		
	
	
					260 lines
				
				9.0 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								package com.ynxbd.common.helper.common;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import org.apache.pdfbox.pdmodel.PDDocument;
							 | 
						||
| 
								 | 
							
								import org.apache.pdfbox.rendering.ImageType;
							 | 
						||
| 
								 | 
							
								import org.apache.pdfbox.rendering.PDFRenderer;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import javax.imageio.ImageIO;
							 | 
						||
| 
								 | 
							
								import java.awt.image.BufferedImage;
							 | 
						||
| 
								 | 
							
								import java.io.ByteArrayOutputStream;
							 | 
						||
| 
								 | 
							
								import java.io.File;
							 | 
						||
| 
								 | 
							
								import java.io.FileInputStream;
							 | 
						||
| 
								 | 
							
								import java.io.IOException;
							 | 
						||
| 
								 | 
							
								import java.nio.charset.StandardCharsets;
							 | 
						||
| 
								 | 
							
								import java.util.Base64;
							 | 
						||
| 
								 | 
							
								import java.util.UUID;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @Author wsq
							 | 
						||
| 
								 | 
							
								 * @Date 2020/8/3 15:37
							 | 
						||
| 
								 | 
							
								 * @Copyright @ 2020 云南新八达科技有限公司 All rights reserved.
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								public class Base64Helper {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * BASE64解密
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param data data
							 | 
						||
| 
								 | 
							
								     * @return 解密后的内容
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static String decode(String data) {
							 | 
						||
| 
								 | 
							
								        return new String(java.util.Base64.getDecoder().decode(data), StandardCharsets.UTF_8); //
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * BASE64解密
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param data 解密
							 | 
						||
| 
								 | 
							
								     * @return 解密后的内容
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static byte[] decodeByte(String data) {
							 | 
						||
| 
								 | 
							
								        try {
							 | 
						||
| 
								 | 
							
								            return java.util.Base64.getDecoder().decode(data);
							 | 
						||
| 
								 | 
							
								        } catch (Exception e) {
							 | 
						||
| 
								 | 
							
								            e.printStackTrace();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return null;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * BASE64加密
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param data 需要加密的字符串
							 | 
						||
| 
								 | 
							
								     * @return 加密后的字符串
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static String encode(String data) {
							 | 
						||
| 
								 | 
							
								        return java.util.Base64.getEncoder().encodeToString(data.getBytes(StandardCharsets.UTF_8));
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * BASE64加密
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param obj 需要加密的字符串
							 | 
						||
| 
								 | 
							
								     * @return 加密后的字符串
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static String encode(Object obj, Boolean isEncryption) {
							 | 
						||
| 
								 | 
							
								        if (obj == null) return null;
							 | 
						||
| 
								 | 
							
								        String encode;
							 | 
						||
| 
								 | 
							
								        if (obj.getClass() != String.class) {
							 | 
						||
| 
								 | 
							
								            encode = JsonHelper.toJsonString(obj);
							 | 
						||
| 
								 | 
							
								        } else {
							 | 
						||
| 
								 | 
							
								            encode = obj.toString();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if ("".equals(encode)) {
							 | 
						||
| 
								 | 
							
								            return null;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        encode = java.util.Base64.getEncoder().encodeToString(encode.getBytes(StandardCharsets.UTF_8));
							 | 
						||
| 
								 | 
							
								        if (isEncryption) {
							 | 
						||
| 
								 | 
							
								            String uuid = UUID.randomUUID().toString().replace("-", "");
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            if (encode.length() > 25) {
							 | 
						||
| 
								 | 
							
								                String begStr = encode.substring(0, 10);
							 | 
						||
| 
								 | 
							
								                String endStr = encode.substring(encode.length() - 15, encode.length() - 5);
							 | 
						||
| 
								 | 
							
								                String end = encode.substring(encode.length() - 5);
							 | 
						||
| 
								 | 
							
								                String middle = encode.substring(10, encode.length() - 15);
							 | 
						||
| 
								 | 
							
								                return uuid + endStr + middle + begStr + end;
							 | 
						||
| 
								 | 
							
								            } else {
							 | 
						||
| 
								 | 
							
								                encode = uuid + encode;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return encode;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 判断是否只有数字和字母
							 | 
						||
| 
								 | 
							
								    public static boolean isLetterDigit(String str) {
							 | 
						||
| 
								 | 
							
								        return str.matches("^[a-z0-9A-Z]+$");
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 解密
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param data 需要解密的字符串
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static String decodeEn(String data) {
							 | 
						||
| 
								 | 
							
								        try {
							 | 
						||
| 
								 | 
							
								            if (data == null) {
							 | 
						||
| 
								 | 
							
								                return null;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            int dataLen = data.length();
							 | 
						||
| 
								 | 
							
								            if (dataLen > 32) { // 截取掉uuid
							 | 
						||
| 
								 | 
							
								                data = data.substring(32);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            if (dataLen > 57) { // 超过57位的需要调换前后位置
							 | 
						||
| 
								 | 
							
								                String endStr = data.substring(0, 10);
							 | 
						||
| 
								 | 
							
								                String begStr = data.substring(data.length() - 15, data.length() - 5);
							 | 
						||
| 
								 | 
							
								                String middle = data.substring(10, data.length() - 15);
							 | 
						||
| 
								 | 
							
								                String end = data.substring(data.length() - 5);
							 | 
						||
| 
								 | 
							
								                data = begStr + middle + endStr + end;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            String deData = new String(Base64.getDecoder().decode(data), StandardCharsets.UTF_8);// base64解密
							 | 
						||
| 
								 | 
							
								            if (!isLetterDigit(deData)) {
							 | 
						||
| 
								 | 
							
								                return null;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            return deData;
							 | 
						||
| 
								 | 
							
								        } catch (Exception e) {
							 | 
						||
| 
								 | 
							
								            return null;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 将图片转换成Base64编码
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param imgFile 图片文件
							 | 
						||
| 
								 | 
							
								     * @return base64
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static String imageToBase(File imgFile) {
							 | 
						||
| 
								 | 
							
								        try (FileInputStream fis = new FileInputStream(imgFile)) {
							 | 
						||
| 
								 | 
							
								            byte[] buffer = new byte[(int) imgFile.length()];
							 | 
						||
| 
								 | 
							
								            fis.read(buffer);
							 | 
						||
| 
								 | 
							
								            return java.util.Base64.getEncoder().encodeToString(buffer);
							 | 
						||
| 
								 | 
							
								        } catch (Exception e) {
							 | 
						||
| 
								 | 
							
								            e.printStackTrace();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return null;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public static String pdfBaseToImgBase(String base64) {
							 | 
						||
| 
								 | 
							
								        return pdfBaseToImgBase(base64, null);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public static String pdfBaseToImgBase(String base64, Integer dpi) {
							 | 
						||
| 
								 | 
							
								        if (base64 == null) {
							 | 
						||
| 
								 | 
							
								            return null;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        String imgBase64;
							 | 
						||
| 
								 | 
							
								        PDDocument pdDoc = null;
							 | 
						||
| 
								 | 
							
								        ByteArrayOutputStream bos = null;
							 | 
						||
| 
								 | 
							
								        if (dpi == null) {
							 | 
						||
| 
								 | 
							
								            dpi = 100;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        try {
							 | 
						||
| 
								 | 
							
								            // Base64解码
							 | 
						||
| 
								 | 
							
								            byte[] pdfBytes = java.util.Base64.getDecoder().decode(base64);
							 | 
						||
| 
								 | 
							
								            pdDoc = PDDocument.load(pdfBytes);
							 | 
						||
| 
								 | 
							
								            int size = pdDoc.getNumberOfPages();
							 | 
						||
| 
								 | 
							
								            /*图像合并使用的参数*/
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            int width = 0;
							 | 
						||
| 
								 | 
							
								            int height = 0;
							 | 
						||
| 
								 | 
							
								            // 保存一张图片中的RGB数据
							 | 
						||
| 
								 | 
							
								            int[] singleImgRGB;
							 | 
						||
| 
								 | 
							
								            //保存每张图片的像素值
							 | 
						||
| 
								 | 
							
								            BufferedImage imageResult = null;
							 | 
						||
| 
								 | 
							
								            // 利用PdfBox生成图像
							 | 
						||
| 
								 | 
							
								            PDFRenderer renderer = new PDFRenderer(pdDoc);
							 | 
						||
| 
								 | 
							
								            /* 根据总页数, 按照50页生成一张长图片的逻辑, 进行拆分--每50页转成1张图片--总计循环的次数 */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            BufferedImage image;
							 | 
						||
| 
								 | 
							
								            int imageHeight, imageWidth, pageIndex;
							 | 
						||
| 
								 | 
							
								            int totalCount = pdDoc.getNumberOfPages() / size + 1;
							 | 
						||
| 
								 | 
							
								            for (int m = 0; m < totalCount; m++) {
							 | 
						||
| 
								 | 
							
								                for (int i = 0; i < size; i++) {
							 | 
						||
| 
								 | 
							
								                    pageIndex = i + (m * size);
							 | 
						||
| 
								 | 
							
								                    if (pageIndex == pdDoc.getNumberOfPages()) {
							 | 
						||
| 
								 | 
							
								                        break;
							 | 
						||
| 
								 | 
							
								                    }
							 | 
						||
| 
								 | 
							
								                    // 96为图片的dpi,dpi越大,则图片越清晰,图片越大,转换耗费的时间也越多
							 | 
						||
| 
								 | 
							
								                    image = renderer.renderImageWithDPI(pageIndex, dpi, ImageType.RGB);
							 | 
						||
| 
								 | 
							
								                    imageHeight = image.getHeight();
							 | 
						||
| 
								 | 
							
								                    imageWidth = image.getWidth();
							 | 
						||
| 
								 | 
							
								                    if (i == 0) {
							 | 
						||
| 
								 | 
							
								                        // 计算高度和偏移量 使用第一张图片宽度;
							 | 
						||
| 
								 | 
							
								                        width = imageWidth;
							 | 
						||
| 
								 | 
							
								                        // 保存每页图片的像素值
							 | 
						||
| 
								 | 
							
								                        // 加个判断:如果m次循环后所剩的图片总数小于pageLength,则图片高度按剩余的张数绘制,否则会出现长图片下面全是黑色的情况
							 | 
						||
| 
								 | 
							
								                        if ((pdDoc.getNumberOfPages() - m * size) < size) {
							 | 
						||
| 
								 | 
							
								                            imageResult = new BufferedImage(width, imageHeight * (pdDoc.getNumberOfPages() - m * size), BufferedImage.TYPE_INT_RGB);
							 | 
						||
| 
								 | 
							
								                        } else {
							 | 
						||
| 
								 | 
							
								                            imageResult = new BufferedImage(width, imageHeight * size, BufferedImage.TYPE_INT_RGB);
							 | 
						||
| 
								 | 
							
								                        }
							 | 
						||
| 
								 | 
							
								                    } else {
							 | 
						||
| 
								 | 
							
								                        // 将高度不断累加
							 | 
						||
| 
								 | 
							
								                        height += imageHeight;
							 | 
						||
| 
								 | 
							
								                    }
							 | 
						||
| 
								 | 
							
								                    singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
							 | 
						||
| 
								 | 
							
								                    imageResult.setRGB(0, height, width, imageHeight, singleImgRGB, 0, width);
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                // 这个很重要,下面会有说明
							 | 
						||
| 
								 | 
							
								                height = 0;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            if (imageResult == null) {
							 | 
						||
| 
								 | 
							
								                return null;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            bos = new ByteArrayOutputStream();//io流
							 | 
						||
| 
								 | 
							
								            ImageIO.write(imageResult, "png", bos);//写入流中
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            imgBase64 = java.util.Base64.getEncoder().encodeToString(bos.toByteArray()).trim();
							 | 
						||
| 
								 | 
							
								            imgBase64 = "data:image/png;base64," + imgBase64.replaceAll("\n", "").replaceAll("\r", ""); //删除 \r\n
							 | 
						||
| 
								 | 
							
								            return imgBase64;
							 | 
						||
| 
								 | 
							
								        } catch (IOException e) {
							 | 
						||
| 
								 | 
							
								            e.printStackTrace();
							 | 
						||
| 
								 | 
							
								        } finally {
							 | 
						||
| 
								 | 
							
								            try {
							 | 
						||
| 
								 | 
							
								                if (bos != null) {
							 | 
						||
| 
								 | 
							
								                    bos.flush();
							 | 
						||
| 
								 | 
							
								                    bos.close();
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								                if (pdDoc != null) {
							 | 
						||
| 
								 | 
							
								                    pdDoc.close();
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								            } catch (Exception e) {
							 | 
						||
| 
								 | 
							
								                e.printStackTrace();
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return null;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								//    public static void main(String[] args) {
							 | 
						||
| 
								 | 
							
								//        String a = encode("123456789011121314151123123", true);
							 | 
						||
| 
								 | 
							
								//        System.out.println(a);
							 | 
						||
| 
								 | 
							
								//        String s = decodeEn(a);
							 | 
						||
| 
								 | 
							
								//        System.out.println(s);
							 | 
						||
| 
								 | 
							
								//    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								//    public static void main(String[] args) {
							 | 
						||
| 
								 | 
							
								//        String encode = CodeHelper.get28UUID() + encode(JsonHelper.toJsonString(new ArrayList<>()));
							 | 
						||
| 
								 | 
							
								//        System.out.println(encode);
							 | 
						||
| 
								 | 
							
								//    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public static void main(String[] args) {
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |