打开ar测距:-鸿蒙开发-实战:用AR Engine测量两点之间的距离 - font-size:16px
实战:用 AR Engine 做物体测量
你有没有想过用手机测量桌子有多宽、房间有多长?AR 技术可以做到——打开摄像头,点击两个点,手机就告诉你这两点之间的距离。
物体测量整体流程
下面是用AR Engine测量距离的完整流程:
#mermaid-svg-DuYFJapCZYn8pX1u{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-DuYFJapCZYn8pX1u .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-DuYFJapCZYn8pX1u .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-DuYFJapCZYn8pX1u .error-icon{fill:#552222;}#mermaid-svg-DuYFJapCZYn8pX1u .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-DuYFJapCZYn8pX1u .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-DuYFJapCZYn8pX1u .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-DuYFJapCZYn8pX1u .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-DuYFJapCZYn8pX1u .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-DuYFJapCZYn8pX1u .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-DuYFJapCZYn8pX1u .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-DuYFJapCZYn8pX1u .marker{fill:#333333;stroke:#333333;}#mermaid-svg-DuYFJapCZYn8pX1u .marker.cross{stroke:#333333;}#mermaid-svg-DuYFJapCZYn8pX1u svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-DuYFJapCZYn8pX1u p{margin:0;}#mermaid-svg-DuYFJapCZYn8pX1u .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-DuYFJapCZYn8pX1u .cluster-label text{fill:#333;}#mermaid-svg-DuYFJapCZYn8pX1u .cluster-label span{color:#333;}#mermaid-svg-DuYFJapCZYn8pX1u .cluster-label span p{background-color:transparent;}#mermaid-svg-DuYFJapCZYn8pX1u .label text,#mermaid-svg-DuYFJapCZYn8pX1u span{fill:#333;color:#333;}#mermaid-svg-DuYFJapCZYn8pX1u .node rect,#mermaid-svg-DuYFJapCZYn8pX1u .node circle,#mermaid-svg-DuYFJapCZYn8pX1u .node ellipse,#mermaid-svg-DuYFJapCZYn8pX1u .node polygon,#mermaid-svg-DuYFJapCZYn8pX1u .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-DuYFJapCZYn8pX1u .rough-node .label text,#mermaid-svg-DuYFJapCZYn8pX1u .node .label text,#mermaid-svg-DuYFJapCZYn8pX1u .image-shape .label,#mermaid-svg-DuYFJapCZYn8pX1u .icon-shape .label{text-anchor:middle;}#mermaid-svg-DuYFJapCZYn8pX1u .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-DuYFJapCZYn8pX1u .rough-node .label,#mermaid-svg-DuYFJapCZYn8pX1u .node .label,#mermaid-svg-DuYFJapCZYn8pX1u .image-shape .label,#mermaid-svg-DuYFJapCZYn8pX1u .icon-shape .label{text-align:center;}#mermaid-svg-DuYFJapCZYn8pX1u .node.clickable{cursor:pointer;}#mermaid-svg-DuYFJapCZYn8pX1u .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-DuYFJapCZYn8pX1u .arrowheadPath{fill:#333333;}#mermaid-svg-DuYFJapCZYn8pX1u .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-DuYFJapCZYn8pX1u .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-DuYFJapCZYn8pX1u .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-DuYFJapCZYn8pX1u .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-DuYFJapCZYn8pX1u .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-DuYFJapCZYn8pX1u .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-DuYFJapCZYn8pX1u .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-DuYFJapCZYn8pX1u .cluster text{fill:#333;}#mermaid-svg-DuYFJapCZYn8pX1u .cluster span{color:#333;}#mermaid-svg-DuYFJapCZYn8pX1u div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-DuYFJapCZYn8pX1u .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-DuYFJapCZYn8pX1u rect.text{fill:none;stroke-width:0;}#mermaid-svg-DuYFJapCZYn8pX1u .icon-shape,#mermaid-svg-DuYFJapCZYn8pX1u .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-DuYFJapCZYn8pX1u .icon-shape p,#mermaid-svg-DuYFJapCZYn8pX1u .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-DuYFJapCZYn8pX1u .icon-shape .label rect,#mermaid-svg-DuYFJapCZYn8pX1u .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-DuYFJapCZYn8pX1u .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-DuYFJapCZYn8pX1u .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-DuYFJapCZYn8pX1u :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}用户打开测量工具初始化AR Session启用平面检测移动手机扫描平面检测到平面用户点击第一个点命中检测hitTest获取3D坐标用户点击第二个点获取第二个3D坐标计算两点距离显示测量结果
物体测量的原理
物体测量的核心是平面检测 + 距离计算:
平面检测:AR Engine 检测摄像头画面中的平面(地面、桌面、墙面)点击定位:用户在平面上点击两个点距离计算:根据两个点的 3D 坐标,计算实际距离
第一步:初始化 AR
import { arEngine } from '@kit.ArkGraphics2D';
import { common } from '@kit.AbilityKit';
导入需要的模块。
interface Point3D {
x: number;
y: number;
z: number;
}
@Entry
@Component
struct MeasureTool {
@State status: string = '初始化中...';
@State distance: string = '';
@State points: Point3D[] = [];
private arSession: arEngine.ARSession | null = null;
private planes: arEngine.ARPlane[] = [];
定义 3D 坐标接口和状态变量。
async aboutToAppear() {
await this.initAR();
}
async initAR() {
const context: Context = this.getUIContext().getHostContext() as common.UIAbilityContext;
获取上下文。
// 检查 AR 能力
let isSupported = arEngine.isSupport(context);
if (!isSupported) {
this.status = '设备不支持 AR';
return;
}
检查设备是否支持 AR。
// 创建 AR Session,启用平面检测
let config: arEngine.ARSessionConfig = {
planeFindingMode: arEngine.PlaneFindingMode.HORIZONTAL_AND_VERTICAL
};
this.arSession = await arEngine.createSession(context, config);
创建 AR Session 时,启用平面检测。HORIZONTAL_AND_VERTICAL 表示同时检测水平面(地面、桌面)和垂直面(墙壁)。
if (this.arSession == null) {
this.status = '创建 AR Session 失败';
return;
}
// 启动 AR
this.arSession.start();
this.status = '请移动手机扫描平面';
启动 AR。
第二步:检测平面
// 持续检测平面
this.startPlaneDetection();
}
startPlaneDetection() {
setInterval(() => {
if (this.arSession == null) return;
// 获取所有检测到的平面
this.planes = this.arSession.getAllTrackables(arEngine.ARPlane);
if (this.planes.length > 0) {
this.status = `检测到 ${this.planes.length} 个平面,点击两个点测量距离`;
}
}, 500);
}
每 500ms 检测一次平面。getAllTrackables(arEngine.ARPlane) 获取所有检测到的平面。
第三步:命中检测
// 用户点击屏幕时调用
onScreenTap(x: number, y: number) { 拼多多帮助力入口
if (this.arSession == null) return;
// 命中检测:从屏幕坐标转换到 3D 空间
let hitResult = this.arSession.hitTest(x, y);
hitTest 从屏幕坐标 (x, y) 发射一条射线,检测它和哪个平面相交。返回的是一个数组——可能命中多个平面,我们取第一个。
if (hitResult.length > 0) {
let hit = hitResult[0];
let pose = hit.getHitPose();
// 获取 3D 坐标
let point: Point3D = {
x: pose.tx(),
y: pose.ty(),
z: pose.tz()
};
getHitPose() 返回命中点的位姿(位置 + 旋转)。tx()、ty()、tz() 分别是 X、Y、Z 坐标,单位是米。
this.points.push(point);
if (this.points.length === 1) {
this.status = '已标记第一个点,请点击第二个点';
} else if (this.points.length === 2) {
// 计算距离
let dist = this.calculateDistance(this.points[0], this.points[1]);
this.distance = `${(dist * 100).toFixed(1)} 厘米`;
this.status = '测量完成,点击"重置"继续测量';
}
}
}
把点击的点保存起来。如果已经有两个点,计算距离。
第四步:计算距离
calculateDistance(p1: Point3D, p2: Point3D): number {
let dx = p2.x - p1.x;
let dy = p2.y - p1.y;
let dz = p2.z - p1.z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
三维空间中两点之间的距离公式:√((x2-x1)² + (y2-y1)² + (z2-z1)²)
返回值的单位是米,所以乘以 100 转成厘米。
第五步:页面布局
reset() {
this.points = [];
this.distance = '';
this.status = '请点击第一个点';
}
build() {
Column() {
Text(this.status)
.fontSize(16)
.margin({ top: 20 })
.textAlign(TextAlign.Center)
显示状态文字。
// 测量结果显示
if (this.distance) {
Text(this.distance)
.fontSize(48)
.fontWeight(FontWeight.Bold)
.fontColor('#2196f3')
.margin({ top: 30 })
Text('两点之间的距离')
.fontSize(14)
.fontColor('#999999')
.margin({ top: 5 })
}
显示测量结果。
// AR 视图区域
// 实际项目中,这里会放一个 AR 视图组件
// 用户可以点击视图中的平面来标记点
// 操作按钮
Row() {
Button('重置')
.margin(10)
.onClick(() => this.reset())
Button('撤销')
.margin(10)
.onClick(() => {
if (this.points.length > 0) {
this.points.pop();
this.distance = '';
this.status = '已撤销,请重新标记';
}
})
}
.margin({ top: 30 })
添加重置和撤销按钮。
// 平面信息
if (this.planes.length > 0) {
Text(`已检测到 ${this.planes.length} 个平面`)
.fontSize(12)
.fontColor('#999999')
.margin({ top: 20 })
}
}
.width('100%')
.height('100%')
}
aboutToDisappear() {
if (this.arSession != null) {
this.arSession.stop();
this.arSession.release();
}
}
}
显示检测到的平面数量,页面销毁时释放资源。
命中检测流程
命中检测将屏幕坐标转换为3D空间坐标:
#mermaid-svg-luu3G14uy50MeYbb{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-luu3G14uy50MeYbb .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-luu3G14uy50MeYbb .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-luu3G14uy50MeYbb .error-icon{fill:#552222;}#mermaid-svg-luu3G14uy50MeYbb .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-luu3G14uy50MeYbb .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-luu3G14uy50MeYbb .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-luu3G14uy50MeYbb .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-luu3G14uy50MeYbb .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-luu3G14uy50MeYbb .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-luu3G14uy50MeYbb .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-luu3G14uy50MeYbb .marker{fill:#333333;stroke:#333333;}#mermaid-svg-luu3G14uy50MeYbb .marker.cross{stroke:#333333;}#mermaid-svg-luu3G14uy50MeYbb svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-luu3G14uy50MeYbb p{margin:0;}#mermaid-svg-luu3G14uy50MeYbb .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-luu3G14uy50MeYbb .cluster-label text{fill:#333;}#mermaid-svg-luu3G14uy50MeYbb .cluster-label span{color:#333;}#mermaid-svg-luu3G14uy50MeYbb .cluster-label span p{background-color:transparent;}#mermaid-svg-luu3G14uy50MeYbb .label text,#mermaid-svg-luu3G14uy50MeYbb span{fill:#333;color:#333;}#mermaid-svg-luu3G14uy50MeYbb .node rect,#mermaid-svg-luu3G14uy50MeYbb .node circle,#mermaid-svg-luu3G14uy50MeYbb .node ellipse,#mermaid-svg-luu3G14uy50MeYbb .node polygon,#mermaid-svg-luu3G14uy50MeYbb .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-luu3G14uy50MeYbb .rough-node .label text,#mermaid-svg-luu3G14uy50MeYbb .node .label text,#mermaid-svg-luu3G14uy50MeYbb .image-shape .label,#mermaid-svg-luu3G14uy50MeYbb .icon-shape .label{text-anchor:middle;}#mermaid-svg-luu3G14uy50MeYbb .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-luu3G14uy50MeYbb .rough-node .label,#mermaid-svg-luu3G14uy50MeYbb .node .label,#mermaid-svg-luu3G14uy50MeYbb .image-shape .label,#mermaid-svg-luu3G14uy50MeYbb .icon-shape .label{text-align:center;}#mermaid-svg-luu3G14uy50MeYbb .node.clickable{cursor:pointer;}#mermaid-svg-luu3G14uy50MeYbb .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-luu3G14uy50MeYbb .arrowheadPath{fill:#333333;}#mermaid-svg-luu3G14uy50MeYbb .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-luu3G14uy50MeYbb .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-luu3G14uy50MeYbb .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-luu3G14uy50MeYbb .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-luu3G14uy50MeYbb .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-luu3G14uy50MeYbb .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-luu3G14uy50MeYbb .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-luu3G14uy50MeYbb .cluster text{fill:#333;}#mermaid-svg-luu3G14uy50MeYbb .cluster span{color:#333;}#mermaid-svg-luu3G14uy50MeYbb div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-luu3G14uy50MeYbb .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-luu3G14uy50MeYbb rect.text{fill:none;stroke-width:0;}#mermaid-svg-luu3G14uy50MeYbb .icon-shape,#mermaid-svg-luu3G14uy50MeYbb .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-luu3G14uy50MeYbb .icon-shape p,#mermaid-svg-luu3G14uy50MeYbb .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-luu3G14uy50MeYbb .icon-shape .label rect,#mermaid-svg-luu3G14uy50MeYbb .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-luu3G14uy50MeYbb .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-luu3G14uy50MeYbb .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-luu3G14uy50MeYbb :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}是否1个2个用户点击屏幕获取屏幕坐标x,y调用hitTest方法从摄像头发射射线检测射线与平面交点是否命中平面?获取命中点位姿提示未命中提取3D坐标tx,ty,tz保存测量点已有几个点?等待第二个点计算距离
进阶改进
1. 多点测量
支持标记多个点,计算折线总长度:
calculateTotalDistance(): number {
let total = 0;
for (let i = 1; i < this.points.length; i++) {
total += this.calculateDistance(this.points[i - 1], this.points[i]);
}
return total;
}
2. 面积测量
标记 4 个点,计算矩形面积:
calculateArea(p1: Point3D, p2: Point3D, p3: Point3D, p4: Point3D): number {
let width = this.calculateDistance(p1, p2);
let height = this.calculateDistance(p2, p3);
return width * height;
}
3. 精度提示
AR 测量的精度受很多因素影响:
光线条件平面纹理(纯色平面检测效果差)手机移动的稳定性
你可以在 UI 上显示一个"精度指示器",告诉用户当前测量的可靠程度。
进阶改进方向
AR测量工具可以通过以下方式增强功能:
#mermaid-svg-zJkPXDV4q11LBj9g{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-zJkPXDV4q11LBj9g .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-zJkPXDV4q11LBj9g .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-zJkPXDV4q11LBj9g .error-icon{fill:#552222;}#mermaid-svg-zJkPXDV4q11LBj9g .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-zJkPXDV4q11LBj9g .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-zJkPXDV4q11LBj9g .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-zJkPXDV4q11LBj9g .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-zJkPXDV4q11LBj9g .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-zJkPXDV4q11LBj9g .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-zJkPXDV4q11LBj9g .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-zJkPXDV4q11LBj9g .marker{fill:#333333;stroke:#333333;}#mermaid-svg-zJkPXDV4q11LBj9g .marker.cross{stroke:#333333;}#mermaid-svg-zJkPXDV4q11LBj9g svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-zJkPXDV4q11LBj9g p{margin:0;}#mermaid-svg-zJkPXDV4q11LBj9g .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-zJkPXDV4q11LBj9g .cluster-label text{fill:#333;}#mermaid-svg-zJkPXDV4q11LBj9g .cluster-label span{color:#333;}#mermaid-svg-zJkPXDV4q11LBj9g .cluster-label span p{background-color:transparent;}#mermaid-svg-zJkPXDV4q11LBj9g .label text,#mermaid-svg-zJkPXDV4q11LBj9g span{fill:#333;color:#333;}#mermaid-svg-zJkPXDV4q11LBj9g .node rect,#mermaid-svg-zJkPXDV4q11LBj9g .node circle,#mermaid-svg-zJkPXDV4q11LBj9g .node ellipse,#mermaid-svg-zJkPXDV4q11LBj9g .node polygon,#mermaid-svg-zJkPXDV4q11LBj9g .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-zJkPXDV4q11LBj9g .rough-node .label text,#mermaid-svg-zJkPXDV4q11LBj9g .node .label text,#mermaid-svg-zJkPXDV4q11LBj9g .image-shape .label,#mermaid-svg-zJkPXDV4q11LBj9g .icon-shape .label{text-anchor:middle;}#mermaid-svg-zJkPXDV4q11LBj9g .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-zJkPXDV4q11LBj9g .rough-node .label,#mermaid-svg-zJkPXDV4q11LBj9g .node .label,#mermaid-svg-zJkPXDV4q11LBj9g .image-shape .label,#mermaid-svg-zJkPXDV4q11LBj9g .icon-shape .label{text-align:center;}#mermaid-svg-zJkPXDV4q11LBj9g .node.clickable{cursor:pointer;}#mermaid-svg-zJkPXDV4q11LBj9g .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-zJkPXDV4q11LBj9g .arrowheadPath{fill:#333333;}#mermaid-svg-zJkPXDV4q11LBj9g .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-zJkPXDV4q11LBj9g .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-zJkPXDV4q11LBj9g .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-zJkPXDV4q11LBj9g .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-zJkPXDV4q11LBj9g .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-zJkPXDV4q11LBj9g .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-zJkPXDV4q11LBj9g .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-zJkPXDV4q11LBj9g .cluster text{fill:#333;}#mermaid-svg-zJkPXDV4q11LBj9g .cluster span{color:#333;}#mermaid-svg-zJkPXDV4q11LBj9g div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-zJkPXDV4q11LBj9g .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-zJkPXDV4q11LBj9g rect.text{fill:none;stroke-width:0;}#mermaid-svg-zJkPXDV4q11LBj9g .icon-shape,#mermaid-svg-zJkPXDV4q11LBj9g .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-zJkPXDV4q11LBj9g .icon-shape p,#mermaid-svg-zJkPXDV4q11LBj9g .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-zJkPXDV4q11LBj9g .icon-shape .label rect,#mermaid-svg-zJkPXDV4q11LBj9g .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-zJkPXDV4q11LBj9g .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-zJkPXDV4q11LBj9g .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-zJkPXDV4q11LBj9g :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}AR测量工具多点测量面积测量精度优化标记多个点计算折线总长度标记4个点计算矩形面积考虑光线条件平面纹理影响手机稳定性显示精度指示器
小结
物体测量的核心流程:
arEngine.createSession 创建 AR Session,启用平面检测getAllTrackables(arEngine.ARPlane) 获取检测到的平面hitTest(x, y) 从屏幕坐标转换到 3D 坐标用三维距离公式计算两点之间的距离
AR Engine 提供了平面检测和命中检测能力,距离计算就是简单的数学。实际应用中,难点在于 UI 交互和精度优化。