Loading...
Searching...
No Matches
bitmap_region_decoder_example.dart
Go to the documentation of this file.
1import 'dart:async';
2import 'dart:io';
3import 'dart:typed_data';
4import 'package:navigine_sdk/navigine_sdk.dart';
5
10class BitmapRegionDecoderExample {
11 BitmapRegionDecoder? _decoder;
12 Uint8List? _imageData;
13
14 BitmapRegionDecoderExample() {
15 _loadSampleImageData();
16 }
17
21 void _loadSampleImageData() {
22 // Simulate loading image data (in real app, this would be from file or network)
23 _imageData = Uint8List.fromList([
24 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // PNG header
25 // ... more image data would be here
26 ]);
27 print("Sample image data loaded (${_imageData!.length} bytes)");
28 }
29
33 void demonstrateBitmapRegionDecoderMethods() {
34 if (_imageData == null) {
35 print("No image data available");
36 return;
37 }
38
39 // [dart_BitmapRegionDecoder_newInstance]
40 // Create new instance of BitmapRegionDecoder
41 _decoder = BitmapRegionDecoder.newInstance(_imageData!);
42 print("Created BitmapRegionDecoder instance");
43 // [dart_BitmapRegionDecoder_newInstance]
44
45 // Demonstrate rectangle creation and usage
46 _demonstrateRectangleUsage();
47
48 // Demonstrate region decoding
49 _demonstrateRegionDecoding();
50
51 // Demonstrate different sample sizes
52 _demonstrateSampleSizes();
53
54 // Demonstrate multiple regions
55 _demonstrateMultipleRegions();
56 }
57
61 void _demonstrateRectangleUsage() {
62 // [dart_Rectangle_constructor]
63 // Create rectangle with x, y, width, height
64 Rectangle rect1 = Rectangle(10, 20, 100, 150);
65 print(
66 "Created rectangle: x=${rect1.x}, y=${rect1.y}, width=${rect1.width}, height=${rect1.height}",
67 );
68 // [dart_Rectangle_constructor]
69
70 // [dart_Rectangle_getX]
71 // Get X coordinate
72 int x = rect1.x;
73 print("Rectangle X coordinate: $x");
74 // [dart_Rectangle_getX]
75
76 // [dart_Rectangle_getY]
77 // Get Y coordinate
78 int y = rect1.y;
79 print("Rectangle Y coordinate: $y");
80 // [dart_Rectangle_getY]
81
82 // [dart_Rectangle_getWidth]
83 // Get width
84 int width = rect1.width;
85 print("Rectangle width: $width");
86 // [dart_Rectangle_getWidth]
87
88 // [dart_Rectangle_getHeight]
89 // Get height
90 int height = rect1.height;
91 print("Rectangle height: $height");
92 // [dart_Rectangle_getHeight]
93
94 // Create different rectangles for different use cases
95 Rectangle fullImage = Rectangle(0, 0, 1024, 768);
96 Rectangle topLeft = Rectangle(0, 0, 512, 384);
97 Rectangle center = Rectangle(256, 192, 512, 384);
98 Rectangle bottomRight = Rectangle(512, 384, 512, 384);
99
100 print("Created different rectangles for different regions");
101 }
102
106 void _demonstrateRegionDecoding() {
107 if (_decoder == null) {
108 print("Decoder not initialized");
109 return;
110 }
111
112 // Create a sample rectangle
113 Rectangle sampleRect = Rectangle(50, 50, 200, 200);
114
115 // [dart_BitmapRegionDecoder_decodeRegion]
116 // Decode region with sample size 1 (full resolution)
117 ImageWrapper decodedImage = _decoder!.decodeRegion(sampleRect, 1);
118 print(
119 "Decoded region: ${sampleRect.width}x${sampleRect.height} at sample size 1",
120 );
121 // [dart_BitmapRegionDecoder_decodeRegion]
122
123 // Demonstrate the decoded image
124 _demonstrateDecodedImage(decodedImage, "Sample Region");
125 }
126
130 void _demonstrateSampleSizes() {
131 if (_decoder == null) {
132 return;
133 }
134
135 Rectangle testRect = Rectangle(0, 0, 400, 300);
136
137 // Decode with different sample sizes
138 List<double> sampleSizes = [1.0, 2.0, 4.0, 8.0];
139
140 for (double sampleSize in sampleSizes) {
141 // [dart_BitmapRegionDecoder_decodeRegion_sampleSize]
142 // Decode region with specific sample size
143 ImageWrapper decodedImage = _decoder!.decodeRegion(testRect, sampleSize);
144 print(
145 "Decoded region with sample size $sampleSize: ${(testRect.width / sampleSize).round()}x${(testRect.height / sampleSize).round()}",
146 );
147 // [dart_BitmapRegionDecoder_decodeRegion_sampleSize]
148
149 _demonstrateDecodedImage(decodedImage, "Sample Size $sampleSize");
150 }
151 }
152
156 void _demonstrateMultipleRegions() {
157 if (_decoder == null) {
158 return;
159 }
160
161 // Define multiple regions to decode
162 List<Rectangle> regions = [
163 Rectangle(0, 0, 256, 256), // Top-left quadrant
164 Rectangle(256, 0, 256, 256), // Top-right quadrant
165 Rectangle(0, 256, 256, 256), // Bottom-left quadrant
166 Rectangle(256, 256, 256, 256), // Bottom-right quadrant
167 ];
168
169 print("=== Decoding Multiple Regions ===");
170 for (int i = 0; i < regions.length; i++) {
171 Rectangle region = regions[i];
172
173 // [dart_BitmapRegionDecoder_decodeRegion_multiple]
174 // Decode multiple regions
175 ImageWrapper decodedImage = _decoder!.decodeRegion(region, 1);
176 print(
177 "Region ${i + 1}: ${region.width}x${region.height} at (${region.x}, ${region.y})",
178 );
179 // [dart_BitmapRegionDecoder_decodeRegion_multiple]
180
181 _demonstrateDecodedImage(decodedImage, "Region ${i + 1}");
182 }
183 }
184
188 void _demonstrateDecodedImage(ImageWrapper image, String description) {
189 print("--- $description ---");
190 print("Image dimensions: ${image.width}x${image.height}");
191 print("Image format: ${image.format}");
192 print("Image data size: ${image.data.length} bytes");
193 print("---");
194 }
195
199 Future<void> demonstrateAdvancedFeatures() async {
200 print("=== Advanced BitmapRegionDecoder Features ===");
201
202 if (_imageData == null) {
203 return;
204 }
205
206 // Create multiple decoders for different images
207 List<Uint8List> imageDataList = [
208 _imageData!,
209 Uint8List.fromList([0xFF, 0xD8, 0xFF, 0xE0]), // JPEG header
210 Uint8List.fromList([0x47, 0x49, 0x46, 0x38]), // GIF header
211 ];
212
213 for (int i = 0; i < imageDataList.length; i++) {
214 try {
215 // [dart_BitmapRegionDecoder_newInstance_advanced]
216 // Create decoder for different image types
217 BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(
218 imageDataList[i],
219 );
220 print("Created decoder for image type ${i + 1}");
221 // [dart_BitmapRegionDecoder_newInstance_advanced]
222
223 // Test decoding with different regions
224 Rectangle testRect = Rectangle(0, 0, 100, 100);
225 ImageWrapper decodedImage = decoder.decodeRegion(testRect, 1);
226 print("Successfully decoded region from image type ${i + 1}");
227 } catch (e) {
228 print("Failed to decode image type ${i + 1}: $e");
229 }
230 }
231 }
232
236 void demonstrateErrorHandling() {
237 print("=== Error Handling ===");
238
239 // Test with invalid image data
240 Uint8List invalidData = Uint8List.fromList([0x00, 0x01, 0x02, 0x03]);
241
242 try {
243 // [dart_BitmapRegionDecoder_newInstance_error]
244 // Create decoder with invalid data
245 BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(
246 invalidData,
247 );
248 print("Created decoder with invalid data");
249 // [dart_BitmapRegionDecoder_newInstance_error]
250
251 // Try to decode region
252 Rectangle rect = Rectangle(0, 0, 50, 50);
253 ImageWrapper image = decoder.decodeRegion(rect, 1);
254 print("Successfully decoded region from invalid data");
255 } catch (e) {
256 print("Expected error when creating decoder with invalid data: $e");
257 }
258
259 // Test with invalid rectangle
260 if (_decoder != null) {
261 try {
262 Rectangle invalidRect = Rectangle(-10, -10, 100, 100);
263 ImageWrapper image = _decoder!.decodeRegion(invalidRect, 1);
264 print("Successfully decoded region with negative coordinates");
265 } catch (e) {
266 print("Expected error with invalid rectangle: $e");
267 }
268 }
269 }
270
274 void demonstratePerformanceOptimization() {
275 print("=== Performance Optimization ===");
276
277 if (_decoder == null) {
278 return;
279 }
280
281 // Test different sample sizes for performance
282 Rectangle largeRect = Rectangle(0, 0, 800, 600);
283 List<double> sampleSizes = [1.0, 2.0, 4.0, 8.0, 16.0];
284
285 for (double sampleSize in sampleSizes) {
286 final stopwatch = Stopwatch()..start();
287
288 ImageWrapper image = _decoder!.decodeRegion(largeRect, sampleSize);
289
290 stopwatch.stop();
291 print(
292 "Sample size $sampleSize: ${stopwatch.elapsedMilliseconds}ms, "
293 "Output size: ${image.width}x${image.height}",
294 );
295 }
296 }
297
301 void demonstrateRectangleManipulation() {
302 print("=== Rectangle Manipulation ===");
303
304 // Create base rectangle
305 Rectangle baseRect = Rectangle(100, 100, 200, 150);
306 print(
307 "Base rectangle: ${baseRect.x}, ${baseRect.y}, ${baseRect.width}x${baseRect.height}",
308 );
309
310 // Create different variations
311 List<Rectangle> variations = [
312 Rectangle(
313 baseRect.x,
314 baseRect.y,
315 baseRect.width ~/ 2,
316 baseRect.height,
317 ), // Half width
318 Rectangle(
319 baseRect.x,
320 baseRect.y,
321 baseRect.width,
322 baseRect.height ~/ 2,
323 ), // Half height
324 Rectangle(
325 baseRect.x + 50,
326 baseRect.y + 25,
327 baseRect.width - 100,
328 baseRect.height - 50,
329 ), // Centered smaller
330 Rectangle(0, 0, baseRect.width, baseRect.height), // Top-left aligned
331 ];
332
333 for (int i = 0; i < variations.length; i++) {
334 Rectangle rect = variations[i];
335 print(
336 "Variation ${i + 1}: ${rect.x}, ${rect.y}, ${rect.width}x${rect.height}",
337 );
338 }
339 }
340
344 Future<void> runExample() async {
345 print("=== BitmapRegionDecoder Example ===");
346
347 demonstrateBitmapRegionDecoderMethods();
348 demonstrateRectangleManipulation();
349 await demonstrateAdvancedFeatures();
350 demonstrateErrorHandling();
351 demonstratePerformanceOptimization();
352
353 // Wait a bit for processing
354 await Future.delayed(Duration(seconds: 2));
355
356 print("=== Example completed ===");
357 }
358}
359
363Future<void> main() async {
365 await example.runExample();
366}