当前位置: 首页 > article >正文

微信登录模块封装

文章目录

    • 1.资质申请
    • 2.combinations-wx-login-starter
        • 1.目录结构
        • 2.pom.xml 引入okhttp依赖
        • 3.WxLoginProperties.java 属性配置
        • 4.WxLoginUtil.java 后端通过 code 获取 access_token的工具类
        • 5.WxLoginAutoConfiguration.java 自动配置类
        • 6.spring.factories 激活自动配置类
    • 3.combinations-wx-starter-demo
        • 1.目录结构
        • 2.pom.xml 引入依赖
        • 3.application.yml 配置AppID和AppSecret
        • 4.application-prod.yml 配置生产环境的日志和.env文件路径
        • 5.CodeAndState.java 接受code和state的bean
        • 6.WxLoginController.java 微信登录Controller
        • 7.WxApplication.java 启动类
    • 4.微信登录流程梳理
        • 1.用户点击微信登录按钮
        • 2.前端向开放平台发送请求主要携带appId和redirectUri
        • 3.此时开放平台会弹出一个扫码的页面,用户扫码确认
        • 4.用户确认成功后,开放平台会将code和state作为参数去请求redirectUri(前端页面)
        • 5.前端页面获取code和state,再向后端发送请求
        • 6.后端使用code进行微信登录,可以获取到AccessTokenResponse

1.资质申请

  1. 主体为企业的域名和备案的服务器
  2. 主体为企业的微信开放平台的开发者资质认证
  3. 微信开放平台创建应用获取AppID和AppSecret

2.combinations-wx-login-starter

1.目录结构

CleanShot 2025-01-23 at 21.17.45@2x

2.pom.xml 引入okhttp依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.sunxiansheng</groupId><artifactId>sunrays-combinations</artifactId><version>1.0.0</version></parent><artifactId>combinations-wx-login-starter</artifactId><!-- 项目名 --><name>${project.groupId}:${project.artifactId}</name><!-- 简单描述 --><description>微信登录模块封装</description><dependencies><!-- okhttp --><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId></dependency></dependencies>
</project>
3.WxLoginProperties.java 属性配置
package cn.sunxiansheng.wx.login.config.properties;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;/*** Description: 微信登录的属性配置** @Author sun* @Create 2025/1/23 18:49* @Version 1.0*/
@ConfigurationProperties(prefix = "sun-rays.wx.login")
@Data
public class WxLoginProperties {/*** 微信开放平台应用的AppID*/private String appId;/*** 微信开放平台应用的AppSecret*/private String appSecret;/*** 微信开放平台的access_token_url前缀,有默认值,可以不填,为了防止变化!*/private String accessTokenUrlPrefix = "https://api.weixin.qq.com/sns/oauth2/access_token";
}
4.WxLoginUtil.java 后端通过 code 获取 access_token的工具类
package cn.sunxiansheng.wx.login.utils;import cn.sunxiansheng.wx.login.config.properties.WxLoginProperties;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;import javax.annotation.Resource;/*** Description: 微信登录工具类** @Author sun* @Create 2025/1/23 19:01* @Version 1.0*/
@Slf4j
public class WxLoginUtil {/*** 获取微信登录的配置*/@Resourceprivate WxLoginProperties wxLoginProperties;/*** 微信登录的响应类*/@Datapublic static class AccessTokenResponse {@SerializedName("access_token")private String accessToken;@SerializedName("expires_in")private Integer expiresIn;@SerializedName("refresh_token")private String refreshToken;@SerializedName("openid")private String openId;@SerializedName("scope")private String scope;@SerializedName("unionid")private String unionId;}/*** 根据code来完成微信登录** @param code 微信开放平台返回的code* @return 返回AccessTokenResponse*/public AccessTokenResponse wxLogin(String code) {return getAccessToken(wxLoginProperties.getAppId(), wxLoginProperties.getAppSecret(), code);}/*** 后端通过 code 获取 access_token** @param appid  微信应用的 appid* @param secret 微信应用的 secret* @param code   后端已经获得的 code 参数* @return 返回封装的 AccessTokenResponse 对象*/private AccessTokenResponse getAccessToken(String appid, String secret, String code) {// 构造请求 URLString url = String.format("%s?appid=%s&secret=%s&code=%s&grant_type=authorization_code",wxLoginProperties.getAccessTokenUrlPrefix(), appid, secret, code);// 创建 OkHttpClient 实例OkHttpClient client = new OkHttpClient();// 创建 Request 对象Request request = new Request.Builder().url(url).build();// 执行请求并处理响应try (Response response = client.newCall(request).execute()) {// 检查请求是否成功if (!response.isSuccessful()) {String responseBody = response.body() != null ? response.body().string() : "响应体为空";log.error("后端通过 code 获取 access_token 的请求失败,响应码:{}, 响应体:{}", response.code(), responseBody);return null;}// 打印成功的响应String jsonResponse = response.body() != null ? response.body().string() : "响应体为空";log.info("成功获取 access_token,响应:{}", jsonResponse);// 使用 Gson 解析 JSON 数据并封装成 AccessTokenResponse 对象Gson gson = new Gson();// 返回封装的对象return gson.fromJson(jsonResponse, AccessTokenResponse.class);} catch (Exception e) {log.error(e.getMessage(), e);// 返回 null 或者其他错误处理return null;}}
}
5.WxLoginAutoConfiguration.java 自动配置类
package cn.sunxiansheng.wx.login.config;import cn.sunxiansheng.wx.login.config.properties.WxLoginProperties;
import cn.sunxiansheng.wx.login.utils.WxLoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct;/*** Description: 微信登录自动配置类** @Author sun* @Create 2025/1/13 16:11* @Version 1.0*/
@Configuration
@EnableConfigurationProperties({WxLoginProperties.class})
@Slf4j
public class WxLoginAutoConfiguration {/*** 自动配置成功日志*/@PostConstructpublic void logConfigSuccess() {log.info("WxLoginAutoConfiguration has been loaded successfully!");}/*** 注入WxLoginUtil** @return*/@Bean@ConditionalOnMissingBeanWxLoginUtil wxLoginUtil() {return new WxLoginUtil();}
}
6.spring.factories 激活自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.sunxiansheng.wx.login.config.WxLoginAutoConfiguration

3.combinations-wx-starter-demo

1.目录结构

CleanShot 2025-01-23 at 21.25.48@2x

2.pom.xml 引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.sunxiansheng</groupId><artifactId>sunrays-combinations-demo</artifactId><version>1.0.0</version></parent><artifactId>combinations-wx-starter-demo</artifactId><dependencies><!-- combinations-wx-login-starter --><dependency><groupId>cn.sunxiansheng</groupId><artifactId>combinations-wx-login-starter</artifactId><version>1.0.0</version></dependency><!-- common-web-starter --><dependency><groupId>cn.sunxiansheng</groupId><artifactId>common-web-starter</artifactId><version>1.0.0</version></dependency></dependencies><!-- maven 打包常规配置 --><build><!-- 打包成 jar 包时的名字为项目的artifactId + version --><finalName>${project.artifactId}-${project.version}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><!-- 引用父模块中统一管理的插件版本(与SpringBoot的版本一致! --><executions><execution><goals><!-- 将所有的依赖包都打到这个模块中 --><goal>repackage</goal></goals></execution></executions></plugin></plugins></build>
</project>
3.application.yml 配置AppID和AppSecret
sun-rays:log4j2:home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-combinations-demo/combinations-wx-starter-demo/logs # 日志存储根目录env:path: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-combinations-demo/combinations-wx-starter-demo # .env文件的绝对路径wx:login:app-id: ${WX_LOGIN_APP_ID} # 微信开放平台应用的AppIDapp-secret: ${WX_LOGIN_APP_SECRET} # 微信开放平台应用的AppSecret
spring:profiles:active: prod # 激活的环境
4.application-prod.yml 配置生产环境的日志和.env文件路径
sun-rays:log4j2:home: /www/wwwroot/sunrays-framework/logs # 日志存储根目录env:path: /www/wwwroot/sunrays-framework # .env文件的绝对路径
5.CodeAndState.java 接受code和state的bean
package cn.sunxiansheng.wx.entity;import lombok.Data;/*** Description: 接受code和state的bean** @Author sun* @Create 2025/1/16 19:15* @Version 1.0*/@Data
public class CodeAndState {/*** 微信的code*/private String code;/*** 微信的state*/private String state;
}
6.WxLoginController.java 微信登录Controller
package cn.sunxiansheng.wx.controller;import cn.sunxiansheng.wx.entity.CodeAndState;
import cn.sunxiansheng.wx.login.utils.WxLoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;/*** Description: 微信登录Controller** @Author sun* @Create 2025/1/13 16:26* @Version 1.0*/
@Slf4j
@RestController
@RequestMapping("/wx")
public class WxLoginController {@Resourceprivate WxLoginUtil wxLoginUtil;@RequestMapping("/test")public String test() {return "test";}/*** 微信登录** @param codeAndState 前端传过来的code和state* @return 返回unionId*/@RequestMapping("/login")public String login(@RequestBody CodeAndState codeAndState) {// 使用code来完成微信登录WxLoginUtil.AccessTokenResponse accessTokenResponse = wxLoginUtil.wxLogin(codeAndState.getCode());if (accessTokenResponse == null) {log.error("accessToken is null");return "null";}// 获取unionIdString unionId = accessTokenResponse.getUnionId();if (unionId == null) {log.error("unionId is null");return "null";}// 获取unionIdreturn accessTokenResponse.getUnionId();}
}
7.WxApplication.java 启动类
package cn.sunxiansheng.wx;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Description: 微信启动类** @Author sun* @Create 2025/1/13 17:54* @Version 1.0*/
@SpringBootApplication
public class WxApplication {public static void main(String[] args) {SpringApplication.run(WxApplication.class, args);}
}

4.微信登录流程梳理

1.用户点击微信登录按钮

CleanShot 2025-01-23 at 21.36.20@2x

2.前端向开放平台发送请求主要携带appId和redirectUri
<template><button @click="handleLogin" class="wechat-login-button">微信登录</button>
</template><script>
export default {methods: {handleLogin() {// 从环境变量中获取参数const appId = import.meta.env.VITE_APP_ID; // 从环境变量中读取 appIdconst redirectUri = encodeURIComponent(import.meta.env.VITE_REDIRECT_URI); // 从环境变量中读取 redirectUriconst responseType = 'code';const scope = 'snsapi_login'; // 网页应用固定填写 snsapi_login// 生成一个随机的 state 参数,用于防止 CSRF 攻击const state = Math.random().toString(36).substring(2); // 或者使用更安全的方式生成一个随机字符串// 拼接请求URL,并加入 state 参数const wechatLoginUrl = `https://open.weixin.qq.com/connect/qrconnect?appid=${appId}&redirect_uri=${redirectUri}&response_type=${responseType}&scope=${scope}&state=${state}#wechat_redirect`;// 跳转到微信登录页面window.location.href = wechatLoginUrl;},},
};
</script><style scoped>
.wechat-login-button {background-color: #1aad19;color: white;border: none;border-radius: 5px;padding: 10px 20px;cursor: pointer;transition: background-color 0.3s ease;
}.wechat-login-button:hover {background-color: #128c13;
}
</style>
3.此时开放平台会弹出一个扫码的页面,用户扫码确认

CleanShot 2025-01-23 at 21.36.33@2x

4.用户确认成功后,开放平台会将code和state作为参数去请求redirectUri(前端页面)

CleanShot 2025-01-23 at 21.36.47@2x

5.前端页面获取code和state,再向后端发送请求
<template><div class="login-container"><div class="loading-spinner"></div><p class="loading-text">微信登录中,请稍候...</p></div>
</template><script>
export default {async mounted() {const urlParams = new URLSearchParams(window.location.search);const code = urlParams.get("code");const state = urlParams.get("state");if (!code) {console.error("未获取到微信返回的 code");alert("登录失败,请重试");return;}try {const response = await fetch("/wx/login", {method: "POST",headers: {"Content-Type": "application/json",},body: JSON.stringify({ code, state }),});const result = await response.json();if (result.success) {const unionid = result.data;alert(`登录成功,您的unionid是:${unionid}`);this.$router.push({ path: "/products" });} else {alert("登录失败,请重试");}} catch (error) {console.error("请求失败", error);alert("网络错误,请稍后重试");}},
};
</script><style scoped>
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap");:root {--primary-color: #4facfe;--secondary-color: #00f2fe;--text-color: #333;
}.login-container {display: flex;flex-direction: column;align-items: center;justify-content: center;height: 100vh;background: linear-gradient(120deg, #ffffff, #f0f0f0);font-family: "Poppins", sans-serif;
}.loading-spinner {width: 60px;height: 60px;border: 6px solid #e0e0e0;border-top: 6px solid var(--primary-color);border-radius: 50%;animation: spin 1s linear infinite;
}@keyframes spin {0% {transform: rotate(0deg);}100% {transform: rotate(360deg);}
}.loading-text {margin-top: 20px;font-size: 18px;font-weight: 500;color: var(--text-color);animation: fadeIn 2s ease-in-out infinite alternate;
}@keyframes fadeIn {0% {opacity: 0.6;}100% {opacity: 1;}
}
</style>
6.后端使用code进行微信登录,可以获取到AccessTokenResponse
/*** 微信登录的响应类*/
@Data
public static class AccessTokenResponse {@SerializedName("access_token")private String accessToken;@SerializedName("expires_in")private Integer expiresIn;@SerializedName("refresh_token")private String refreshToken;@SerializedName("openid")private String openId;@SerializedName("scope")private String scope;@SerializedName("unionid")private String unionId;
}

相关文章:

微信登录模块封装

文章目录 1.资质申请2.combinations-wx-login-starter1.目录结构2.pom.xml 引入okhttp依赖3.WxLoginProperties.java 属性配置4.WxLoginUtil.java 后端通过 code 获取 access_token的工具类5.WxLoginAutoConfiguration.java 自动配置类6.spring.factories 激活自动配置类 3.com…...

SRS代码目录

代码目录&#xff1a; src/目录下核心代码&#xff1a; core&#xff1a;核心功能模块&#xff0c;包括日志、配置、错误处理等&#xff1b;protocol&#xff1a;实现RTMP、HTTP-FLV、HLS等协议的模块&#xff1b;app&#xff1a;应用层的实现&#xff0c;包括流的发布、播放…...

C++STL(一)——string类

目录 一、string的定义方式二、 string类对象的容量操作三、string类对象的访问及遍历操作四、string类对象的修改操作五、string类非成员函数 一、string的定义方式 string是个管理字符数组的类&#xff0c;其实就是字符数组的顺序表。 它的接口也是非常多的。本章介绍一些常…...

机器学习--1.KNN机器学习入门

1、机器学习概述 1.1、什么是机器学习 机器学习&#xff08;Machine Learning&#xff09;是人工智能&#xff08;Artificial Intelligence&#xff09;领域的一个子集&#xff0c;它主要关注如何让计算机系统通过经验学习&#xff08;数据&#xff09;并自动改进性能。机器学…...

Adaptive LLM Transformer²

看到了一个不错的论文https://arxiv.org/pdf/2501.06252 TRANSFORMER-SQUARED: SELF-ADAPTIVE LLMS 挺有意思的&#xff0c;是一家日本AI公司SakanaAI的论文&#xff08;我以前写过他们的不训练提升模型的能力的文章&#xff0c;感兴趣可以去翻&#xff09;它家有Lion Jones坐镇…...

三路排序算法

三路排序算法 引言 排序算法是计算机科学中基础且重要的算法之一。在数据分析和处理中&#xff0c;排序算法的效率直接影响着程序的执行速度和系统的稳定性。本文将深入探讨三路排序算法&#xff0c;包括其原理、实现和应用场景。 一、三路排序算法的原理 三路排序算法是一…...

代码随想录day27

669. /** lc appleetcode.cn id669 langcpp** [669] 修剪二叉搜索树*/// lc codestart /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}…...

基于LabVIEW的Modbus-RTU设备通信失败问题分析与解决

在使用 LabVIEW 通过 Modbus-RTU 协议与工业设备进行通信时&#xff0c;可能遇到无法正常发送或接收指令的问题。常见原因包括协议参数配置错误、硬件连接问题、数据帧格式不正确等。本文以某 RGBW 控制器调光失败为例&#xff0c;提出了一种通用的排查思路&#xff0c;帮助开发…...

1. 【.NET 8 实战--孢子记账--从单体到微服务--转向微服务】--前言

在我们的专栏《单体开发》中&#xff0c;我们实现了一个简单的记账软件的服务端&#xff0c;并且成功上线。随着用户数量的不断增长&#xff0c;问题逐渐开始显现。访问量逐渐增加&#xff0c;服务端的压力也随之加大。随着访问量的攀升&#xff0c;服务端的响应时间变得越来越…...

直方图:摄影中的视觉数据指南

目录 一、直方图基础&#xff1a;揭开它的神秘面纱 二、解读直方图类型&#xff1a;亮度与色彩的密码 &#xff08;一&#xff09;亮度直方图 &#xff08;二&#xff09;RGB 直方图 三、拍摄中巧用直方图&#xff1a;优化曝光与效果 &#xff08;一&#xff09;精准判断曝…...

一份完整系统化提升信息输出密度与逻辑严谨性的训练素材

一、方法 1. 理论基础与核心概念 1.1 信息输出密度 定义 信息输出密度可以理解为单位表达中所包含的有效信息量。它要求在有限的篇幅或时间内传递更多有价值、低冗余的信息。衡量指标 信息熵&#xff08;Shannon Entropy&#xff09;&#xff1a;在信息论中&#xff0c;信息…...

CommonJS 和 ES6module 的区别

动态与静态 CommonJS 与 ES6 Module 最本质的区别在于前者对模块依赖的解决是“动态的”&#xff0c;而后者是“静态的”。在这里“动态”的含义是&#xff0c;模块依赖关系的建立发生在代码运行阶段&#xff1a;而“静态”则表示模块依赖关系的建立发生在代码编译阶段。 看一…...

IM 即时通讯系统-51-MPush开源实时消息推送系统

IM 开源系列 IM 即时通讯系统-41-开源 野火IM 专注于即时通讯实时音视频技术&#xff0c;提供优质可控的IMRTC能力 IM 即时通讯系统-42-基于netty实现的IM服务端,提供客户端jar包,可集成自己的登录系统 IM 即时通讯系统-43-简单的仿QQ聊天安卓APP IM 即时通讯系统-44-仿QQ即…...

前端 | JavaScript中的reduce方法

1. 什么是reduce reduce 方法是 JavaScript 中数组的重要方法之一&#xff0c;用于对数组中的元素进行累积计算。它接收一个回调函数作为参数&#xff0c;并返回一个最终计算结果。reduce 在许多场景下都非常有用&#xff0c;比如求和、数组扁平化、对象计数、数据转换等。 2…...

【Linux】从硬件到软件了解进程

个人主页~ 从硬件到软件了解进程 一、冯诺依曼体系结构二、操作系统三、操作系统进程管理1、概念2、PCB和task_struct3、查看进程4、通过系统调用fork创建进程&#xff08;1&#xff09;简述&#xff08;2&#xff09;系统调用生成子进程的过程〇提出问题①fork函数②父子进程关…...

2024-我的学习成长之路

因为热爱&#xff0c;无畏山海...

机试题——到邻国目标城市的最短距离

题目描述 A国与B国是相邻的两个国家&#xff0c;每个国家都有很多城市。国家内部有很多连接城市的公路&#xff0c;国家之间也有很多跨国公路&#xff0c;连接两个国家的边界城市。两个国家一共有N个城市&#xff0c;编号从1到N&#xff0c;一共有M条公路&#xff0c;包括国内…...

连续预测、

一、连续预测 调用模型遍历需要预测文件夹中的图片&#xff1a; image_ids open(‘VOCdevkit/VOC2007/ImageSets/Main/test.txt’).read().strip().split() for image_id in tqdm(image_ids): # 遍历测试图像 image_path “./VOCdevkit/VOC2007/JPEGImages/” image_id …...

Kamailio 不通过 dmq 实现注册复制功能

春节期间找到一篇文章&#xff0c;需要 fg 才能看到&#xff1a; https://medium.com/tumalevich/kamailio-registration-replication-without-dmq-65e225f9a8a7 kamailio1 192.168.56.115 kamailio2 192.168.56.116 kamailio3 192.168.56.117 route[HANDLE_REPLICATION] {i…...

002 mapper代理开发方式-xml方式

文章目录 代理xml方式UserMapper.javaUser.javadb.propertiesSqlMapConfig.xmlUserMapper.xmlUserMapperTest.javapom.xml 代理 此处使用的是JDK的动态代理方式&#xff0c;延迟加载使用的cglib动态代理方式 代理分为静态代理和动态代理。此处先不说静态代理&#xff0c;因为…...

大模型系列21-AI聊天机器人

聊天机器人 背景机器学习基础监督学习&#xff08;Supervised Learning&#xff09;概念应用场景主要问题 无监督学习&#xff08;Unsupervised Learning&#xff09;概念常见方法应用场景 强化学习&#xff08;Reinforcement Learning&#xff09;概念关键要素应用场景 模型优…...

Apache Iceberg数据湖技术在海量实时数据处理、实时特征工程和模型训练的应用技术方案和具体实施步骤及代码

Apache Iceberg在处理海量实时数据、支持实时特征工程和模型训练方面的强大能力。Iceberg支持实时特征工程和模型训练&#xff0c;特别适用于需要处理海量实时数据的机器学习工作流。 Iceberg作为数据湖&#xff0c;以支持其机器学习平台中的特征存储。Iceberg的分层结构、快照…...

25.2.3 【洛谷】作为栈的复习不错(学习记录)

今天学习的东西不算多&#xff0c;放了一个星期假&#xff0c;感觉不少东西都没那么清楚&#xff0c;得复习一下才行。今天搞个栈题写&#xff0c;把栈复习一下&#xff0c;明天进入正轨&#xff0c;边复习边学习新东西&#xff0c;应该会有二叉树的学习等等... 【洛谷】P1449 …...

Windows 中的 WSL:开启你的 Linux 之旅

今天在安装windows上安装Docker Desktop的时候&#xff0c;遇到了WSL。下面咱们就学习下。 欢迎来到涛涛聊AI 一、什么是 WSL&#xff1f; WSL&#xff0c;全称为 Windows Subsystem for Linux&#xff0c;是微软为 Windows 系统开发的一个兼容层&#xff0c;它允许用户在 Win…...

二维前缀和:高效求解矩阵区域和问题

在处理二维矩阵时&#xff0c;频繁计算某一子矩阵的和是一个常见的操作。传统的做法是直接遍历该子矩阵&#xff0c;时间复杂度较高。当矩阵非常大且有大量的查询时&#xff0c;直接计算将变得低效。为了提高效率&#xff0c;我们可以通过 二维前缀和 技巧在常数时间内解决这个…...

音视频入门基础:RTP专题(5)——FFmpeg源码中,解析SDP的实现

一、引言 FFmpeg源码中通过ff_sdp_parse函数解析SDP。该函数定义在libavformat/rtsp.c中&#xff1a; int ff_sdp_parse(AVFormatContext *s, const char *content) {const char *p;int letter, i;char buf[SDP_MAX_SIZE], *q;SDPParseState sdp_parse_state { { 0 } }, *s1…...

Android开发工作经历整理

一.无人机应用软件开发 集成大疆官网的DJIMobileSDK到AS中编写软件&#xff0c;操控无人机执行多个航点任务。集成OpenCV库进行图像识别&#xff0c;通过获取参数&#xff0c;根据算法执行sdk&#xff0c;使无人机降落到机库&#xff0c;并执行后续的换电操作。待无人机就绪后…...

C++中常用的十大排序方法之4——希尔排序

成长路上不孤单&#x1f60a;&#x1f60a;&#x1f60a;&#x1f60a;&#x1f60a;&#x1f60a; 【&#x1f60a;///计算机爱好者&#x1f60a;///持续分享所学&#x1f60a;///如有需要欢迎收藏转发///&#x1f60a;】 今日分享关于C中常用的排序方法之4——希尔排序的相…...

解决注入线程池的栈溢出问题

文章目录 1.问题产生2.问题解决 1.问题产生 在使用sleuth的时候&#xff0c;需要注入线程池&#xff0c;他才会自动包装&#xff0c;实现traceId的传递&#xff0c;但是突然启动时出现了栈溢出的问题 2.问题解决 根据报错&#xff0c;发现是Gson序列化相关的问题&#xff0c…...

自动驾驶---两轮自行车的自主导航

1 背景 无人驾驶汽车最早出现在DARPA的比赛中&#xff0c;从那个时刻开始&#xff0c;逐渐引起全球学者的注意&#xff0c;于是从上个世纪开始各大高校院所开始了无人汽车的研发。直到这两年&#xff0c;无人驾驶汽车才开始走进寻常百姓家&#xff0c;虽然目前市面上的乘用车还…...