一: 前端传图片文件
@PostMapping("/check_is_opaque")
public Result checkImgIsOpaque(@RequestParam(value = "file") MultipartFile file) {
BufferedImage bufferedImage = null;
try {
bufferedImage = Thumbnails.of(file.getInputStream()).size(600, 600).outputQuality(0.8).asBufferedImage();
} catch (IOException e) {
e.printStackTrace();
}
return ResultUtil.success(ImageUtil.isTransparent(bufferedImage));
}
二:引用图片路径
@PostMapping("/check_is_opaque")
public Result checkImgIsOpaque(@RequestParam("url") String url) {
boolean isTransparent = false;
try {
isTransparent = ImageUtil.isTransparent(Thumbnails.of(url).asBufferedImage());
} catch (IOException e) {
log.error("检测图片是否透明出错 了", e);
}
return ResultUtil.success(!isTransparent);
}
ImgUtil.java
public class ImageUtil {
public static boolean isTransparent(BufferedImage bufImg) {
int height = bufImg.getHeight();
int width = bufImg.getWidth();
boolean isTransparent = false;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int pixel = bufImg.getRGB(i, j);
if (pixel >> 24 == 0) {
isTransparent = true;
break;
}
}
}
return isTransparent;
}
}