|
| 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