|
@@ -1,180 +0,0 @@
|
|
|
-package cn.aiyangniu.api.common.util;
|
|
|
-
|
|
|
-import java.awt.AlphaComposite;
|
|
|
-import java.awt.Graphics2D;
|
|
|
-import java.awt.Image;
|
|
|
-import java.awt.RenderingHints;
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-import javax.swing.ImageIcon;
|
|
|
-import com.sun.image.codec.jpeg.JPEGCodec;
|
|
|
-import com.sun.image.codec.jpeg.JPEGImageEncoder;
|
|
|
-import java.util.Base64;
|
|
|
-import java.util.Base64.Decoder;
|
|
|
-
|
|
|
-/**
|
|
|
- * 图片处理类
|
|
|
- *
|
|
|
- * @author Henry Hall
|
|
|
- * @since 2020-08-08
|
|
|
- */
|
|
|
-public class ImgUtil {
|
|
|
-
|
|
|
- /**
|
|
|
- * comImg 等比例高清压缩图片
|
|
|
- *
|
|
|
- * @param srcPath 原图片路径
|
|
|
- * @param comW 压缩宽度, 如果为0,则按300像素宽
|
|
|
- * @param comH 压缩高度, 如果为0,则按300像素高
|
|
|
- */
|
|
|
- public static void comImg(String srcPath, double comW, double comH) {
|
|
|
- String destPath = srcPath.replace("srcImgs", "dstImgs"), foldPath = destPath.substring(0, destPath.lastIndexOf("/") + 1);
|
|
|
- comW = (comW == 0 ? 200 : comW);
|
|
|
- comH = (comH == 0 ? 150 : comH);
|
|
|
- OutputStream fos;
|
|
|
- try {
|
|
|
- File srcFile = new File(srcPath), foldFile = new File(foldPath);
|
|
|
- if(!foldFile.exists()) {
|
|
|
- foldFile.mkdirs();
|
|
|
- }
|
|
|
- BufferedImage scrBI = ImageIO.read(srcFile);
|
|
|
- int srcW = scrBI.getWidth(), srcH = scrBI.getHeight(), destW, destH;
|
|
|
- fos = new FileOutputStream(destPath);
|
|
|
- if(comW < srcW || comH < srcH) {
|
|
|
- double ratio, ratioW, ratioH;
|
|
|
- ratioW = comW / srcW;
|
|
|
- ratioH = comH / srcH;
|
|
|
- ratio = Math.min(ratioW, ratioH);
|
|
|
- destW = (int) (srcW * ratio);
|
|
|
- destH = (int) (srcH * ratio);
|
|
|
- BufferedImage bi = new BufferedImage(destW, destH, 1);
|
|
|
- bi.getGraphics().drawImage(scrBI, 0, 0, destW, destH, null);
|
|
|
- JPEGImageEncoder jpegIe = JPEGCodec.createJPEGEncoder(fos);
|
|
|
- jpegIe.encode(bi);
|
|
|
- } else {
|
|
|
- InputStream fis = new FileInputStream(srcFile);
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
- int len;
|
|
|
- while((len = fis.read(buffer)) > 0) {
|
|
|
- fos.write(buffer, 0, len);
|
|
|
- }
|
|
|
- fis.close();
|
|
|
- }
|
|
|
- fos.close();
|
|
|
- } catch(IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * cutImg 裁剪图片
|
|
|
- *
|
|
|
- * @param srcPath 原图片路径
|
|
|
- * @param cutX 裁剪的X坐标
|
|
|
- * @param cutY 裁剪的Y坐标
|
|
|
- * @param cutW 压缩宽度, 如果为0,则按300像素宽
|
|
|
- * @param cutH 压缩高度, 如果为0,则按300像素高
|
|
|
- */
|
|
|
- public static void cutImg(String srcPath, int cutX, int cutY, int cutW, int cutH) {
|
|
|
- String dstPath = srcPath.replace("srcImg", "dstImg");
|
|
|
- String foldPath = dstPath.substring(0, dstPath.lastIndexOf("/") + 1);
|
|
|
- OutputStream fos;
|
|
|
- try {
|
|
|
- File foldFile = new File(foldPath);
|
|
|
- if(!foldFile.exists()) {
|
|
|
- foldFile.mkdirs();
|
|
|
- }
|
|
|
- fos = new FileOutputStream(dstPath);
|
|
|
- BufferedImage srcBI = ImageIO.read(new File(srcPath));
|
|
|
- BufferedImage midBI = srcBI.getSubimage(cutX, cutY, cutW, cutH);
|
|
|
- BufferedImage dstBI = new BufferedImage(300, 300, 1);
|
|
|
- dstBI.getGraphics().drawImage(midBI, 0, 0, 300, 300, null);
|
|
|
- JPEGImageEncoder jpegIe = JPEGCodec.createJPEGEncoder(fos);
|
|
|
- jpegIe.encode(dstBI);
|
|
|
- fos.close();
|
|
|
- } catch(IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * wmImg 图片加水印
|
|
|
- *
|
|
|
- * @param srcPath 原图片地址
|
|
|
- * @param wmPath 水印图片地址
|
|
|
- */
|
|
|
- public static void wmImg(String srcPath, String wmPath) {
|
|
|
- float alphaVal = 0.6f;
|
|
|
- FileOutputStream fos = null;
|
|
|
- Image wmImg, srcImg;
|
|
|
- ImageIcon srcImgIco;
|
|
|
- BufferedImage bi;
|
|
|
- Graphics2D gd;
|
|
|
- try {
|
|
|
- srcImgIco = new ImageIcon(wmPath);
|
|
|
- srcImg = srcImgIco.getImage();
|
|
|
- wmImg = ImageIO.read(new File(srcPath));
|
|
|
- bi = new BufferedImage(wmImg.getWidth(null), wmImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
|
|
|
- gd = bi.createGraphics();
|
|
|
- gd.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
|
- // 水印图片水平、垂直居中显示
|
|
|
- // gd.drawImage(srcImg, wmImg.getWidth(null) / 2 - srcImg.getWidth(null) / 2, wmImg.getHeight(null) / 2 - srcImg.getHeight(null) / 2, null);
|
|
|
- // 水印图片在右下角显示
|
|
|
- gd.drawImage(wmImg.getScaledInstance(wmImg.getWidth(null), wmImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
|
|
|
- gd.rotate(Math.toRadians(0), (double) bi.getWidth() / 2, (double) bi.getHeight() / 2);
|
|
|
- gd.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alphaVal));
|
|
|
- gd.drawImage(srcImg, wmImg.getWidth(null) - srcImg.getWidth(null), wmImg.getHeight(null) - srcImg.getHeight(null), null);
|
|
|
- gd.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
|
|
|
- gd.dispose();
|
|
|
- fos = new FileOutputStream(srcPath);
|
|
|
- ImageIO.write(bi, srcPath.substring(srcPath.lastIndexOf(".") + 1), fos);
|
|
|
- fos.close();
|
|
|
- } catch(IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * base64Upload 64位转码上传图片
|
|
|
- *
|
|
|
- * @param base64 64位加密
|
|
|
- */
|
|
|
- public void base64Upload(String base64) {
|
|
|
- String[] basa = base64.split("base64,");
|
|
|
- // 通过base64来转化图片
|
|
|
- Decoder decoder = Base64.getDecoder();
|
|
|
- // Base64解码
|
|
|
- byte[] imageByte;
|
|
|
- // 生成文件名
|
|
|
- // String files = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) +
|
|
|
- // (new Random().nextInt(9000) % (9000 - 1000 + 1) + 1000) + ".png";
|
|
|
- // 生成文件路径
|
|
|
- String path = "";
|
|
|
- try {
|
|
|
- imageByte = decoder.decode(basa[1]);
|
|
|
- for(int i = 0; i < imageByte.length; ++i) {
|
|
|
- if(imageByte[i] < 0) {// 调整异常数据
|
|
|
- imageByte[i] += 256;
|
|
|
- }
|
|
|
- }
|
|
|
- // 生成文件
|
|
|
- File imageFile = new File(path);
|
|
|
- imageFile.createNewFile();
|
|
|
- if(!imageFile.exists()) {
|
|
|
- imageFile.createNewFile();
|
|
|
- }
|
|
|
- OutputStream imageStream = new FileOutputStream(imageFile);
|
|
|
- imageStream.write(imageByte);
|
|
|
- imageStream.flush();
|
|
|
- imageStream.close();
|
|
|
- } catch(Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-}
|