|
2 | 2 |
|
3 | 3 | Do not completely decode the image file, just read the metadata to get the image width and height.
|
4 | 4 |
|
5 |
| -Just support jpeg, gif, png, webp. |
| 5 | +Just support jpeg, gif, png, webp, bmp. |
6 | 6 |
|
7 | 7 | ## Usage
|
8 | 8 |
|
@@ -43,6 +43,140 @@ void foo(Uint8List image){
|
43 | 43 | }
|
44 | 44 | ```
|
45 | 45 |
|
| 46 | +## AsyncImageInput |
| 47 | + |
| 48 | +If your data source is read asynchronously, consider using `AsyncImageInput`. |
| 49 | + |
| 50 | +A typical use case is [http_input](https://pub.dev/packages/image_size_getter_http_input). |
| 51 | + |
| 52 | +## Custom |
| 53 | + |
| 54 | +We can implement our own input or decoder. |
| 55 | + |
| 56 | +In addition to several built-in implementations, subsequent implementations will also be added to the project through plugin. |
| 57 | + |
| 58 | +### Custom Input |
| 59 | + |
| 60 | +Such as: [http_input](https://github.com/CaiJingLong/dart_image_size_getter/tree/master/image_size_getter_http_input). |
| 61 | + |
| 62 | +In addition, if your picture has verification, for example, you need to use the request header to access it, or you need a post request to get it, you need to customize input. |
| 63 | + |
| 64 | +### Custom Decoder |
| 65 | + |
| 66 | +Such as bmp decoder |
| 67 | + |
| 68 | +Check the file type: |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +The width and height: |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +So, we can write code with: |
| 77 | + |
| 78 | +```dart |
| 79 | +import 'package:image_size_getter/image_size_getter.dart'; |
| 80 | +
|
| 81 | +class BmpDecoder extends BaseDecoder { |
| 82 | + const BmpDecoder(); |
| 83 | +
|
| 84 | + @override |
| 85 | + String get decoderName => 'bmp'; |
| 86 | +
|
| 87 | + @override |
| 88 | + Size getSize(ImageInput input) { |
| 89 | + final widthList = input.getRange(0x12, 0x16); |
| 90 | + final heightList = input.getRange(0x16, 0x1a); |
| 91 | +
|
| 92 | + final width = convertRadix16ToInt(widthList, reverse: true); |
| 93 | + final height = convertRadix16ToInt(heightList, reverse: true); |
| 94 | + return Size(width, height); |
| 95 | + } |
| 96 | +
|
| 97 | + @override |
| 98 | + Future<Size> getSizeAsync(AsyncImageInput input) async { |
| 99 | + final widthList = await input.getRange(0x12, 0x16); |
| 100 | + final heightList = await input.getRange(0x16, 0x1a); |
| 101 | +
|
| 102 | + final width = convertRadix16ToInt(widthList, reverse: true); |
| 103 | + final height = convertRadix16ToInt(heightList, reverse: true); |
| 104 | + return Size(width, height); |
| 105 | + } |
| 106 | +
|
| 107 | + @override |
| 108 | + bool isValid(ImageInput input) { |
| 109 | + final list = input.getRange(0, 2); |
| 110 | + return _isBmp(list); |
| 111 | + } |
| 112 | +
|
| 113 | + @override |
| 114 | + Future<bool> isValidAsync(AsyncImageInput input) async { |
| 115 | + final list = await input.getRange(0, 2); |
| 116 | + return _isBmp(list); |
| 117 | + } |
| 118 | +
|
| 119 | + bool _isBmp(List<int> startList) { |
| 120 | + return startList[0] == 66 && startList[1] == 77; |
| 121 | + } |
| 122 | +} |
| 123 | +
|
| 124 | +``` |
| 125 | + |
| 126 | +Use it: |
| 127 | + |
| 128 | +```dart |
| 129 | +final bmp = File('../example/asset/demo.bmp'); |
| 130 | +
|
| 131 | +const BmpDecoder decoder = BmpDecoder(); |
| 132 | +final input = FileInput(bmp); |
| 133 | +
|
| 134 | +assert(decoder.isValid(input)); |
| 135 | +expect(decoder.getSize(input), Size(256, 256)); |
| 136 | +``` |
| 137 | + |
| 138 | +#### Register custom decoder to image size getter |
| 139 | + |
| 140 | +```dart |
| 141 | +ImageSizeGetter.registerDecoder(const BmpDecoder()); |
| 142 | +``` |
| 143 | + |
| 144 | +The method can also be used to replace the default decoder. |
| 145 | + |
| 146 | +For example, you think the existing JPEG format is not rigorous enough. |
| 147 | + |
| 148 | +```dart |
| 149 | +ImageSizeGetter.registerDecoder(const MyJpegDecoder()); |
| 150 | +``` |
| 151 | + |
| 152 | +#### Use decoder alone |
| 153 | + |
| 154 | +Each decoder can be used alone. |
| 155 | + |
| 156 | +```dart |
| 157 | +void decodeWithImageInput(ImageInput input) { |
| 158 | + BaseDecoder decoder = const GifDecoder(); |
| 159 | + final isGif = decoder.isValid(input); |
| 160 | + print('isGif: $isGif'); |
| 161 | +
|
| 162 | + if (isGif) { |
| 163 | + final size = decoder.getSize(input); |
| 164 | + print('size: $size'); |
| 165 | + } |
| 166 | +} |
| 167 | +
|
| 168 | +void decodeWithAsyncImageInput(AsyncImageInput input) async { |
| 169 | + BaseDecoder decoder = const PngDecoder(); |
| 170 | + final isPng = await decoder.isValidAsync(input); |
| 171 | + print('isPng: $isPng'); |
| 172 | +
|
| 173 | + if (isPng) { |
| 174 | + final size = await decoder.getSizeAsync(input); |
| 175 | + print('size: $size'); |
| 176 | + } |
| 177 | +} |
| 178 | +``` |
| 179 | + |
46 | 180 | ## migrate
|
47 | 181 |
|
48 | 182 | See [migrate](https://github.com/CaiJingLong/dart_image_size_getter/blob/master/library/migrate.md)
|
|
0 commit comments