package com.qxueyou.scc.base.util; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.imageio.ImageIO; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import sun.misc.BASE64Encoder; @SuppressWarnings("restriction") public class ImageUtils { private static Logger log = LogManager.getLogger("ImageUtils"); public static void doBeautiful(File file) { try { BufferedImage bi1 = ImageIO.read(file); Ellipse2D.Double shapeEllipse = new Ellipse2D.Double(0, 0, Math.min(bi1.getWidth(),bi1.getHeight()), Math.min(bi1.getWidth(),bi1.getHeight())); // 设置透明背景 BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TRANSLUCENT); Graphics2D g2 = bi2.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setClip(shapeEllipse); // 画图片 g2.drawImage(bi1, 0, 0, null); g2.dispose(); ImageIO.write(bi2, "PNG", file); } catch (IOException e) { log.error("图片处理失败", e); } } //矩形头像 public static void doRectangle(File file){ try { BufferedImage bi1 = ImageIO.read(file); Rectangle2D.Double shapeRectangle = new Rectangle2D.Double(0, 0, Math.min(bi1.getWidth(),bi1.getHeight()), Math.min(bi1.getWidth(),bi1.getHeight())); // 设置透明背景 BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TRANSLUCENT); Graphics2D g2 = bi2.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setClip(shapeRectangle); // 画图片 g2.drawImage(bi1, 0, 0, null); g2.dispose(); ImageIO.write(bi2, "PNG", file); } catch (IOException e) { log.error("图片处理失败", e); } } /** * 为图片生成缩略图 * @param file */ public static void decreaseSizeImage(File file) { try { BufferedImage bi1 = ImageIO.read(file); int width = bi1.getWidth(); int height = bi1.getHeight(); // 设置透明背景 BufferedImage bi2 = new BufferedImage(getPriviewImageWidth(width,height), getPriviewImageHeight(width,height), bi1.getType()); Graphics2D g2 = bi2.createGraphics(); // 画图片 Image image = bi1.getScaledInstance(getPriviewImageWidth(width,height), getPriviewImageHeight(width,height), Image.SCALE_DEFAULT); g2.drawImage(image, 0, 0, null); g2.dispose(); ImageIO.write(bi2, "PNG", createPreviewFile(file)); } catch (IOException e) { log.error("图片处理失败", e); } } public static File createPreviewFile(File f){ return new File(f.getParentFile().getAbsolutePath().concat("/pre_").concat(f.getName())); } private static int getPriviewImageWidth(int width,int height){ double wid = width; double hei = height; double ratioWid = wid / 120; double ratioHeight = hei / 160; if(ratioWid >= ratioHeight){ return 120; }else{ return (int) (wid/ratioHeight); } } private static int getPriviewImageHeight(int width,int height){ double wid = width; double hei = height; double ratioWid = wid / 120; double ratioHeight = hei / 160; if(ratioWid >= ratioHeight){ return (int) (hei/ratioWid); }else{ return 160; } } public static String encodeImgageToBase64(String imgStr) { // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 ByteArrayOutputStream outputStream = null; try { URL imageUrl= new URL(imgStr); BufferedImage bufferedImage = ImageIO.read(imageUrl); outputStream = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "jpg", outputStream); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串 } public static void main(String[] args){ doRectangle(new File("C:/Users/Public/Pictures/Sample Pictures/desert.jpg")); } }