|
|
@@ -0,0 +1,264 @@
|
|
|
+package cn.hnthyy.thmz.Utils;
|
|
|
+
|
|
|
+import com.sun.image.codec.jpeg.JPEGCodec;
|
|
|
+import com.sun.image.codec.jpeg.JPEGEncodeParam;
|
|
|
+import com.sun.image.codec.jpeg.JPEGImageEncoder;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.geom.Area;
|
|
|
+import java.awt.geom.RoundRectangle2D;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 图片处理类
|
|
|
+ *
|
|
|
+ * @author sanshizi
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class ImageUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 针对高度与宽度进行等比缩放
|
|
|
+ *
|
|
|
+ * @param img
|
|
|
+ * @param maxSize 要缩放到的尺寸
|
|
|
+ * @param type 1:高度与宽度的最大值为maxSize进行等比缩放 , 2:高度与宽度的最小值为maxSize进行等比缩放
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static Image getScaledImage(BufferedImage img, int maxSize, int type) {
|
|
|
+ int w0 = img.getWidth();
|
|
|
+ int h0 = img.getHeight();
|
|
|
+ int w = w0;
|
|
|
+ int h = h0;
|
|
|
+ if (type == 1) {
|
|
|
+ w = w0 > h0 ? maxSize : (maxSize * w0 / h0);
|
|
|
+ h = w0 > h0 ? (maxSize * h0 / w0) : maxSize;
|
|
|
+ } else if (type == 2) {
|
|
|
+ w = w0 > h0 ? (maxSize * w0 / h0) : maxSize;
|
|
|
+ h = w0 > h0 ? maxSize : (maxSize * h0 / w0);
|
|
|
+ }
|
|
|
+ Image image = img.getScaledInstance(w, h, Image.SCALE_SMOOTH);
|
|
|
+ BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
|
|
|
+ Graphics2D g = result.createGraphics();
|
|
|
+ g.drawImage(image, 0, 0, null);//在适当的位置画出
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 先按最小宽高为size等比例绽放, 然后图像居中抠出直径为size的圆形图像
|
|
|
+ *
|
|
|
+ * @param img
|
|
|
+ * @param size
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static BufferedImage getRoundedImage(BufferedImage img, int size) {
|
|
|
+ return getRoundedImage(img, size, size / 2, 2);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 先按最小宽高为size等比例绽放, 然后图像居中抠出半径为radius的圆形图像
|
|
|
+ *
|
|
|
+ * @param img
|
|
|
+ * @param size 要缩放到的尺寸
|
|
|
+ * @param radius 圆角半径
|
|
|
+ * @param type 1:高度与宽度的最大值为maxSize进行等比缩放 , 2:高度与宽度的最小值为maxSize进行等比缩放
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static BufferedImage getRoundedImage(BufferedImage img, int size, int radius, int type) {
|
|
|
+
|
|
|
+ BufferedImage result = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
|
|
|
+ Graphics2D g = result.createGraphics();
|
|
|
+
|
|
|
+ //先按最小宽高为size等比例绽放, 然后图像居中抠出直径为size的圆形图像
|
|
|
+ Image fixedImg = getScaledImage(img, size, type);
|
|
|
+ g.drawImage(fixedImg, (size - fixedImg.getWidth(null)) / 2, (size - fixedImg.getHeight(null)) / 2, null);//在适当的位置画出
|
|
|
+
|
|
|
+ //圆角
|
|
|
+ if (radius > 0) {
|
|
|
+ RoundRectangle2D round = new RoundRectangle2D.Double(0, 0, size, size, radius * 2, radius * 2);
|
|
|
+ Area clear = new Area(new Rectangle(0, 0, size, size));
|
|
|
+ clear.subtract(new Area(round));
|
|
|
+ g.setComposite(AlphaComposite.Clear);
|
|
|
+
|
|
|
+ //抗锯齿
|
|
|
+ g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
+ g.fill(clear);
|
|
|
+ g.dispose();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用删除alpha值的方式去掉图像的alpha通道
|
|
|
+ *
|
|
|
+ * @param $image
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ protected static BufferedImage get24BitImage(BufferedImage $image) {
|
|
|
+ int __w = $image.getWidth();
|
|
|
+ int __h = $image.getHeight();
|
|
|
+ int[] __imgARGB = getRGBs($image.getRGB(0, 0, __w, __h, null, 0, __w));
|
|
|
+ BufferedImage __newImg = new BufferedImage(__w, __h, BufferedImage.TYPE_INT_RGB);
|
|
|
+ __newImg.setRGB(0, 0, __w, __h, __imgARGB, 0, __w);
|
|
|
+ return __newImg;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用绘制的方式去掉图像的alpha值
|
|
|
+ *
|
|
|
+ * @param $image
|
|
|
+ * @param $bgColor
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ protected static BufferedImage get24BitImage(BufferedImage $image, Color $bgColor) {
|
|
|
+ int $w = $image.getWidth();
|
|
|
+ int $h = $image.getHeight();
|
|
|
+ BufferedImage img = new BufferedImage($w, $h, BufferedImage.TYPE_INT_RGB);
|
|
|
+ Graphics2D g = img.createGraphics();
|
|
|
+ g.setColor($bgColor);
|
|
|
+ g.fillRect(0, 0, $w, $h);
|
|
|
+ g.drawRenderedImage($image, null);
|
|
|
+ g.dispose();
|
|
|
+ return img;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将32位色彩转换成24位色彩(丢弃Alpha通道)
|
|
|
+ *
|
|
|
+ * @param $argb
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int[] getRGBs(int[] $argb) {
|
|
|
+ int[] __rgbs = new int[$argb.length];
|
|
|
+ for (int i = 0; i < $argb.length; i++) {
|
|
|
+ __rgbs[i] = $argb[i] & 0xFFFFFF;
|
|
|
+ }
|
|
|
+ return __rgbs;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将矩形转换成正方形
|
|
|
+ *
|
|
|
+ * @param img
|
|
|
+ * @param save
|
|
|
+ * @param size
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void toJPG(File img, File save, Integer size) throws IOException {
|
|
|
+ if (size == null) {
|
|
|
+ size = 250;
|
|
|
+ }
|
|
|
+ FileOutputStream out = new FileOutputStream(save);
|
|
|
+ JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
|
|
|
+
|
|
|
+ BufferedImage image = getRoundedImage(ImageIO.read(img), size, 0, 2);//默认无圆角
|
|
|
+
|
|
|
+ //如果图像是透明的,就丢弃Alpha通道
|
|
|
+ if (image.getTransparency() == Transparency.TRANSLUCENT) {
|
|
|
+ image = get24BitImage(image);
|
|
|
+ }
|
|
|
+ JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);//使用jpeg编码器
|
|
|
+ param.setQuality(1, true);//高质量jpg图片输出
|
|
|
+ encoder.encode(image, param);
|
|
|
+
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换成png格式
|
|
|
+ *
|
|
|
+ * @param img
|
|
|
+ * @param save
|
|
|
+ * @param size
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void toPNG(File img, File save, Integer size) throws IOException {
|
|
|
+ if (size == null) {
|
|
|
+ size = 250;
|
|
|
+ }
|
|
|
+ ImageIO.write(getRoundedImage(ImageIO.read(img), size, 0, 2), "PNG", save);//默认无圆角
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换BufferedImage 数据为byte数组
|
|
|
+ *
|
|
|
+ * @param bImage Image对象
|
|
|
+ * @param format image格式字符串.如"gif","png"
|
|
|
+ * @return byte数组
|
|
|
+ */
|
|
|
+ public static byte[] imageToBytes(BufferedImage bImage, String format) {
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
+ try {
|
|
|
+ ImageIO.write(bImage, format, out);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return out.toByteArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换byte数组为Image
|
|
|
+ *
|
|
|
+ * @param bytes
|
|
|
+ * @return Image
|
|
|
+ */
|
|
|
+ public static Image bytesToImage(byte[] bytes) {
|
|
|
+ Image image = Toolkit.getDefaultToolkit().createImage(bytes);
|
|
|
+ try {
|
|
|
+ MediaTracker mt = new MediaTracker(new Label());
|
|
|
+ mt.addImage(image, 0);
|
|
|
+ mt.waitForAll();
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return image;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将矩形转换成正方形 (字节数组)
|
|
|
+ *
|
|
|
+ * @param bytes 源码字节数组
|
|
|
+ * @param formatName 源文件格式
|
|
|
+ * @param size
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static byte[] formatToSquare(byte[] bytes, String formatName, Integer size) throws IOException {
|
|
|
+ ByteArrayOutputStream out = null;
|
|
|
+ if (size == null) {
|
|
|
+ size = 250;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
|
|
+ BufferedImage image = getRoundedImage(ImageIO.read(in), size, 0, 2);//默认无圆角
|
|
|
+ //如果图像是透明的,就丢弃Alpha通道
|
|
|
+ if (image.getTransparency() == Transparency.TRANSLUCENT) {
|
|
|
+ image = get24BitImage(image);
|
|
|
+ }
|
|
|
+
|
|
|
+ out = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(image, formatName, out);
|
|
|
+ return out.toByteArray();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("图片转码失败,错误原因:{}", e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (out != null) {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+// File img = new File("C:\\Users\\Administrator\\Desktop\\back.jpg");
|
|
|
+// File save = new File("C:\\Users\\Administrator\\Desktop\\111111112.jpg");
|
|
|
+// toJPG(img, save, 250);
|
|
|
+ System.out.println("back.jpg".substring("back.jpg".indexOf(".") + 1));
|
|
|
+ }
|
|
|
+}
|