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 {
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
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 // [dart_BitmapRegionDecoder_newInstanceFromImage]
46 // When you already have an Image (e.g. from SDK), create decoder without passing raw bytes again
47 final Image? imageForDecoder = null; // set from your pipeline when available
48 final BitmapRegionDecoder? decoderFromImage =
49 BitmapRegionDecoder.newInstanceFromImage(imageForDecoder);
50 print('Decoder from Image: ${decoderFromImage != null}');
51 // [dart_BitmapRegionDecoder_newInstanceFromImage]
52
53 // [dart_BitmapRegionDecoder_getWidth]
54 final sourceWidth = _decoder!.width;
55 print('Source image width: $sourceWidth');
56 // [dart_BitmapRegionDecoder_getWidth]
57
58 // [dart_BitmapRegionDecoder_getHeight]
59 final sourceHeight = _decoder!.height;
60 print('Source image height: $sourceHeight');
61 // [dart_BitmapRegionDecoder_getHeight]
62
63 // Demonstrate rectangle creation and usage
64 _demonstrateRectangleUsage();
65
66 // Demonstrate region decoding
67 _demonstrateRegionDecoding();
68
69 // Demonstrate different sample sizes
70 _demonstrateSampleSizes();
71
72 // Demonstrate multiple regions
73 _demonstrateMultipleRegions();
74 }
75
79 void _demonstrateRectangleUsage() {
80 // [dart_Rectangle_constructor]
81 // Create rectangle with x, y, width, height
82 Rectangle rect1 = Rectangle(10, 20, 100, 150);
83 print(
84 "Created rectangle: x=${rect1.x}, y=${rect1.y}, width=${rect1.width}, height=${rect1.height}",
85 );
86 // [dart_Rectangle_constructor]
87
88 // [dart_Rectangle_getX]
89 // Get X coordinate
90 int x = rect1.x;
91 print("Rectangle X coordinate: $x");
92 // [dart_Rectangle_getX]
93
94 // [dart_Rectangle_getY]
95 // Get Y coordinate
96 int y = rect1.y;
97 print("Rectangle Y coordinate: $y");
98 // [dart_Rectangle_getY]
99
100 // [dart_Rectangle_getWidth]
101 // Get width
102 int width = rect1.width;
103 print("Rectangle width: $width");
104 // [dart_Rectangle_getWidth]
105
106 // [dart_Rectangle_getHeight]
107 // Get height
108 int height = rect1.height;
109 print("Rectangle height: $height");
110 // [dart_Rectangle_getHeight]
111
112 // Create different rectangles for different use cases
113 Rectangle fullImage = Rectangle(0, 0, 1024, 768);
114 Rectangle topLeft = Rectangle(0, 0, 512, 384);
115 Rectangle center = Rectangle(256, 192, 512, 384);
116 Rectangle bottomRight = Rectangle(512, 384, 512, 384);
117
118 print("Created different rectangles for different regions");
119 }
120
124 void _demonstrateRegionDecoding() {
125 if (_decoder == null) {
126 print("Decoder not initialized");
127 return;
128 }
129
130 // Create a sample rectangle
131 Rectangle sampleRect = Rectangle(50, 50, 200, 200);
132
133 // [dart_BitmapRegionDecoder_decodeRegion]
134 // Decode region with sample size 1 (full resolution)
135 ImageWrapper decodedImage = _decoder!.decodeRegion(sampleRect, 1);
136 print(
137 "Decoded region: ${sampleRect.width}x${sampleRect.height} at sample size 1",
138 );
139 // [dart_BitmapRegionDecoder_decodeRegion]
140
141 // Demonstrate the decoded image
142 _demonstrateDecodedImage(decodedImage, "Sample Region");
143 }
144
148 void _demonstrateSampleSizes() {
149 if (_decoder == null) {
150 return;
151 }
152
153 Rectangle testRect = Rectangle(0, 0, 400, 300);
154
155 // Decode with different sample sizes
156 List<double> sampleSizes = [1.0, 2.0, 4.0, 8.0];
157
158 for (double sampleSize in sampleSizes) {
159 // [dart_BitmapRegionDecoder_decodeRegion_sampleSize]
160 // Decode region with specific sample size
161 ImageWrapper decodedImage = _decoder!.decodeRegion(testRect, sampleSize);
162 print(
163 "Decoded region with sample size $sampleSize: ${(testRect.width / sampleSize).round()}x${(testRect.height / sampleSize).round()}",
164 );
165 // [dart_BitmapRegionDecoder_decodeRegion_sampleSize]
166
167 _demonstrateDecodedImage(decodedImage, "Sample Size $sampleSize");
168 }
169 }
170
174 void _demonstrateMultipleRegions() {
175 if (_decoder == null) {
176 return;
177 }
178
179 // Define multiple regions to decode
180 List<Rectangle> regions = [
181 Rectangle(0, 0, 256, 256), // Top-left quadrant
182 Rectangle(256, 0, 256, 256), // Top-right quadrant
183 Rectangle(0, 256, 256, 256), // Bottom-left quadrant
184 Rectangle(256, 256, 256, 256), // Bottom-right quadrant
185 ];
186
187 print("=== Decoding Multiple Regions ===");
188 for (int i = 0; i < regions.length; i++) {
189 Rectangle region = regions[i];
190
191 // [dart_BitmapRegionDecoder_decodeRegion_multiple]
192 // Decode multiple regions
193 ImageWrapper decodedImage = _decoder!.decodeRegion(region, 1);
194 print(
195 "Region ${i + 1}: ${region.width}x${region.height} at (${region.x}, ${region.y})",
196 );
197 // [dart_BitmapRegionDecoder_decodeRegion_multiple]
198
199 _demonstrateDecodedImage(decodedImage, "Region ${i + 1}");
200 }
201 }
202
206 void _demonstrateDecodedImage(ImageWrapper image, String description) {
207 print("--- $description ---");
208 print("Image dimensions: ${image.width}x${image.height}");
209 print("Image format: ${image.format}");
210 print("Image data size: ${image.data.length} bytes");
211 print("---");
212 }
213
217 Future<void> demonstrateAdvancedFeatures() async {
218 print("=== Advanced BitmapRegionDecoder Features ===");
219
220 if (_imageData == null) {
221 return;
222 }
223
224 // Create multiple decoders for different images
225 List<Uint8List> imageDataList = [
226 _imageData!,
227 Uint8List.fromList([0xFF, 0xD8, 0xFF, 0xE0]), // JPEG header
228 Uint8List.fromList([0x47, 0x49, 0x46, 0x38]), // GIF header
229 ];
230
231 for (int i = 0; i < imageDataList.length; i++) {
232 try {
233 // [dart_BitmapRegionDecoder_newInstance_advanced]
234 // Create decoder for different image types
235 BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(
236 imageDataList[i],
237 );
238 print("Created decoder for image type ${i + 1}");
239 // [dart_BitmapRegionDecoder_newInstance_advanced]
240
241 // Test decoding with different regions
242 Rectangle testRect = Rectangle(0, 0, 100, 100);
243 ImageWrapper decodedImage = decoder.decodeRegion(testRect, 1);
244 print("Successfully decoded region from image type ${i + 1}");
245 } catch (e) {
246 print("Failed to decode image type ${i + 1}: $e");
247 }
248 }
249 }
250
255 print("=== Error Handling ===");
256
257 // Test with invalid image data
258 Uint8List invalidData = Uint8List.fromList([0x00, 0x01, 0x02, 0x03]);
259
260 try {
261 // [dart_BitmapRegionDecoder_newInstance_error]
262 // Create decoder with invalid data
263 BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(
264 invalidData,
265 );
266 print("Created decoder with invalid data");
267 // [dart_BitmapRegionDecoder_newInstance_error]
268
269 // Try to decode region
270 Rectangle rect = Rectangle(0, 0, 50, 50);
271 ImageWrapper image = decoder.decodeRegion(rect, 1);
272 print("Successfully decoded region from invalid data");
273 } catch (e) {
274 print("Expected error when creating decoder with invalid data: $e");
275 }
276
277 // Test with invalid rectangle
278 if (_decoder != null) {
279 try {
280 Rectangle invalidRect = Rectangle(-10, -10, 100, 100);
281 ImageWrapper image = _decoder!.decodeRegion(invalidRect, 1);
282 print("Successfully decoded region with negative coordinates");
283 } catch (e) {
284 print("Expected error with invalid rectangle: $e");
285 }
286 }
287 }
288
293 print("=== Performance Optimization ===");
294
295 if (_decoder == null) {
296 return;
297 }
298
299 // Test different sample sizes for performance
300 Rectangle largeRect = Rectangle(0, 0, 800, 600);
301 List<double> sampleSizes = [1.0, 2.0, 4.0, 8.0, 16.0];
302
303 for (double sampleSize in sampleSizes) {
304 final stopwatch = Stopwatch()..start();
305
306 ImageWrapper image = _decoder!.decodeRegion(largeRect, sampleSize);
307
308 stopwatch.stop();
309 print(
310 "Sample size $sampleSize: ${stopwatch.elapsedMilliseconds}ms, "
311 "Output size: ${image.width}x${image.height}",
312 );
313 }
314 }
315
320 print("=== Rectangle Manipulation ===");
321
322 // Create base rectangle
323 Rectangle baseRect = Rectangle(100, 100, 200, 150);
324 print(
325 "Base rectangle: ${baseRect.x}, ${baseRect.y}, ${baseRect.width}x${baseRect.height}",
326 );
327
328 // Create different variations
329 List<Rectangle> variations = [
330 Rectangle(
331 baseRect.x,
332 baseRect.y,
333 baseRect.width ~/ 2,
334 baseRect.height,
335 ), // Half width
336 Rectangle(
337 baseRect.x,
338 baseRect.y,
339 baseRect.width,
340 baseRect.height ~/ 2,
341 ), // Half height
342 Rectangle(
343 baseRect.x + 50,
344 baseRect.y + 25,
345 baseRect.width - 100,
346 baseRect.height - 50,
347 ), // Centered smaller
348 Rectangle(0, 0, baseRect.width, baseRect.height), // Top-left aligned
349 ];
350
351 for (int i = 0; i < variations.length; i++) {
352 Rectangle rect = variations[i];
353 print(
354 "Variation ${i + 1}: ${rect.x}, ${rect.y}, ${rect.width}x${rect.height}",
355 );
356 }
357 }
358
362 Future<void> runExample() async {
363 print("=== BitmapRegionDecoder Example ===");
364
367 await demonstrateAdvancedFeatures();
370
371 // Wait a bit for processing
372 await Future.delayed(Duration(seconds: 2));
373
374 print("=== Example completed ===");
375 }
376}
377
381Future<void> main() async {
383 await example.runExample();
384}