11package com .lzq .web .utils ;
22
33
4+ import com .luciad .imageio .webp .WebPWriteParam ;
45import com .lzq .api .pojo .Content ;
56import com .lzq .api .pojo .Example ;
67import com .lzq .api .service .ContentService ;
78import com .lzq .api .service .ExampleService ;
89import com .qiniu .util .StringUtils ;
10+ import lombok .extern .slf4j .Slf4j ;
911import org .openqa .selenium .Dimension ;
1012import org .openqa .selenium .OutputType ;
1113import org .openqa .selenium .TakesScreenshot ;
1416import org .springframework .beans .factory .annotation .Value ;
1517import org .springframework .stereotype .Component ;
1618
19+ import javax .imageio .IIOImage ;
20+ import javax .imageio .ImageIO ;
21+ import javax .imageio .ImageWriter ;
22+ import javax .imageio .stream .FileImageOutputStream ;
23+ import java .awt .image .BufferedImage ;
1724import java .io .File ;
1825import java .io .FileNotFoundException ;
1926import java .io .FileOutputStream ;
2229import java .util .UUID ;
2330import java .util .concurrent .TimeUnit ;
2431
32+ @ Slf4j
2533@ Component
2634public class ExampleUtils {
2735
@@ -33,8 +41,6 @@ public class ExampleUtils {
3341
3442 public static String BUCKET ;
3543
36- public static String URL ;
37-
3844 private static final char [] _UU64 = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" .toCharArray ();
3945
4046 @ Value ("${resources.InitHtml}" )
@@ -57,21 +63,16 @@ public void setBucket(String bucket) {
5763 ExampleUtils .BUCKET = bucket ;
5864 }
5965
60- @ Value ("${qiniuyun.url}" )
61- public void setUrl (String url ) {
62- ExampleUtils .URL = url ;
63- }
6466
6567 /**
6668 * 截图
6769 *
6870 * @param username 用户名
6971 * @param filename 文件名
70- * @param imgname
7172 * @return
7273 * @throws IOException
7374 */
74- public static String screenshot (String username , String filename , String imgname ) throws IOException {
75+ public static String screenshot (String username , String filename ) throws IOException {
7576 //使用截屏工具进行截屏
7677 //启用chrome驱动
7778 //chrome驱动的位置
@@ -90,20 +91,29 @@ public static String screenshot(String username, String filename, String imgname
9091 broswer .manage ().timeouts ().implicitlyWait (1 , TimeUnit .SECONDS );
9192 //打开url
9293 broswer .get ("http://localhost:8090/" + username + "/" + filename + ".html" );
93-
94- byte [] bytes = ((TakesScreenshot ) broswer ).getScreenshotAs (OutputType .BYTES );
95- //返回图片名
96- String fileName = QiniuyunUtils .uploadFiles (bytes , imgname );
94+ //截图
95+ File screenshotAs = ((TakesScreenshot ) broswer ).getScreenshotAs (OutputType .FILE );
96+ //生成的webp文件
97+ File file = new File (FILE_LOCATION + "/" + username + "/" + username + ".webp" );
98+ convertWebp (screenshotAs , file );
99+ //把生成的webp文件转换位byte数组
100+ //上传到七牛云
101+ String imgName = QiniuyunUtils .uploadFile (file );
97102 broswer .close ();
98- return fileName ;
103+ //删除截图原始图片缓存
104+ screenshotAs .delete ();
105+ boolean delete = file .delete ();
106+ log .info (Boolean .toString (delete ));
107+ return imgName ;
99108
100109 }
101110
102111 /**
103112 * 保存实例
113+ *
104114 * @param example
105115 * @param exampleContent
106- * @param content 编译后的内容
116+ * @param content 编译后的内容
107117 * @param exampleService
108118 * @param contentService
109119 * @return
@@ -114,7 +124,7 @@ public static Boolean SaveExampleContent(Example example, Content exampleContent
114124 //实例内容和实例进行绑定
115125 exampleContent .setExampleId (example .getExampleId ());
116126 String file = FILE_LOCATION + example .getUsername () + "/" + example .getFileName () + ".html" ;
117- System . out . println (file );
127+ log . info (file );
118128 FileOutputStream fos = new FileOutputStream (new File (file ));
119129 String screenshot = null ;
120130 Boolean bol = false ;
@@ -123,23 +133,22 @@ public static Boolean SaveExampleContent(Example example, Content exampleContent
123133 fos .write (content .getBytes ("GBK" ));
124134 //第一次保存时生成图片
125135 if (StringUtils .isNullOrEmpty (example .getImg ())) {
126- //把当前时间戳设置为图片名称
127- example .setImg (Long .toString (System .currentTimeMillis ()));
128136 //截图后进行保存
129- screenshot = ExampleUtils .screenshot (example .getUsername (), example .getFileName (), example .getImg ());
137+ screenshot = ExampleUtils .screenshot (example .getUsername (), example .getFileName ());
138+ //把当前时间戳设置为图片名称
139+ example .setImg (screenshot );
130140 } else {
131141 //先删除图片后上传新图片
132142 QiniuyunUtils .deleteFiles (example .getImg ());
133- example .setImg (Long .toString (System .currentTimeMillis ()));
134143 //截图后进行保存
135- screenshot = ExampleUtils .screenshot (example .getUsername (), example .getFileName (), example .getImg ());
144+ screenshot = ExampleUtils .screenshot (example .getUsername (), example .getFileName ());
145+ example .setImg (screenshot );
136146 }
137- //修改图片地址
138- example .setImg (URL + screenshot );
139147 //更新实例
140148 bol = exampleService .update (example );
141- //修改实例内容 当表无该数据时插入数据
149+ //修改实例内容
142150 bol = contentService .updateContent (exampleContent );
151+ //当表无该数据时插入数据
143152 if (!bol ) {
144153 //第一次保存时在表中添加实例内容
145154 bol = contentService .addContent (exampleContent );
@@ -153,8 +162,37 @@ public static Boolean SaveExampleContent(Example example, Content exampleContent
153162 }
154163 }
155164
165+
166+ /**
167+ * 转换webp
168+ *
169+ * @param oldfile 要转换的文件
170+ * @param newfile 生成的webp文件
171+ * @return
172+ * @throws IOException
173+ */
174+ public static void convertWebp (File oldfile , File newfile ) throws IOException {
175+ // Obtain an image to encode from somewhere
176+ BufferedImage image = ImageIO .read (oldfile );
177+
178+ // Obtain a WebP ImageWriter instance
179+ ImageWriter writer = ImageIO .getImageWritersByMIMEType ("image/webp" ).next ();
180+
181+ // Configure encoding parameters
182+ WebPWriteParam writeParam = new WebPWriteParam (writer .getLocale ());
183+ writeParam .setCompressionMode (WebPWriteParam .MODE_DEFAULT );
184+
185+ // Configure the output on the ImageWriter
186+ FileImageOutputStream fileImageOutputStream = new FileImageOutputStream (newfile );
187+ writer .setOutput (fileImageOutputStream );
188+ // Encode
189+ writer .write (null , new IIOImage (image , null , null ), writeParam );
190+ fileImageOutputStream .close ();
191+ }
192+
193+
156194 //生成22为uuid
157- public static String getUUid (){
195+ public static String getUUid () {
158196 UUID uuid = UUID .randomUUID ();
159197 int index = 0 ;
160198 char [] cs = new char [22 ];
0 commit comments