/*     */ package com.picc.grab.util;
/*     */ 
/*     */ import java.awt.image.BufferedImage;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.net.HttpURLConnection;
/*     */ import java.net.URL;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import javax.imageio.ImageIO;
/*     */ import net.coobird.thumbnailator.Thumbnails;
/*     */ import net.coobird.thumbnailator.geometry.Position;
/*     */ import net.coobird.thumbnailator.geometry.Positions;
/*     */ import org.apache.commons.lang3.StringUtils;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import sun.misc.BASE64Decoder;
/*     */ import sun.misc.BASE64Encoder;
/*     */ 
/*     */ 
/*     */ public class ThumbnailatorUtil
/*     */ {
/*  26 */   private static final Logger logger = LoggerFactory.getLogger(ThumbnailatorUtil.class);
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static List<String> imgToThumbnail(List<String> imagePath, int imgSize) {
/*  33 */     List<String> thumbList = new ArrayList<>();
/*  34 */     for (int i = 0; i < imagePath.size(); i++) {
/*  35 */       thumbList.add(imgToThumbnail(imagePath.get(i), imgSize));
/*     */     }
/*  37 */     return thumbList;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String imgToThumbnail(String imagePath, int imgSize) {
/*     */     try {
/*  47 */       BufferedImage image = ImageIO.read(new File(imagePath));
/*  48 */       int imageWidth = image.getWidth();
/*  49 */       int imageHeight = image.getHeight();
/*  50 */       if (imageWidth != imageHeight) {
/*  51 */         int imgLength = imgSize;
/*  52 */         if (imageWidth > imageHeight) {
/*  53 */           imgLength = imageHeight;
/*  54 */           image = Thumbnails.of(new BufferedImage[] { image }).sourceRegion((Position)Positions.CENTER, imgLength, imgLength).size(imgSize, imgSize).asBufferedImage();
/*     */         } else {
/*  56 */           imgLength = imageWidth;
/*  57 */           image = Thumbnails.of(new BufferedImage[] { image }).sourceRegion((Position)Positions.TOP_LEFT, imgLength, imgLength).size(imgSize, imgSize).asBufferedImage();
/*     */         } 
/*     */       } else {
/*  60 */         image = Thumbnails.of(new BufferedImage[] { image }).size(imgSize, imgSize).asBufferedImage();
/*     */       } 
/*     */       
/*  63 */       return getImageBinary(image);
/*  64 */     } catch (Exception e) {
/*  65 */       e.printStackTrace();
/*     */       
/*  67 */       return "";
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String imgToBase64(String imagePath) {
/*     */     try {
/*  78 */       String formatName = imagePath.substring(imagePath.lastIndexOf(".") + 1, imagePath.length());
/*  79 */       BufferedImage image = ImageIO.read(new File(imagePath));
/*  80 */       String baseStr = getImageBinary(image, formatName);
/*  81 */       logger.info("ThumbnailatorUtil imgToBase64 is not null.");
/*  82 */       return baseStr;
/*  83 */     } catch (Exception e) {
/*  84 */       e.printStackTrace();
/*     */       
/*  86 */       return "";
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String imgToBase64(InputStream input, String imagePath) {
/*  95 */     if (input != null) {
/*     */       
/*     */       try {
/*  98 */         String formatName = imagePath.substring(imagePath.lastIndexOf(".") + 1, imagePath.length());
/*  99 */         BufferedImage image = ImageIO.read(input);
/* 100 */         String baseStr = getImageBinary(image, formatName);
/* 101 */         logger.info("ThumbnailatorUtil imgToBase64 is not null.");
/* 102 */         return baseStr;
/* 103 */       } catch (Exception e) {
/* 104 */         logger.info("ThumbnailatorUtil imgToBase64  error: " + e.getMessage(), e);
/*     */       } 
/*     */     } else {
/* 107 */       logger.info("ThumbnailatorUtil imgToBase64 error input is null.");
/*     */     } 
/* 109 */     return "";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String imgToThumbnail(InputStream inputStream, int imgSize, String type) {
/*     */     try {
/* 121 */       BufferedImage image = ImageIO.read(inputStream);
/* 122 */       int imageWidth = image.getWidth();
/* 123 */       int imageHeight = image.getHeight();
/* 124 */       if (imageWidth != imageHeight) {
/* 125 */         int imgLength = imgSize;
/* 126 */         if (imageWidth > imageHeight) {
/* 127 */           imgLength = imageHeight;
/* 128 */           image = Thumbnails.of(new BufferedImage[] { image }).sourceRegion((Position)Positions.CENTER, imgLength, imgLength).size(imgSize, imgSize).asBufferedImage();
/*     */         } else {
/* 130 */           imgLength = imageWidth;
/* 131 */           image = Thumbnails.of(new BufferedImage[] { image }).sourceRegion((Position)Positions.TOP_LEFT, imgLength, imgLength).size(imgSize, imgSize).asBufferedImage();
/*     */         } 
/*     */       } else {
/* 134 */         image = Thumbnails.of(new BufferedImage[] { image }).size(imgSize, imgSize).asBufferedImage();
/*     */       } 
/* 136 */       if (!StringUtils.isEmpty(type)) {
/* 137 */         return getImageBinary(image, type);
/*     */       }
/* 139 */       return getImageBinary(image);
/*     */     }
/* 141 */     catch (Exception e) {
/* 142 */       e.printStackTrace();
/*     */       
/* 144 */       return "";
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String getImageBinary(BufferedImage bi) {
/* 153 */     BASE64Encoder encoder = new BASE64Encoder();
/*     */     try {
/* 155 */       ByteArrayOutputStream baos = new ByteArrayOutputStream();
/* 156 */       ImageIO.write(bi, "jpg", baos);
/* 157 */       byte[] bytes = baos.toByteArray();
/* 158 */       return encoder.encodeBuffer(bytes).trim();
/* 159 */     } catch (IOException e) {
/* 160 */       e.printStackTrace();
/*     */       
/* 162 */       return "";
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String getImageBinary(BufferedImage bi, String type) {
/* 172 */     BASE64Encoder encoder = new BASE64Encoder();
/*     */     try {
/* 174 */       ByteArrayOutputStream baos = new ByteArrayOutputStream();
/* 175 */       ImageIO.write(bi, type, baos);
/* 176 */       byte[] bytes = baos.toByteArray();
/* 177 */       return encoder.encodeBuffer(bytes).trim();
/* 178 */     } catch (IOException e) {
/* 179 */       e.printStackTrace();
/*     */       
/* 181 */       return "";
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static void base64StringToImage(String base64String, String savePath) {
/* 190 */     BASE64Decoder decoder = new BASE64Decoder();
/*     */     try {
/* 192 */       byte[] bytes1 = decoder.decodeBuffer(base64String);
/* 193 */       ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
/* 194 */       BufferedImage bi1 = ImageIO.read(bais);
/* 195 */       File file = new File(savePath);
/* 196 */       ImageIO.write(bi1, "jpg", file);
/* 197 */     } catch (IOException e) {
/* 198 */       e.printStackTrace();
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static List<String> downImgToThumbnail(List<String> destUrl, int imgSize) {
/* 208 */     List<String> thumbList = new ArrayList<>();
/* 209 */     for (int i = 0; i < destUrl.size(); i++) {
/* 210 */       thumbList.add(downImgToThumbnail(destUrl.get(i), imgSize));
/*     */     }
/* 212 */     return thumbList;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String downImgToThumbnail(String destUrl, int imgSize) {
/* 222 */     HttpURLConnection httpUrl = null;
/* 223 */     URL url = null;
/*     */     try {
/* 225 */       url = new URL(destUrl);
/* 226 */       httpUrl = (HttpURLConnection)url.openConnection();
/* 227 */       httpUrl.connect();
/*     */       
/* 229 */       BufferedImage image = ImageIO.read(httpUrl.getInputStream());
/* 230 */       int imageWidth = image.getWidth();
/* 231 */       int imageHeight = image.getHeight();
/* 232 */       if (imageWidth != imageHeight) {
/* 233 */         int imgLength = imgSize;
/* 234 */         if (imageWidth > imageHeight) {
/* 235 */           imgLength = imageHeight;
/* 236 */           image = Thumbnails.of(new BufferedImage[] { image }).sourceRegion((Position)Positions.CENTER, imgLength, imgLength).size(imgSize, imgSize).asBufferedImage();
/*     */         } else {
/* 238 */           imgLength = imageWidth;
/* 239 */           image = Thumbnails.of(new BufferedImage[] { image }).sourceRegion((Position)Positions.TOP_LEFT, imgLength, imgLength).size(imgSize, imgSize).asBufferedImage();
/*     */         } 
/*     */       } else {
/* 242 */         image = Thumbnails.of(new BufferedImage[] { image }).size(imgSize, imgSize).asBufferedImage();
/*     */       } 
/*     */       
/* 245 */       return getImageBinary(image);
/* 246 */     } catch (Exception e) {
/* 247 */       e.printStackTrace();
/*     */     } finally {
/*     */       try {
/* 250 */         httpUrl.disconnect();
/* 251 */       } catch (Exception e) {
/* 252 */         e.printStackTrace();
/*     */       } 
/*     */     } 
/* 255 */     return "";
/*     */   }
/*     */   
/*     */   public static void main(String[] args) {
/* 259 */     String str = "";
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     
/* 265 */     str = downImgToThumbnail("http://n.sinaimg.cn/news/transform/20151113/axeo-fxksqis4775583.jpg", 70);
/* 266 */     System.out.println(str);
/* 267 */     base64StringToImage(str, "C:/Users/toptime/Desktop/Thumbnailator/test/x.jpg");
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static InputStream imgToMin(InputStream inputStream, int imgSize, String type) throws IOException {
/* 278 */     BufferedImage image = ImageIO.read(inputStream);
/* 279 */     image = Thumbnails.of(new BufferedImage[] { image }).size(imgSize, imgSize).asBufferedImage();
/* 280 */     ByteArrayOutputStream os = new ByteArrayOutputStream();
/* 281 */     ImageIO.write(image, type.replace(".", ""), os);
/* 282 */     InputStream input = new ByteArrayInputStream(os.toByteArray());
/* 283 */     return input;
/*     */   }
/*     */ }


/* Location:              C:\Users\Administrator\Desktop\ROOT\WEB-INF\classes\!\com\picc\gra\\util\ThumbnailatorUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */