package com.ciip.cloud.core.market.util;
import sun.misc.BASE64Encoder;
/**
* Title: WaterMarkUtils
* Description: 阿里云Oss水印图片处理
*
* @author jt
* @created 2020/4/9 11:36
*/
public class WaterMarkUtils {
public static String thumbnail = "?x-oss-process=image/resize,p_50";
/**
* 图片水印
*
* @return 返回值
*/
public static String addImageWaterMark(){
//水印图片为当前的Bucket下图片,直接针对图片名称进行base64编码。(水印图片地址,存在OSS中)
String imagePathStr = "ciipResoruce/watermark/wm.png?x-oss-process=image/resize,P_80";
String encodeBase64 = new BASE64Encoder().encode(imagePathStr.getBytes());
String safeBase64Str = encodeBase64.replace('+', '-');
safeBase64Str = safeBase64Str.replace('/', '_');
safeBase64Str = safeBase64Str.replaceAll("=", "");
String waterMark = "?x-oss-process=image/resize,w_4000/watermark,image_"+safeBase64Str+",g_center";
return waterMark;
}
/**
* 文字水印
*
* @return 返回值
*/
public static String addTextWaterMark(String text,String planName,String time,String code){
String textBase64 = new BASE64Encoder().encode(text.getBytes(Charset.forName("utf8")));
String textBase64Str = textBase64.replace('+', '-');
textBase64Str = textBase64Str.replace('/', '_');
textBase64Str = textBase64Str.replaceAll("=", "");
String planNameBase64 = new BASE64Encoder().encode(planName.getBytes(Charset.forName("utf8")));
String planNameBase64Str = planNameBase64.replace('+', '-');
planNameBase64Str = planNameBase64Str.replace('/', '_');
planNameBase64Str = planNameBase64Str.replaceAll("=", "");
String timeBase64 = new BASE64Encoder().encode(time.getBytes(Charset.forName("utf8")));
String timeBase64Str = timeBase64.replace('+', '-');
timeBase64Str = timeBase64Str.replace('/', '_');
timeBase64Str = timeBase64Str.replaceAll("=", "");
String codeBase64 = new BASE64Encoder().encode(code.getBytes(Charset.forName("utf8")));
String codeBase64Str = codeBase64.replace('+', '-');
codeBase64Str = codeBase64Str.replace('/', '_');
codeBase64Str = codeBase64Str.replaceAll("=", "");
String waterMark = "?x-oss-process=image/resize,w_450,h_640/watermark,type_d3F5LW1pY3JvaGVp,size_22,text_"+textBase64Str+",color_333333,t_100,g_north,y_230/watermark,type_d3F5LW1pY3JvaGVp,size_20,text_"+planNameBase64Str+",color_000000,t_100,g_north,y_260/watermark,type_d3F5LW1pY3JvaGVp,size_12,text_"+timeBase64Str+",color_333333,t_100,g_sw,x_48,y_240/watermark,type_d3F5LW1pY3JvaGVp,size_12,text_"+codeBase64Str+",color_333333,t_100,g_sw,x_48,y_220";
return waterMark;
}
}
图片水印使用方法:
CiipFileResources ciipFileResourcesA = JSON.parseObject(ciipCertification.getCertificatesPhotoJsonA(),CiipFileResources.class);
ciipFileResourcesA.setId(null);
ciipFileResourcesA.setCreateDate(new Date());//创建时间
ciipFileResourcesA.setFileResourceType(CiipFileResourceType.REALNAME_CERTIFICATE);//实名认证:证件照片
//缩略图
ciipFileResourcesA.setThumbnail(ciipFileResourcesA.getFileShowUrl() + WaterMarkUtils.thumbnail);
//水印处理过图片
ciipFileResourcesA.setWatermark(ciipFileResourcesA.getFileShowUrl() + WaterMarkUtils.addImageWaterMark());
ciipFileResourcesService.save(ciipFileResourcesA);
文字水印使用方法:
/**
* 证书签名:从计划中获取证书,签名后,存入合作伙伴中
*
*
* @param planId 计划id
* @param recordId 合作伙伴记录id
* @return 返回值
*/
public void certificateSignName(Long planId, Long recordId) {
//根据计划id,获取计划信息
CooperationPlan plan = cooperationPlanRepository.findById(planId).orElse(null);
CooperationRecord record = cooperationRecordRepository.findById(recordId).orElse(null);
if (null != plan && null != record) {
//个人
if (record.getCreateUserRole().equals(CIIPCommonConstant.SELECT_ROLE_TYPE.CIIP_PERSON)) {
//从Redis中获取,个人用户信息
Object userListObj = cacheUtil.getCache(CacheConstant.CIIP_USER_LOGO_LIST_PERSON.CACHE_NAME, CacheConstant.CIIP_USER_LOGO_LIST_PERSON.UUID + CacheConstant.CIIP_USER_LOGO_LIST_PERSON.CACHE_PARAM);
List userLogoVoList = JSON.parseArray(JSON.toJSONString(userListObj), UserLogoVO.class);
userLogoVoList.forEach(item -> {
if (record.getPersonalUser().equals(item.getId())) {
record.setCreateUserName(item.getName());
}
});
}
//企业
if (record.getCreateUserRole().equals(CIIPCommonConstant.SELECT_ROLE_TYPE.CIIP_ENTERPRISE) || record.getCreateUserRole().equals(CIIPCommonConstant.SELECT_ROLE_TYPE.CIIP_AGENCY)) {
//从Redis中获取,个人用户信息
Object userListObj = cacheUtil.getCache(CacheConstant.CIIP_USER_LOGO_LIST_COMPANY.CACHE_NAME, CacheConstant.CIIP_USER_LOGO_LIST_COMPANY.UUID + CacheConstant.CIIP_USER_LOGO_LIST_COMPANY.CACHE_PARAM);
List userLogoVoList = JSON.parseArray(JSON.toJSONString(userListObj), UserLogoVO.class);
userLogoVoList.forEach(item -> {
if (record.getEnterpriseUser().equals(item.getId())) {
record.setCreateUserName(item.getCompanyName());
}
});
}
//签名
if (null != plan.getPlanCertificate()) {
String certificateUrl = plan.getPlanCertificate().getFileShowUrl();
String authorizationTime = "授权期限:" + DateUtil.format(record.getExaminePassTime(), "yyyy年MM月dd日") + "-" + DateUtil.format(record.getExamineFailureTime(), "yyyy年MM月dd日");
List list = new ArrayList<>();
plan.getCooperationRecords().forEach(item -> {
//表示已审核通过
if (item.getExamineStatus().name().equals(CooperationExamineStatus.AUDITED.name())) {
list.add(item);
}
});
String code = "";
if (list.size() > 0) {
if (String.valueOf(list.size()).length() == 2) {
code = "00" + String.valueOf(list.size()+1);
}
if (String.valueOf(list.size()).length() == 1) {
code = "000" + String.valueOf(list.size()+1);
}
if (String.valueOf(list.size()).length() == 3) {
code = "0" + String.valueOf(list.size()+1);
}
if (String.valueOf(list.size()).length() == 4) {
code = String.valueOf(list.size()+1);
}
} else {
code = "0001";
}
//合作伙伴计划名称拼音首字母+审核通过日期(年后两位+月+日)+该计划第几个伙伴(共四位),例:CIIPHZHB2004070020
String certificateCode = "证书编号:" + PinYinUtil.getFirstSpell(plan.getPlanName()).toUpperCase() + DateUtil.format(record.getExaminePassTime(), "yyMMdd") + code;
//添加水印,给证书上签名
record.setPlanCertificateUrl(certificateUrl + WaterMarkUtils.addTextWaterMark(record.getCreateUserName(), "为" + plan.getPlanName(), authorizationTime, certificateCode));
}
cooperationRecordRepository.save(record);
}
}