派生自 projectDept/qhighschool

yn147
2023-05-10 96286178ee1c257c130cb2ad964a781f36c4eee5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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"));
    }
}