文章目录
1、完善用户信息
用户在首次登录时需要完善个人信息,包括性别、昵称、生日、城市、头像等。其中,头像数据需要做图片上传,这里采用阿里云的OSS服务作为我们的图片服务器,并且对头像要做人脸识别,非人脸照片不得上传。
-
首次登录时(手机号码不存在),需要创建用户存入数据库中
-
客户端检测首次登录需要完善用户信息
-
填写用户基本信息
-
上传用户头像(需要人脸认证)
-
1.1、阿里云OSS
实现图片上传服务,需要有存储的支持,那么我们的解决方案将以下几种:
-
直接将图片保存到服务的硬盘(springmvc将的文件上传)
-
优点:开发便捷,成本低
-
缺点:扩容困难
-
-
使用分布式文件系统进行存储
-
优点:容易实现扩容
-
缺点:开发复杂度稍大(有成熟的产品可以使用,比如:FastDFS)
-
-
使用第三方的存储服务
-
优点:开发简单,拥有强大功能,免维护
-
缺点:付费
-
在本套课程中选用阿里云的OSS服务进行图片存储。
1.1.1、概述
对象存储服务(Object Storage Service,OSS)是一种海量、安全、低成本、高可靠的云存储服务,适合存放任意类型的文件。容量和处理能力弹性扩展,多种存储类型供选择,全面优化存储成本。
地址:对象存储OSS_云存储服务_企业数据管理_存储-阿里云
1.1.2、账号申请
购买服务
使用第三方服务最大的缺点就是需要付费,下面,我们看下如何购买开通服务。
购买下行流量包: (不购买也可以使用,按照流量付费)
说明:OSS的上行流量是免费的,但是下行流量是需要购买的。
创建Bucket
使用OSS,首先需要创建Bucket,Bucket翻译成中文是水桶的意思,把存储的图片资源看做是水,想要盛水必须得有桶,就是这个意思了。
进入控制台,阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台
选择Bucket后,即可看到对应的信息,如:url、消耗流量等 :
文件管理:
查看文件:
1.1.3、抽取模板工具
和发送短信类似,阿里云OSS也是采用自定义工具的形式进行封装
OssProperties
tanhua-autoconfig创建配置类
@Data @ConfigurationProperties(prefix = "tanhua.oss") public class OssProperties { private String accessKey; private String secret; private String bucketName; private String url; //域名 private String endpoint; }
OssTemplate
tanhua-autoconfig创建模板对象
package com.tanhua.autoconfig.template; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.tanhua.autoconfig.properties.OssProperties; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; public class OssTemplate { private OssProperties properties; public OssTemplate(OssProperties properties) { this.properties = properties; } /** * 文件上传 * 1:文件名称 * 2:输入流 */ public String upload(String filename, InputStream is) { //3、拼写图片路径 filename = new SimpleDateFormat("yyyy/MM/dd").format(new Date()) +"/"+ UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf(".")); // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。 String endpoint = properties.getEndpoint(); // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 String accessKeyId = properties.getAccessKey(); String accessKeySecret = properties.getSecret(); // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret); // 填写Byte数组。 // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。 ossClient.putObject(properties.getBucketName(), filename, is); // 关闭OSSClient。 ossClient.shutdown(); String url = properties.getUrl() +"/" + filename; return url; } }
TanhuaAutoConfiguration
TanhuaAutoConfiguration加入配置
@EnableConfigurationProperties({ SmsProperties.class, OssProperties.class }) public class TanhuaAutoConfiguration { @Bean public SmsTemplate smsTemplate(SmsProperties properties) { return new SmsTemplate(properties); } @Bean public OssTemplate ossTemplate(OssProperties properties) { return new OssTemplate(properties); } }
1.1.4、测试
tanhua-app-server加入配置内容,并测试
tanhua: oss: accessKey: LTAI4GKgob9vZ53k2SZdyAC7 secret: LHLBvXmILRoyw0niRSBuXBZewQ30la endpoint: oss-cn-beijing.aliyuncs.com bucketName: tanhua001 url: https://tanhua001.oss-cn-beijing.aliyuncs.com/
编写测试类
@RunWith(SpringRunner.class) @SpringBootTest(classes = AppServerApplication.class) public class OssTest { @Autowired private OssTemplate template; @Test public void testTemplateUpload() throws FileNotFoundException { String path = "C:\\Users\\lemon\\Desktop\\课程资源\\02-完善用户信息\\03-资料\\2.jpg"; FileInputStream inputStream = new FileInputStream(new File(path)); String imageUrl = template.upload(path, inputStream); System.out.println(imageUrl); } }
1.2、百度人脸识别
人脸识别(Face Recognition)基于图像或视频中的人脸检测、分析和比对技术,提供对您已获授权前提下的私有数据的人脸检测与属性分析、人脸对比、人脸搜索、活体检测等能力。灵活应用于金融、泛安防、零售等行业场景,满足身份核验、人脸考勤、闸机通行等业务需求
1.2.1、概述
地址:人脸识别_人脸识别_准确率99.99%_免费试用-百度AI开放平台
1.2.2、账号申请
账号登录注册
百度云AI支持百度账号登录,也可以支持云账号。按需注册即可
创建应用
按需创建应用
1.2.3、抽取模板工具
AipFaceProperties
@Data @ConfigurationProperties("tanhua.aip") public class AipFaceProperties { private String appId; private String apiKey; private String secretKey; @Bean public AipFace aipFace() { AipFace client = new AipFace(appId, apiKey, secretKey); // 可选:设置网络连接参数 client.setConnectionTimeoutInMillis(2000); client.setSocketTimeoutInMillis(60000); return client; } }
AipFaceTemplate
package com.tanhua.autoconfig.template; import com.baidu.aip.face.AipFace; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import java.util.HashMap; public class AipFaceTemplate { @Autowired private AipFace client; /** * 检测图片中是否包含人脸 * true:包含 * false:不包含 */ public boolean detect(String imageUrl) { // 调用接口 String imageType = "URL"; HashMap<String, String> options = new HashMap<String, String>(); options.put("face_field", "age"); options.put("max_face_num", "2"); options.put("face_type", "LIVE"); options.put("liveness_control", "LOW"); // 人脸检测 JSONObject res = client.detect(imageUrl, imageType, options); System.out.println(res.toString(2)); Integer error_code = (Integer) res.get("error_code"); return error_code == 0; } }
1.2.4、测试
tanhua-app-server加入百度AI的配置信息
tanhua: aip: appId: 24021388 apiKey: ZnMTwoETXnu4OPIGwGAO2H4G secretKey: D4jXShyinv5q26bUS78xRKgNLnB9IfZh
编写单元测试类
@RunWith(SpringRunner.class) @SpringBootTest(classes = AppServerApplication.class) public class FaceTest { @Autowired private AipFaceTemplate template; @Test public void detectFace() { String image = "https://tanhua001.oss-cn-beijing.aliyuncs.com/2021/04/19/a3824a45-70e3-4655-8106-a1e1be009a5e.jpg"; boolean detect = template.detect(image); } }