|
@@ -1,778 +0,0 @@
|
|
|
-package com.management.platform.service.impl;
|
|
|
-
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import com.management.platform.constant.Constant;
|
|
|
-import com.management.platform.constant.Parameter;
|
|
|
-import com.management.platform.entity.PicContentKeywords;
|
|
|
-import com.management.platform.entity.Screenshot;
|
|
|
-import com.management.platform.entity.TimeCalculation;
|
|
|
-import com.management.platform.entity.TimeCalculationShow;
|
|
|
-import com.management.platform.entity.vo.ScreenshotVO;
|
|
|
-import com.management.platform.mapper.*;
|
|
|
-import com.management.platform.service.ScreenshotService;
|
|
|
-import com.management.platform.util.*;
|
|
|
-import org.apache.log4j.Logger;
|
|
|
-import org.springframework.beans.BeanUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
-import java.io.*;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.time.LocalDate;
|
|
|
-import java.time.LocalTime;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
-import java.time.temporal.ChronoUnit;
|
|
|
-import java.util.*;
|
|
|
-import java.util.regex.Pattern;
|
|
|
-
|
|
|
-/**
|
|
|
- * <p>
|
|
|
- * 服务实现类
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @author 吴涛涛
|
|
|
- * @since 2020-01-02
|
|
|
- */
|
|
|
-@Service
|
|
|
-@Transactional
|
|
|
-public class ScreenshotServiceImpl extends ServiceImpl<ScreenshotMapper, Screenshot> implements ScreenshotService {
|
|
|
- public static List<String> nKeyWordsList = new ArrayList<String>();
|
|
|
- public static Logger log = Logger.getLogger(ScreenshotServiceImpl.class);
|
|
|
-
|
|
|
- //检测时间间隔秒数
|
|
|
- private static Integer DETECTION_INTERVAL = 600;
|
|
|
-
|
|
|
- @Value(value = "${upload.path}")
|
|
|
- private String path;
|
|
|
- @Value(value = "${picrecongnize.browser}")
|
|
|
- private String browserFolder;
|
|
|
- @Value(value = "${picrecongnize.develop}")
|
|
|
- private String developFolder;
|
|
|
- @Value(value = "${picrecongnize.im}")
|
|
|
- private String imFolder;
|
|
|
- @Value(value = "${picrecongnize.design}")
|
|
|
- private String designFolder;
|
|
|
-
|
|
|
- @Value("classpath:novel_words.data")
|
|
|
- private org.springframework.core.io.Resource novelWords;
|
|
|
- @Autowired
|
|
|
- private RedisUtil redisUtil;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private ScreenshotMapper screenshotMapper;
|
|
|
- @Resource
|
|
|
- private PicContentKeywordsMapper picContentKeywordsMapper;
|
|
|
- @Resource
|
|
|
- private TimeCalculationMapper timeCalculationMapper;
|
|
|
- @Resource
|
|
|
- private UserMapper userMapper;
|
|
|
- @Resource
|
|
|
- private TimeCalculationShowMapper timeCalculationShowMapper;
|
|
|
-
|
|
|
- //每次获取到截屏后计算并处理
|
|
|
- private void calculateTime(Screenshot screenshot) {
|
|
|
- try {
|
|
|
- //首先拿去处理show表
|
|
|
- calculateShowTime(screenshot);
|
|
|
-
|
|
|
- //如果图片类型为空 则认为是 -1 - 其他
|
|
|
- if (null == screenshot.getPicType()) {
|
|
|
- screenshot.setPicType(-1);
|
|
|
- }
|
|
|
- //默认状态为不连续 如果下面判断是连续才会修改为true
|
|
|
- Boolean isConsecutive = false,
|
|
|
- isSameType = false;
|
|
|
- //获取本人当天结束时间为准的最后一条记录
|
|
|
- TimeCalculation latestRecord = timeCalculationMapper.selectOne(new QueryWrapper<TimeCalculation>()
|
|
|
- .eq("user_id", screenshot.getUid())
|
|
|
- .eq("date", screenshot.getIndate().toLocalDate())
|
|
|
- .orderByDesc("id")
|
|
|
- .last("LIMIT 1"));
|
|
|
- //截图时间
|
|
|
- LocalTime currentTime = screenshot.getIndate().toLocalTime();
|
|
|
- //如果有上一条记录
|
|
|
- if (latestRecord != null) {
|
|
|
- //前后两张是否相同类型
|
|
|
- isSameType = latestRecord.getActionType().equals(screenshot.getPicType());
|
|
|
- //前后两张时间是否连续 这个目前为600秒
|
|
|
- LocalTime estimatedTime = latestRecord.getEndTime();
|
|
|
- Integer durationSecond = ((currentTime.getHour() - estimatedTime.getHour()) * 3600
|
|
|
- + (currentTime.getMinute() - estimatedTime.getMinute()) * 60
|
|
|
- + (currentTime.getSecond() - estimatedTime.getSecond()));
|
|
|
- //断层不大于一定时间的话 用来判断是否连续
|
|
|
- isConsecutive = durationSecond <= DETECTION_INTERVAL;
|
|
|
- }
|
|
|
- if (isConsecutive && isSameType) {
|
|
|
- //如果时间连续 且是同一类型的话 修改上一条记录的最后时间和持续时间
|
|
|
- LocalTime startTime = latestRecord.getStartTime();
|
|
|
- //计算新的间隔
|
|
|
- Integer duration = ((currentTime.getHour() - startTime.getHour()) * 3600
|
|
|
- + (currentTime.getMinute() - startTime.getMinute()) * 60
|
|
|
- + (currentTime.getSecond() - startTime.getSecond()));
|
|
|
- //设置新的结束时间和持续时间 保存记录
|
|
|
- latestRecord.setEndTime(currentTime).setDuration(duration);
|
|
|
- timeCalculationMapper.updateById(latestRecord);
|
|
|
- } else {
|
|
|
- //如果不是连续的话 新增一个记录
|
|
|
- TimeCalculation timeCalculation = new TimeCalculation();
|
|
|
- timeCalculation
|
|
|
- .setUserId(screenshot.getUid())
|
|
|
- //根据截图种类设置行为代号
|
|
|
- .setActionType(screenshot.getPicType())
|
|
|
- .setDate(screenshot.getIndate().toLocalDate())
|
|
|
- //设置开始时间和结束时间都为当前时间
|
|
|
- .setStartTime(currentTime)
|
|
|
- .setEndTime(currentTime)
|
|
|
- //第一次的持续时间默认为最少单位1秒
|
|
|
- .setDuration(1)
|
|
|
- .setPicUrl(screenshot.getPicUrl());
|
|
|
- timeCalculationMapper.insert(timeCalculation);
|
|
|
- if (isConsecutive) {
|
|
|
- //然后如果只是类型不同但是能连上的话
|
|
|
- LocalTime startTime = latestRecord.getStartTime();
|
|
|
- //计算新的间隔
|
|
|
- Integer duration = ((currentTime.getHour() - startTime.getHour()) * 3600
|
|
|
- + (currentTime.getMinute() - startTime.getMinute()) * 60
|
|
|
- + (currentTime.getSecond() - startTime.getSecond()));
|
|
|
- //设置新的结束时间和持续时间 保存记录
|
|
|
- latestRecord.setEndTime(currentTime).setDuration(duration);
|
|
|
- timeCalculationMapper.updateById(latestRecord);
|
|
|
- }
|
|
|
- }
|
|
|
- /*之后可能还需要处理跨越一天的情况*/
|
|
|
- } catch (NullPointerException e) {
|
|
|
- //凡是有空指针说明缺少用户id或者时间数据
|
|
|
- log.info("工作时长统计失败 缺少用户或时间数据");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //每次获取到截屏后计算并处理
|
|
|
- private void calculateShowTime(Screenshot screenshot) {
|
|
|
- try {
|
|
|
- //如果图片类型为空 则认为是 -1 - 其他
|
|
|
- if (null == screenshot.getPicType()) {
|
|
|
- screenshot.setPicType(-1);
|
|
|
- }
|
|
|
- //默认状态为不连续 如果下面判断是连续才会修改为true
|
|
|
- Boolean isConsecutive = false;
|
|
|
- //获取本人当天结束时间为准的最后一条记录
|
|
|
- TimeCalculationShow latestRecord = timeCalculationShowMapper.selectOne(
|
|
|
- new QueryWrapper<TimeCalculationShow>()
|
|
|
- .eq("user_id", screenshot.getUid())
|
|
|
- .eq("date", screenshot.getIndate().toLocalDate())
|
|
|
- .orderByDesc("id")
|
|
|
- .last("LIMIT 1"));
|
|
|
- //截图时间
|
|
|
- LocalTime currentTime = screenshot.getIndate().toLocalTime();
|
|
|
- //如果有上一条记录
|
|
|
- if (latestRecord != null) {
|
|
|
- //前后两张时间是否连续 这个目前为600秒
|
|
|
- LocalTime estimatedTime = latestRecord.getEndTime();
|
|
|
- Integer durationSecond = ((currentTime.getHour() - estimatedTime.getHour()) * 3600
|
|
|
- + (currentTime.getMinute() - estimatedTime.getMinute()) * 60
|
|
|
- + (currentTime.getSecond() - estimatedTime.getSecond()));
|
|
|
- //断层不大于一定时间的话 用来判断是否连续
|
|
|
- isConsecutive = durationSecond <= DETECTION_INTERVAL;
|
|
|
- }
|
|
|
-
|
|
|
- if (isConsecutive) {
|
|
|
- //如果是连续的话 修改上一条记录的最后时间和持续时间
|
|
|
- LocalTime startTime = latestRecord.getStartTime();
|
|
|
- //计算新的间隔
|
|
|
- Integer duration = ((currentTime.getHour() - startTime.getHour()) * 3600
|
|
|
- + (currentTime.getMinute() - startTime.getMinute()) * 60
|
|
|
- + (currentTime.getSecond() - startTime.getSecond()));
|
|
|
- //设置新的结束时间和持续时间 保存记录
|
|
|
- latestRecord.setEndTime(currentTime).setDuration(duration);
|
|
|
- timeCalculationShowMapper.updateById(latestRecord);
|
|
|
- } else {
|
|
|
- //如果不是连续的话 新增一个记录
|
|
|
- TimeCalculationShow timeCalculationShow = new TimeCalculationShow();
|
|
|
- timeCalculationShow
|
|
|
- .setUserId(screenshot.getUid())
|
|
|
- .setDate(screenshot.getIndate().toLocalDate())
|
|
|
- //设置开始时间和结束时间都为当前时间
|
|
|
- .setStartTime(currentTime)
|
|
|
- .setEndTime(currentTime)
|
|
|
- //第一次的持续时间默认为最少单位1秒
|
|
|
- .setDuration(1);
|
|
|
- timeCalculationShowMapper.insert(timeCalculationShow);
|
|
|
- }
|
|
|
- /*之后可能还需要处理跨越一天的情况*/
|
|
|
- } catch (NullPointerException e) {
|
|
|
- //凡是有空指针说明缺少用户id或者时间数据
|
|
|
- log.info("工作时长统计失败 缺少用户或时间数据");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //列表截图
|
|
|
- @Override
|
|
|
- public HttpRespMsg getLatestScreenshotList(String date, HttpServletRequest request) {
|
|
|
- HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
- //获取某天每一个人最后一张截图
|
|
|
- try {
|
|
|
- List<Map<String, Object>> dataMap = screenshotMapper
|
|
|
- .getLatestScreenshotList(userMapper.selectById(request.getHeader("Token")).getCompanyId(), date);
|
|
|
- // LocalDate.now(ZoneOffset.of("+8")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
- for (Map<String, Object> map : dataMap) {
|
|
|
- //对于每一张图 将时间戳转换为时间
|
|
|
- map.put("time", new SimpleDateFormat("HH:mm:ss").format(map.get("indate")));
|
|
|
- map.remove("indate");
|
|
|
- }
|
|
|
- httpRespMsg.data = dataMap;
|
|
|
- } catch (NullPointerException e) {
|
|
|
- httpRespMsg.setError("验证失败");
|
|
|
- return httpRespMsg;
|
|
|
- }
|
|
|
- return httpRespMsg;
|
|
|
- }
|
|
|
-
|
|
|
- //获取个人截图页
|
|
|
- @Override
|
|
|
- public HttpRespMsg getTodayScreenshotList(String userId, String date) {
|
|
|
- HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
- Map<String, Object> resultMap = new HashMap<>();
|
|
|
- List<String> srcList = new ArrayList<>();
|
|
|
- List<Object> dataList = new ArrayList<>();
|
|
|
- List<Screenshot> screenshotList = screenshotMapper.selectList(new QueryWrapper<Screenshot>()
|
|
|
- .eq("date_str", date)
|
|
|
- .eq("uid", userId)
|
|
|
- .orderByDesc("indate"));
|
|
|
- for (Screenshot screenshot : screenshotList) {
|
|
|
- srcList.add(screenshot.getPicUrl());
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
- map.put("time", screenshot.getIndate().toLocalTime());
|
|
|
- map.put("type", screenshot.getPicType() == null ? 0 : screenshot.getPicType());
|
|
|
- dataList.add(map);
|
|
|
- }
|
|
|
- resultMap.put("srcList", srcList);
|
|
|
- resultMap.put("data", dataList);
|
|
|
- httpRespMsg.data = resultMap;
|
|
|
- return httpRespMsg;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public HttpRespMsg getScreenshotDate(String date, HttpServletRequest request) {
|
|
|
- HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
- try {
|
|
|
- List<Integer> userIdList = screenshotMapper.getCompanyUserId(request.getHeader("Token"));
|
|
|
- if (screenshotMapper.selectCount(new QueryWrapper<Screenshot>()
|
|
|
- .eq("date_str", date)
|
|
|
- .in("uid", userIdList)) > 0) {
|
|
|
- //当天有截图
|
|
|
- httpRespMsg.data = date;
|
|
|
- } else {
|
|
|
- Screenshot futureScreenshot = screenshotMapper.selectOne(new QueryWrapper<Screenshot>()
|
|
|
- .gt("date_str", date)
|
|
|
- .in("uid", userIdList)
|
|
|
- .orderByAsc("date_str")
|
|
|
- .last("LIMIT 1"));
|
|
|
- Screenshot historyScreenshot = screenshotMapper.selectOne(new QueryWrapper<Screenshot>()
|
|
|
- .lt("date_str", date)
|
|
|
- .in("uid", userIdList)
|
|
|
- .orderByDesc("date_str")
|
|
|
- .last("LIMIT 1"));
|
|
|
- String futureDateString = futureScreenshot != null ? futureScreenshot.getDateStr() : null;
|
|
|
- String historyDateString = historyScreenshot != null ? historyScreenshot.getDateStr() : null;
|
|
|
- if (futureDateString == null && historyDateString == null) {
|
|
|
- httpRespMsg.data = null;
|
|
|
- } else if (futureDateString == null) {
|
|
|
- httpRespMsg.data = historyDateString;
|
|
|
- } else if (historyDateString == null) {
|
|
|
- httpRespMsg.data = futureDateString;
|
|
|
- } else {
|
|
|
- DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
- LocalDate localDate = LocalDate.parse(date, dateTimeFormatter);
|
|
|
- LocalDate futureDate = LocalDate.parse(futureDateString, dateTimeFormatter);
|
|
|
- LocalDate historyDate = LocalDate.parse(historyDateString, dateTimeFormatter);
|
|
|
- Long future = ChronoUnit.DAYS.between(localDate, futureDate);
|
|
|
- Long history = ChronoUnit.DAYS.between(historyDate, localDate);
|
|
|
- if (future >= history) {
|
|
|
- httpRespMsg.data = historyDateString;
|
|
|
- } else {
|
|
|
- httpRespMsg.data = futureDateString;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (NullPointerException e) {
|
|
|
- httpRespMsg.setError("验证失败");
|
|
|
- return httpRespMsg;
|
|
|
- }
|
|
|
- return httpRespMsg;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public HttpRespMsg saveAndProcessImage(ScreenshotVO screenshotvo) {
|
|
|
- Map<String, Object> fileMap = UploadFileToFileNameUtil.uploadFile(screenshotvo.getFile(), path);
|
|
|
- String filePath = (String) fileMap.get("sqlFilePath");
|
|
|
- Screenshot screenshot = new Screenshot();
|
|
|
- BeanUtils.copyProperties(screenshotvo, screenshot);
|
|
|
- screenshot.setPicUrl(filePath);
|
|
|
- screenshot.setDateStr(DateTimeFormatter.ofPattern("yyyy-MM-dd").format(screenshotvo.getIndate()));
|
|
|
- String accessToken = "";
|
|
|
- if (redisUtil.existsKey(Parameter.ACCESS_TOKEN.getName())) {
|
|
|
- accessToken = redisUtil.getKey(Parameter.ACCESS_TOKEN.getName());
|
|
|
- } else {
|
|
|
- Map<String, Object> map = AuthService.getAuth(Constant.API_KEY, Constant.SECRET_KEY);
|
|
|
- accessToken = (String) map.get(Parameter.ACCESS_TOKEN.getName());
|
|
|
- System.out.println(accessToken);
|
|
|
- redisUtil.setKeyWithExpireTime(Parameter.ACCESS_TOKEN.getName(), accessToken, (Long) map.get(Parameter.EXPIRES_IN.getName()));
|
|
|
- }
|
|
|
- log.info("accessToken-->" + accessToken);
|
|
|
- //利用token去检测
|
|
|
-// DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
-// LocalDateTime l = LocalDateTime.parse("2019-02-03",dateTimeFormatter);
|
|
|
-// Set<Object> members = new HashSet<>();
|
|
|
-// if (redisUtil.existsKey(Constant.COMMON_SOFTWARE_KEYWORDS)) {
|
|
|
-// members = redisUtil.members(Constant.COMMON_SOFTWARE_KEYWORDS);
|
|
|
-// } else {
|
|
|
-// List<PicContentKeywords> picContentKeywords = picContentKeywordsMapper.selectList(null);
|
|
|
-// for (PicContentKeywords keyWord : picContentKeywords) {
|
|
|
-// redisUtil.sSetJsonString("keyWords", keyWord);
|
|
|
-// }
|
|
|
-// //由于存入数据库的对象被序列化成了json字符串,所以从redis里拿方便
|
|
|
-// members = redisUtil.members(Constant.COMMON_SOFTWARE_KEYWORDS);
|
|
|
-// }
|
|
|
- File picFile = new File((String) fileMap.get("newFile"));
|
|
|
- System.out.println("File:" + picFile.getAbsolutePath());
|
|
|
-
|
|
|
- List<String> textContents = new ArrayList<String>();
|
|
|
-
|
|
|
- if (isIM(picFile) != null) {
|
|
|
- screenshot.setPicType(9);//聊天
|
|
|
- } else {
|
|
|
- Map<String, Object> picResultMap = CheckPicUtil.generalPicTextContentMap(path + filePath.substring("/upload/".length()), accessToken);
|
|
|
- if (picResultMap != null) {
|
|
|
- textContents = (List<String>) picResultMap.get("wordsList");
|
|
|
- screenshot.setPicContext((String) picResultMap.get("picContent"));
|
|
|
- }
|
|
|
- if (isNovel(textContents)) {
|
|
|
- screenshot.setPicType(6);
|
|
|
- } else if (isDocument(textContents)) {
|
|
|
- screenshot.setPicType(2);//看文档
|
|
|
- } else if (isDevelop(picFile) != null) {//开发
|
|
|
- screenshot.setPicType(0);
|
|
|
- } else if (isDesign(picFile) != null) {//设计
|
|
|
- screenshot.setPicType(3);
|
|
|
- }
|
|
|
- screenshot.setIsHandle(1);
|
|
|
- if (screenshot.getPicType() == null && isEntertainmentColorMode(picFile)) {
|
|
|
- //判断是否是娱乐:看电影, 打游戏
|
|
|
- String uid = screenshotvo.getUid();
|
|
|
- Screenshot preShot = screenshotMapper.selectOne(new QueryWrapper<Screenshot>().eq("uid", uid).orderByDesc("indate").last("limit 1"));
|
|
|
- if (preShot != null) {
|
|
|
- String prePath = path + preShot.getPicUrl().substring(preShot.getPicUrl().lastIndexOf("/"));
|
|
|
- File f = new File(prePath);
|
|
|
- if (f.exists()) {
|
|
|
- ImageCompare comp = new ImageCompare();
|
|
|
- if (comp.isMoviePlay((String) fileMap.get("newFile"), prePath)) {
|
|
|
- screenshot.setPicType(7);
|
|
|
- //前面那条也更新
|
|
|
- if (preShot.getPicType() != null && preShot.getPicType() != 7) {
|
|
|
- preShot.setPicType(7);
|
|
|
- screenshotMapper.updateById(preShot);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (screenshot.getPicType() == null) {
|
|
|
- try {
|
|
|
- String browserName = isBrowser(picFile);
|
|
|
- if (browserName != null) {
|
|
|
- screenshot.setPicType(1);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //将获取到的截图进行时间计算
|
|
|
- calculateTime(screenshot);
|
|
|
- screenshotMapper.insert(screenshot);
|
|
|
- return new HttpRespMsg();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public HttpRespMsg reTestPicMatch(int id) {
|
|
|
- Screenshot item = screenshotMapper.selectById(id);
|
|
|
- String fileName = item.getPicUrl().replaceAll("/upload/", "");
|
|
|
- String filePath = path + fileName;
|
|
|
- File pic = new File(filePath);
|
|
|
- String imName = isIM(pic);
|
|
|
- HttpRespMsg msg = new HttpRespMsg();
|
|
|
- if (imName != null) {
|
|
|
- msg.data = imName;
|
|
|
- } else {
|
|
|
- String devName = isDevelop(pic);
|
|
|
- if (devName != null) {
|
|
|
- msg.data = devName;
|
|
|
- } else {
|
|
|
- try {
|
|
|
- String browserName = isBrowser(pic);
|
|
|
- if (browserName != null) {
|
|
|
- msg.data = browserName;
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return msg;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public HttpRespMsg updateRedisPicContentKeywords() {
|
|
|
- List<PicContentKeywords> picContentKeywords = picContentKeywordsMapper.selectList(null);
|
|
|
- for (PicContentKeywords keyWord : picContentKeywords) {
|
|
|
- redisUtil.sSetJsonString("keyWords", keyWord);
|
|
|
- }
|
|
|
- HttpRespMsg msg = new HttpRespMsg();
|
|
|
- msg.data = redisUtil.members("keyWords");
|
|
|
- return msg;
|
|
|
- }
|
|
|
-
|
|
|
- //判断文字内容是否是小说
|
|
|
- private boolean isNovel(List<String> textContents) {
|
|
|
- /**先粗糙地比较一下, 小说常规包含的词库,匹配频率高,则认为是小说。
|
|
|
- * 第一步, 90%应该都是中文
|
|
|
- */
|
|
|
- int total = 0;
|
|
|
- int chWNum = 0;
|
|
|
- for (String w : textContents) {
|
|
|
- char[] ch = w.toCharArray();
|
|
|
- for (char c : ch) {
|
|
|
- total++;
|
|
|
- if (c >= 0x4E00 && c <= 0x9FBF) {
|
|
|
- chWNum++;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //小说一页中文字至少200个
|
|
|
- if (chWNum < 200) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- int percent = chWNum * 100 / total;
|
|
|
- log.info("中文比例:" + percent);
|
|
|
- if (percent < 60) {
|
|
|
- //英文太多,不是小说; 不考虑英文小说。
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- //第二步,匹配小说常见词汇,超过5次,认为是小说
|
|
|
- try {
|
|
|
- //填充小说关键字到内存,减少重复读取
|
|
|
- if (nKeyWordsList.size() == 0) {
|
|
|
- InputStream ins = novelWords.getInputStream();
|
|
|
- BufferedReader br = new BufferedReader(new InputStreamReader(ins));
|
|
|
- String line = br.readLine();
|
|
|
- while (line != null) {
|
|
|
- nKeyWordsList.add(line);
|
|
|
- line = br.readLine();
|
|
|
- System.out.println(line);
|
|
|
- }
|
|
|
- }
|
|
|
- int totalKNum = 0;
|
|
|
- for (String k : textContents) {
|
|
|
- int kNum = 0;
|
|
|
- for (String nk : nKeyWordsList) {
|
|
|
- if (k.contains(nk)) {
|
|
|
- kNum++;
|
|
|
- }
|
|
|
- }
|
|
|
- totalKNum += kNum;
|
|
|
- //存在第几章这样的关键字, +1分
|
|
|
- if (matchCategory(k)) {
|
|
|
- totalKNum += 1;
|
|
|
- }
|
|
|
- }
|
|
|
- log.info("文章小说匹配得分为==" + totalKNum);
|
|
|
- if (totalKNum >= 5) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- public static boolean matchCategory(String str) {
|
|
|
- String pattern = "^第(.*)章(.*)";
|
|
|
- // 创建 Pattern 对象
|
|
|
- return Pattern.matches(pattern, str.trim());
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) throws Exception {
|
|
|
-// String b = isDesign(new File("C:\\Users\\seya\\Desktop\\mock.jpg"));
|
|
|
-// System.out.println("结果:"+b);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private static boolean isPureColor(BufferedImage img, int colorPixel) {
|
|
|
- int with = img.getWidth();
|
|
|
- int height = img.getHeight();
|
|
|
- boolean hasDifferent = false;
|
|
|
- for (int i = 0; i < with; i++) {
|
|
|
- for (int y = 0; y < height; y++) {
|
|
|
- int pixel = img.getRGB(i, y); // 下面三行代码将一个数字转换为RGB数字
|
|
|
- if (pixel != colorPixel) {
|
|
|
- hasDifferent = true;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- if (hasDifferent) {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- return !hasDifferent;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 是否全是汉字<br>
|
|
|
- * 根据汉字编码范围进行判断<br>
|
|
|
- * CJK统一汉字(不包含中文的,。《》()“‘'”、!¥等符号)<br>
|
|
|
- *
|
|
|
- * @param str
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean isChineseByReg(String str) {
|
|
|
- if (str == null) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+");
|
|
|
- return pattern.matcher(str).matches();
|
|
|
- }
|
|
|
-
|
|
|
- //判断是否是浏览器
|
|
|
- private String isBrowser(File pic) throws Exception {
|
|
|
-// System.out.println("picrecongnizeFolder=="+browserFolder);
|
|
|
- File folder = new File(browserFolder);
|
|
|
-// File folder = new File("C:/picrecongnize/browser/");
|
|
|
- if (!folder.exists()) {
|
|
|
- throw new Exception("没有设置图片上传的浏览器比对模板库");
|
|
|
- } else {
|
|
|
- File[] files = folder.listFiles();
|
|
|
- String browserName = null;
|
|
|
- for (File subFolder : files) {
|
|
|
- File[] targetFile = subFolder.listFiles();
|
|
|
- boolean isMatch = false;
|
|
|
- for (File targetPic : targetFile) {
|
|
|
- boolean matchPic = ImageReconizeUtil.isTemplateMatch(pic.getAbsolutePath(), targetPic.getAbsolutePath());
|
|
|
- if (matchPic) {
|
|
|
- isMatch = true;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- if (isMatch) {
|
|
|
- browserName = subFolder.getName();
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- return browserName;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //判断是否是开发
|
|
|
- private String isDevelop(File pic) {
|
|
|
- File folder = new File(developFolder);
|
|
|
-// File folder = new File("C:\\picrecongnize\\develop\\");
|
|
|
- if (!folder.exists()) {
|
|
|
- try {
|
|
|
- throw new Exception("没有设置图片上传的开发工具比对模板库");
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- } else {
|
|
|
- File[] files = folder.listFiles();
|
|
|
- String toolName = null;
|
|
|
- boolean isMatch = false;
|
|
|
- for (File targetPic : files) {
|
|
|
-// System.out.println("targetPic==" + targetPic.getAbsolutePath());
|
|
|
- boolean matchPic = ImageReconizeUtil.isTemplateMatch(pic.getAbsolutePath(), targetPic.getAbsolutePath());
|
|
|
- if (matchPic) {
|
|
|
- toolName = targetPic.getName();
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- return toolName;
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- private String isDesign(File pic) {
|
|
|
- File folder = new File(designFolder);
|
|
|
-// File folder = new File("C:\\picrecongnize\\design\\");
|
|
|
- if (!folder.exists()) {
|
|
|
- try {
|
|
|
- throw new Exception("没有设置图片上传的设计工具比对模板库");
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- } else {
|
|
|
- File[] files = folder.listFiles();
|
|
|
- String toolName = null;
|
|
|
- boolean isMatch = false;
|
|
|
- for (File targetPic : files) {
|
|
|
-// System.out.println("targetPic==" + targetPic.getAbsolutePath());
|
|
|
- boolean matchPic = ImageReconizeUtil.isTemplateMatch(pic.getAbsolutePath(), targetPic.getAbsolutePath());
|
|
|
- if (matchPic) {
|
|
|
- toolName = targetPic.getName();
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- return toolName;
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- //判断是否是聊天
|
|
|
- private String isIM(File pic) {
|
|
|
- File folder = new File(imFolder);
|
|
|
-// File folder = new File("C:\\picrecongnize\\im\\");
|
|
|
- if (!folder.exists()) {
|
|
|
- try {
|
|
|
- throw new Exception("没有设置图片上传的聊天比对模板库");
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- } else {
|
|
|
- File[] files = folder.listFiles();
|
|
|
- String toolName = null;
|
|
|
- boolean isMatch = false;
|
|
|
- for (File targetPic : files) {
|
|
|
-// System.out.println("targetPic==" + targetPic.getAbsolutePath());
|
|
|
- boolean matchPic = ImageReconizeUtil.isWholeTemplateMatch(pic.getAbsolutePath(), targetPic.getAbsolutePath());
|
|
|
- if (matchPic) {
|
|
|
- toolName = targetPic.getName();
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- return toolName;
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断是否是看文档, 具体类型为word, excel, pdf, ppt
|
|
|
- *
|
|
|
- * @param textContents
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean isDocument(List<String> textContents) {
|
|
|
- boolean find = false;
|
|
|
- for (int i = 0; i < textContents.size() && i <= 2; i++) {//出现在前三行
|
|
|
- String text = textContents.get(i).trim();
|
|
|
- if (text.contains("Word")
|
|
|
- || text.contains("Excel")
|
|
|
- || text.contains("Ecel")
|
|
|
- || text.contains("Power Point")
|
|
|
- || text.contains("Adobe Reader")) {
|
|
|
- find = true;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- return find;
|
|
|
- }
|
|
|
-
|
|
|
- //娱乐类: 电影+游戏; 画面比较丰富的
|
|
|
- public static boolean isEntertainmentColorMode(File pic) {
|
|
|
- try {
|
|
|
- int[] rgb = new int[3];
|
|
|
- if (!pic.exists()) {
|
|
|
- System.err.println("文件不存在" + pic.getAbsolutePath());
|
|
|
- } else {
|
|
|
- System.out.println("找到文件" + pic.getAbsolutePath());
|
|
|
- }
|
|
|
- BufferedImage img = ImageIO.read(pic);
|
|
|
- int width = img.getWidth();
|
|
|
- int height = img.getHeight();
|
|
|
- int minx = img.getMinX();
|
|
|
- int miny = img.getMinY();
|
|
|
-// System.out.println("width=" + width + ",height=" + height + ".");
|
|
|
-// System.out.println("minx=" + minx + ",miniy=" + miny + ".");
|
|
|
- //统计出现最多的一个色值,计算所占比重
|
|
|
- int totalPixl = 0;
|
|
|
- HashMap<Integer, Integer> colorCntMap = new HashMap<Integer, Integer>();
|
|
|
- for (int i = minx; i < width; i++) {
|
|
|
- for (int j = miny; j < height; j++) {
|
|
|
- totalPixl++;
|
|
|
- int pixel = img.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字
|
|
|
- if (colorCntMap.get(pixel) == null) {
|
|
|
- colorCntMap.put(pixel, 1);
|
|
|
- } else {
|
|
|
- colorCntMap.put(pixel, colorCntMap.get(pixel) + 1);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Map<Integer, Integer> sMap = new TreeMap<Integer, Integer>();
|
|
|
- int maxCnt = 0;
|
|
|
- int key = 0;
|
|
|
- Set<Map.Entry<Integer, Integer>> entry2 = colorCntMap.entrySet();
|
|
|
- for (Map.Entry<Integer, Integer> temp : entry2) {
|
|
|
-// System.out.println("sortedMap:"+temp.getKey()+" 值"+temp.getValue());
|
|
|
- if (temp.getValue() > maxCnt) {
|
|
|
- maxCnt = temp.getValue();
|
|
|
- key = temp.getKey();
|
|
|
- }
|
|
|
- sMap.put(temp.getValue(), temp.getKey());
|
|
|
- }
|
|
|
- sMap = ((TreeMap) sMap).descendingMap();
|
|
|
- Iterator it = sMap.keySet().iterator();
|
|
|
- Integer k1 = (Integer) it.next();
|
|
|
- Integer k2 = (Integer) it.next();
|
|
|
- Integer color1 = sMap.get(k1);
|
|
|
- Integer color2 = sMap.get(k2);
|
|
|
-// System.out.println("kkkk==" + k1 + "," + color1 + "," + k2 + "," + color2);
|
|
|
-// System.out.println("最.." + maxCnt + ", key=" + key);
|
|
|
- rgb[0] = (key & 0xff0000) >> 16;
|
|
|
- rgb[1] = (key & 0xff00) >> 8;
|
|
|
- rgb[2] = (key & 0xff);
|
|
|
-// System.out.println("色值为: " + rgb[0] + ", " + rgb[1] + ", " + rgb[2]);
|
|
|
- //计算比例, 应该不低于50%
|
|
|
- int colorPercent = maxCnt * 100 / totalPixl;
|
|
|
- if (colorPercent < 50) {
|
|
|
- //可能存在2中底色, 大布局的底色和小模块的底色,都算底色。
|
|
|
- int secPercent = k2 * 100 / totalPixl;
|
|
|
- rgb[0] = (color2 & 0xff0000) >> 16;
|
|
|
- rgb[1] = (color2 & 0xff00) >> 8;
|
|
|
- rgb[2] = (color2 & 0xff);
|
|
|
-// System.out.println("二级底色色值为: " + rgb[0] + ", " + rgb[1] + ", " + rgb[2]);
|
|
|
-// System.out.println("二级底色比例==" + secPercent);
|
|
|
- colorPercent = (k1 + k2) * 100 / totalPixl;
|
|
|
- }
|
|
|
- System.out.println("总底色比例==" + colorPercent);
|
|
|
- //计算底色是否是连续分布的
|
|
|
- int windowSize = 50;
|
|
|
- int pureColorBlockCnt = 0;
|
|
|
- int totalBlockCnt = 0;
|
|
|
- for (int i = minx; i < width - windowSize; i += windowSize) {
|
|
|
- for (int j = miny; j < height - windowSize; j += windowSize) {
|
|
|
- totalBlockCnt++;
|
|
|
- BufferedImage rect = img.getSubimage(i, j, windowSize, windowSize);
|
|
|
- if (isPureColor(rect, key)) {
|
|
|
- pureColorBlockCnt++;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-// log.info("pureColorBlockCnt===" + pureColorBlockCnt);
|
|
|
- int pureColorBlackPercent = pureColorBlockCnt * 100 / totalBlockCnt;
|
|
|
-// log.info("pureColorBlackPercent===" + pureColorBlackPercent);
|
|
|
- if (colorPercent < 50 && pureColorBlackPercent < 30) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- // TODO Auto-generated catch block
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-}
|