Thumbnailator是一款处理图片的第三方工具。Java本身也有处理图片压缩的方法,但是代码冗长到让人痛不欲生。做下对比,Thumbnailator的处理效果远比Java API的好。不仅可以处理单个图片,还支持对一个目录的所有图片进行批量处理操作。
支持的处理操作:图片缩放,区域裁剪,水印,旋转,保持比例。
<!-- thumbnailator 图片处理 -->
<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
String result = "src/main/resources/images/";
OutputStream os = new FileOutputStream(result + "sijili_out.jpg");
Image image = ImageIO.read(new File(result + "sijili.jpg"));
BufferedImage bufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
bufferedImage.getGraphics().drawImage(image.getScaledInstance(100, 100, image.SCALE_SMOOTH), 0, 0, null);
ImageIO.write(bufferedImage, "jpg",os);
os.close();
String result = "src/main/resources/images/";
// 将 ys.jpg 图片以 200x300 的尺寸保存到 ys_200x300.jpg缩略图中
Thumbnails.of(result + "ys.jpg").size(200, 300).toFile(result + "ys_200x300.jpg");
String result = "src/main/resources/images/";
// 将ys.jpg 以原始图像的0.5倍压缩,保存到ys_0.5f.jpg缩略图中
Thumbnails.of(result + "ys.jpg").scale(0.5f).toFile(result + "ys_0.5f.jpg");
String result = "src/main/resources/images/";
// 将ys.jpg图片顺时针旋转180°,保存到ys_r180.jpg缩略图中
Thumbnails.of(result + "ys.jpg").scale(1.0f).rotate(180).toFile(result + "ys_r180.jpg");
String result = "src/main/resources/images/";
// 将ys.jpg转化为png形式保存到ys_ys.png缩略图中
Thumbnails.of(result + "ys.jpg").scale(1.0f).outputFormat("png").toFile(result + "ys_ys.png");
String result = "src/main/resources/images/";
Thumbnails.of(result + "ys.jpg").scale(1.0f).sourceRegion(Positions.CENTER, 400, 400).toFile(result + "ys_center.png");
Thumbnails.of(result + "ys.jpg").sourceRegion(100, 100, 100, 100).scale(1.0f).toFile(result + "ys_100_4.jpg");
String result = "src/main/resources/images/";
// watermark 参数 1.位置 2.水印图片 3.透明度
Thumbnails.of(result + "ys.jpg").scale(1.0f).watermark(Positions.CENTER, ImageIO.read(new File(result + "warter.jpg")), 0.25f).toFile(result + "ys_warter.jpg");
tring result = "src/main/resources/images/";
BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setColor(Color.LIGHT_GRAY);
g.drawRect(0, 0, 10, 10);
char[] data = "liudTest".toCharArray();
g.drawChars(data, 0, data.length, 5, 32);
// watermark 参数 1.位置 2.水印 3.透明度
Thumbnails.of(result + "ys.jpg").scale(1.0f).watermark(Positions.CENTER, bi, 1.0f).toFile(result + "ys_warter_liud.jpg");
kaptcha 是一个扩展自 simplecaptcha 的验证码库。
可自由配置的选项如:
- 验证码的字体
- 验证码字体的大小
- 验证码字体的字体颜色
- 验证码内容的范围(数字,字母,中文汉字!)
- 验证码图片的大小,边框,边框粗细,边框颜色
- 验证码的干扰线
- 验证码的样式(鱼眼样式、3D、普通模糊、…)
<!-- kaptcha验证码 -->
<!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
<!-- 验证码 -->
<servlet>
<!-- 生成图片的Servlet -->
<servlet-name>Kaptcha</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
<!-- 是否有边框 -->
<init-param>
<param-name>kaptcha.border</param-name>
<param-value>no</param-value>
</init-param>
<!-- 字体颜色 -->
<init-param>
<param-name>kaptcha.textproducer.font.color</param-name>
<param-value>red</param-value>
</init-param>
<!-- 图片宽度 -->
<init-param>
<param-name>kaptcha.image.width</param-name>
<param-value>135</param-value>
</init-param>
<!-- 使用哪些字符生成验证码 -->
<init-param>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>ACDEFGHIJKLMNOPRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789</param-value>
</init-param>
<!-- 图片高度 -->
<init-param>
<param-name>kaptcha.image.height</param-name>
<param-value>50</param-value>
</init-param>
<!-- 字体大小 -->
<init-param>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>43</param-value>
</init-param>
<!-- 干扰线的颜色 -->
<init-param>
<param-name>kaptcha.noise.color</param-name>
<param-value>black</param-value>
</init-param>
<!-- 字符个数 -->
<init-param>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>4</param-value>
</init-param>
<!-- 使用哪些字体 -->
<init-param>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value>Arial</param-value>
</init-param>
</servlet>
<!-- 映射的url -->
<servlet-mapping>
<servlet-name>Kaptcha</servlet-name>
<url-pattern>/Kaptcha</url-pattern>
</servlet-mapping>
public class CodeUtil {
/**
* 判断验证码是否正确
* @param request
* @return boolean
*/
public static boolean checkVerifyCode(HttpServletRequest request) {
String verifyCodeExpected = (String) request.getSession().getAttribute(
com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String verifyCodeActual = HttpServletRequestUtil.getString(request,
"verifyCodeActual");
//equalsIgnoreCase忽略大小写
if (verifyCodeActual == null || !verifyCodeActual.equalsIgnoreCase(verifyCodeExpected)) {
return false;
}
return true;
}
}
MultipartFile是Spring提供的一个接口,用来接收multipart/form-data类型 请求方式中即将上传的文件,为处理或保存文件,MultipartFile和File需要经常进行转换。
/**
* MultipartFile 转 File
* @param file
* @throws Exception
*/
public static void multipartFileToFile( @RequestParam MultipartFile file ) throws Exception {
File toFile = null;
if(file.equals("")||file.getSize()<=0){
file = null;
}else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
}
public static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* File 转 MultipartFile
* @param file
* @throws Exception
*/
public static void fileToMultipartFile( File file ) throws Exception {
FileInputStream fileInput = new FileInputStream(file);
MultipartFile toMultipartFile = new MockMultipartFile("file",file.getName(),"text/plain", IOUtils.toByteArray(fileInput));
toMultipartFile.getInputStream();
}