Skip to content

Commit 7a6f896

Browse files
author
António Valente
committed
add depth scene snapshot example
1 parent 21ab4fe commit 7a6f896

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed

example/ios/Runner/AppDelegate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Flutter
22
import UIKit
33

4-
@UIApplicationMain
4+
@main
55
@objc class AppDelegate: FlutterAppDelegate {
66
override func application(
77
_ application: UIApplication,

example/lib/main.dart

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:arkit_plugin_example/network_image_detection.dart';
1717
import 'package:arkit_plugin_example/occlusion_page.dart';
1818
import 'package:arkit_plugin_example/physics_page.dart';
1919
import 'package:arkit_plugin_example/plane_detection_page.dart';
20+
import 'package:arkit_plugin_example/snapshot_depth_scene.dart';
2021
import 'package:arkit_plugin_example/snapshot_scene.dart';
2122
import 'package:arkit_plugin_example/tap_page.dart';
2223
import 'package:arkit_plugin_example/face_detection_page.dart';
@@ -214,6 +215,13 @@ class MyApp extends StatelessWidget {
214215
() => Navigator.of(context).push<void>(
215216
MaterialPageRoute(builder: (c) => CameraPropertiesPage())),
216217
),
218+
Sample(
219+
'Depth Scene Snapshot',
220+
'Make a photo of the depth scene using LiDAR',
221+
Icons.camera,
222+
() => Navigator.of(context).push<void>(
223+
MaterialPageRoute(builder: (c) => SnapshotDepthScenePage())),
224+
),
217225
];
218226

219227
return Scaffold(

example/lib/snapshot_depth_scene.dart

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import 'package:arkit_plugin/arkit_plugin.dart';
2+
import 'package:arkit_plugin_example/util/ar_helper.dart';
3+
import 'package:flutter/material.dart';
4+
5+
import 'dart:math' as math;
6+
7+
class SnapshotDepthScenePage extends StatefulWidget {
8+
@override
9+
_SnapshotDepthScenePageState createState() => _SnapshotDepthScenePageState();
10+
}
11+
12+
class _SnapshotDepthScenePageState extends State<SnapshotDepthScenePage> {
13+
late ARKitController arkitController;
14+
15+
@override
16+
void dispose() {
17+
arkitController.dispose();
18+
super.dispose();
19+
}
20+
21+
@override
22+
Widget build(BuildContext context) => Scaffold(
23+
appBar: AppBar(
24+
title: const Text('Snapshot'),
25+
),
26+
floatingActionButton: FloatingActionButton(
27+
child: Icon(Icons.camera_alt),
28+
onPressed: () async {
29+
try {
30+
final data = await arkitController.snapshotWithDepthData();
31+
if (data == null) return;
32+
final image = data['image']! as MemoryImage;
33+
final depthData = (data..remove('image')).map<String, String>(
34+
(key, value) => MapEntry(key, value.toString()),
35+
);
36+
await Navigator.push(
37+
context,
38+
MaterialPageRoute(
39+
builder: (context) => SnapshotPreview(
40+
imageProvider: image,
41+
depthData: depthData,
42+
),
43+
),
44+
);
45+
} catch (e) {
46+
print(e);
47+
}
48+
},
49+
),
50+
body: Container(
51+
child: ARKitSceneView(
52+
configuration: ARKitConfiguration.depthTracking,
53+
onARKitViewCreated: onARKitViewCreated,
54+
),
55+
));
56+
57+
void onARKitViewCreated(ARKitController arkitController) {
58+
this.arkitController = arkitController;
59+
this.arkitController.add(createSphere());
60+
}
61+
}
62+
63+
class SnapshotPreview extends StatelessWidget {
64+
const SnapshotPreview({
65+
Key? key,
66+
required this.imageProvider,
67+
required this.depthData,
68+
}) : super(key: key);
69+
70+
final ImageProvider imageProvider;
71+
final Map<String, String> depthData;
72+
73+
@override
74+
Widget build(BuildContext context) {
75+
return Scaffold(
76+
appBar: AppBar(
77+
title: const Text('Image Preview'),
78+
actions: [
79+
IconButton(
80+
icon: Icon(Icons.info),
81+
onPressed: () => Navigator.push(
82+
context,
83+
MaterialPageRoute(
84+
builder: (context) => DepthDataPreview(
85+
depthData: depthData,
86+
),
87+
),
88+
),
89+
)
90+
],
91+
),
92+
body: Stack(
93+
fit: StackFit.expand,
94+
children: [
95+
Transform.rotate(
96+
angle: 90 * math.pi / 180,
97+
child: Image(image: imageProvider),
98+
),
99+
],
100+
),
101+
);
102+
}
103+
}
104+
105+
class DepthDataPreview extends StatelessWidget {
106+
const DepthDataPreview({
107+
Key? key,
108+
required this.depthData,
109+
}) : super(key: key);
110+
111+
final Map<String, String> depthData;
112+
113+
@override
114+
Widget build(BuildContext context) {
115+
return Scaffold(
116+
appBar: AppBar(
117+
title: const Text('Depth Data Preview'),
118+
),
119+
body: ListView(
120+
children: [
121+
ListTile(
122+
title: Text('Depth Width'),
123+
subtitle: Text(depthData['depthWidth']!),
124+
),
125+
ListTile(
126+
title: Text('Depth Height'),
127+
subtitle: Text(depthData['depthHeight']!),
128+
),
129+
ListTile(
130+
title: Text('Intrinsics'),
131+
subtitle: Text(depthData['intrinsics']!),
132+
),
133+
ListTile(
134+
title: Text('Depth Map'),
135+
subtitle: Text(depthData['depthMap']!),
136+
),
137+
],
138+
),
139+
);
140+
}
141+
}

0 commit comments

Comments
 (0)