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.
		
		
		
		
			
				
					185 lines
				
				6.1 KiB
			
		
		
			
		
	
	
					185 lines
				
				6.1 KiB
			| 
											3 years ago
										 | /*
 | ||
|  |  * *
 | ||
|  |  *  * @Description 图片处理
 | ||
|  |  *  * @Author zhangjf
 | ||
|  |  *  * @Date 2020/10/6 下午3:44
 | ||
|  |  *  * @Copyright @ 2020 云南新八达科技有限公司 All rights reserved.
 | ||
|  |  *
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | package com.ynxbd.common.helper.common;
 | ||
|  | 
 | ||
|  | import lombok.extern.slf4j.Slf4j;
 | ||
|  | import net.coobird.thumbnailator.Thumbnails;
 | ||
|  | import org.apache.commons.lang3.ObjectUtils;
 | ||
|  | import org.apache.pdfbox.pdmodel.PDDocument;
 | ||
|  | import org.apache.pdfbox.rendering.ImageType;
 | ||
|  | import org.apache.pdfbox.rendering.PDFRenderer;
 | ||
|  | 
 | ||
|  | import javax.imageio.ImageIO;
 | ||
|  | import javax.xml.bind.DatatypeConverter;
 | ||
|  | import java.awt.image.BufferedImage;
 | ||
|  | import java.io.*;
 | ||
|  | 
 | ||
|  | @Slf4j
 | ||
|  | public class ImageHelper {
 | ||
|  |     /**
 | ||
|  |      * 功能描述: 将图片字节码转换为图片
 | ||
|  |      *
 | ||
|  |      * @param by    图片的字节码
 | ||
|  |      * @param image 目标图片的地址,带图片的格式
 | ||
|  |      */
 | ||
|  |     public static void bufferToImage(byte[] by, String image) throws Exception {
 | ||
|  |         FileOutputStream fileOutputStream = new FileOutputStream(image);
 | ||
|  |         fileOutputStream.write(by);
 | ||
|  |         fileOutputStream.close();
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 功能描述: 将图片转换为字节码
 | ||
|  |      *
 | ||
|  |      * @param path 图片的路径地址
 | ||
|  |      * @return 图片的字节码
 | ||
|  |      */
 | ||
|  |     public static byte[] imageToByte(String path) throws Exception {
 | ||
|  |         FileInputStream fileInputStream = new FileInputStream(new File(path));
 | ||
|  |         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 | ||
|  |         byte[] bytes = new byte[1024];
 | ||
|  |         int len = 0;
 | ||
|  |         while ((len = fileInputStream.read(bytes)) != -1) {
 | ||
|  |             byteArrayOutputStream.write(bytes, 0, len);
 | ||
|  |         }
 | ||
|  |         fileInputStream.close();
 | ||
|  | 
 | ||
|  |         return byteArrayOutputStream.toByteArray();
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * PDF的base64转图片base64
 | ||
|  |      *
 | ||
|  |      * @param base64Pdf pdf
 | ||
|  |      * @param pdi       越小图片越小
 | ||
|  |      * @return base64
 | ||
|  |      */
 | ||
|  |     public static String pdfBaseToImgBase(String base64Pdf, Integer pdi) {
 | ||
|  |         if (ObjectUtils.isEmpty(base64Pdf)) {
 | ||
|  |             return null;
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         PDDocument doc = null;
 | ||
|  |         ByteArrayOutputStream out = null;
 | ||
|  |         try {
 | ||
|  |             String pageImgBase;
 | ||
|  |             byte[] bytes = DatatypeConverter.parseBase64Binary(base64Pdf);
 | ||
|  |             doc = PDDocument.load(bytes);
 | ||
|  |             if (doc == null) {
 | ||
|  |                 return null;
 | ||
|  |             }
 | ||
|  |             int size = doc.getNumberOfPages();
 | ||
|  |             if (size == 0) {
 | ||
|  |                 return null;
 | ||
|  |             }
 | ||
|  |             BufferedImage image;
 | ||
|  | //            BASE64Encoder encoder;
 | ||
|  |             byte[] imgByte;
 | ||
|  |             StringBuilder imgBase = new StringBuilder();
 | ||
|  |             for (int i = 0; i < size; i++) {
 | ||
|  |                 imgBase.append("data:image/jpeg;base64,");
 | ||
|  |                 image = new PDFRenderer(doc).renderImageWithDPI(i, pdi, ImageType.RGB);
 | ||
|  | 
 | ||
|  |                 out = new ByteArrayOutputStream();//io流
 | ||
|  |                 ImageIO.write(image, "jpg", out);//写入流中
 | ||
|  | 
 | ||
|  |                 imgByte = out.toByteArray(); //转换成字节
 | ||
|  | //                encoder = new BASE64Encoder();
 | ||
|  | //                pageImgBase = encoder.encodeBuffer(imgByte).trim(); //转换成base64串
 | ||
|  | 
 | ||
|  |                 pageImgBase = java.util.Base64.getEncoder().encodeToString(imgByte).trim();
 | ||
|  |                 imgBase.append(pageImgBase.replaceAll("\n", "").replaceAll("\r", "")); //删除 \r\n
 | ||
|  |                 if ((i + 1) != size) {
 | ||
|  |                     imgBase.append(",");
 | ||
|  |                 }
 | ||
|  |             }
 | ||
|  |             return imgBase.toString();
 | ||
|  |         } catch (IOException e) {
 | ||
|  |             e.printStackTrace();
 | ||
|  |         } finally {
 | ||
|  |             try {
 | ||
|  |                 if (out != null) out.close();
 | ||
|  | 
 | ||
|  |                 if (doc != null) doc.close();
 | ||
|  |             } catch (IOException e) {
 | ||
|  |                 e.printStackTrace();
 | ||
|  |             }
 | ||
|  |         }
 | ||
|  |         return null;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 功能描述: <br>
 | ||
|  |      * 〈功能详细描述〉图片字节码压缩
 | ||
|  |      *
 | ||
|  |      * @param picByte 图片的字节码
 | ||
|  |      * @return 压缩后的图片字节码
 | ||
|  |      */
 | ||
|  |     public static byte[] decompressPicByte(byte[] picByte, int compressType) {
 | ||
|  |         if (picByte == null) {
 | ||
|  |             return null;
 | ||
|  |         }
 | ||
|  |         ByteArrayInputStream is = new ByteArrayInputStream(picByte);
 | ||
|  |         Thumbnails.Builder<? extends InputStream> builder;
 | ||
|  |         if (compressType == 1)
 | ||
|  |             builder = Thumbnails.of(is).scale(0.5);
 | ||
|  |         else
 | ||
|  |             builder = Thumbnails.of(is).size(200, 400);
 | ||
|  |         try {
 | ||
|  |             BufferedImage bufferedImage = builder.asBufferedImage();
 | ||
|  |             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 | ||
|  |             ImageIO.write(bufferedImage, "jpeg", byteArrayOutputStream);
 | ||
|  | 
 | ||
|  |             return byteArrayOutputStream.toByteArray();
 | ||
|  |         } catch (IOException e) {
 | ||
|  |             e.printStackTrace();
 | ||
|  |         }
 | ||
|  |         return picByte;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public static String convertPDFtoImageBase64(byte[] bytesPDF) {
 | ||
|  |         InputStream targetStream = new ByteArrayInputStream(bytesPDF);
 | ||
|  |         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | ||
|  |         PDDocument document = null;
 | ||
|  |         try {
 | ||
|  |             document = PDDocument.load(targetStream);
 | ||
|  |             PDFRenderer renderer = new PDFRenderer(document);
 | ||
|  |             BufferedImage bi = renderer.renderImageWithDPI(0, 300);
 | ||
|  |             ImageIO.write(bi, "jpg", baos);
 | ||
|  |             baos.flush();
 | ||
|  |         } catch (Exception e) {
 | ||
|  |             log.error(e.getMessage(), e);
 | ||
|  |         } finally {
 | ||
|  |             if (document != null) {
 | ||
|  |                 try {
 | ||
|  |                     document.close();
 | ||
|  |                     baos.close();
 | ||
|  |                     log.info("End convert PDF to Images process");
 | ||
|  |                 } catch (IOException e) {
 | ||
|  |                     log.error(e.getMessage());
 | ||
|  |                 }
 | ||
|  |             }
 | ||
|  |         }
 | ||
|  |         return converPicBase64(baos.toByteArray());
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 功能描述: 将图片字节码进行base64 编码
 | ||
|  |      *
 | ||
|  |      * @param data 图片字节码
 | ||
|  |      * @return base64 编码后的字符串
 | ||
|  |      */
 | ||
|  |     public static String converPicBase64(byte[] data) {
 | ||
|  |         return DatatypeConverter.printBase64Binary(data);
 | ||
|  |     }
 | ||
|  | }
 |