asset.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. import 'package:multimage_picker/multimage_picker.dart';
  4. class Asset {
  5. /// The resource identifier
  6. String _identifier;
  7. /// The resource file name
  8. String _name;
  9. /// Original image width
  10. int _originalWidth;
  11. /// Original image height
  12. int _originalHeight;
  13. Asset(
  14. this._identifier,
  15. this._name,
  16. this._originalWidth,
  17. this._originalHeight,
  18. );
  19. /// The BinaryChannel name this asset is listening on.
  20. String get _channel {
  21. return 'multimage_picker/image/$_identifier';
  22. }
  23. String get _thumbChannel => '$_channel.thumb';
  24. String get _originalChannel => '$_channel.original';
  25. /// Returns the original image width
  26. int get originalWidth {
  27. return _originalWidth;
  28. }
  29. /// Returns the original image height
  30. int get originalHeight {
  31. return _originalHeight;
  32. }
  33. /// Returns true if the image is landscape
  34. bool get isLandscape {
  35. return _originalWidth > _originalHeight;
  36. }
  37. /// Returns true if the image is Portrait
  38. bool get isPortrait {
  39. return _originalWidth < _originalHeight;
  40. }
  41. /// Returns the image identifier
  42. String get identifier {
  43. return _identifier;
  44. }
  45. /// Returns the image name
  46. String get name {
  47. return _name;
  48. }
  49. /// Requests a thumbnail for the [Asset] with give [width] and [hegiht].
  50. ///
  51. /// The method returns a Future with the [ByteData] for the thumb,
  52. /// as well as storing it in the _thumbData property which can be requested
  53. /// later again, without need to call this method again.
  54. ///
  55. /// You can also pass the optional parameter [quality] to reduce the quality
  56. /// and the size of the returned image if needed. The value should be between
  57. /// 0 and 100. By default it set to 100 (max quality).
  58. ///
  59. /// Once you don't need this thumb data it is a good practice to release it,
  60. /// by calling releaseThumb() method.
  61. Future<ByteData> requestThumbnail(int width, int height,
  62. {int quality = 100}) async {
  63. assert(width != null);
  64. assert(height != null);
  65. if (width != null && width < 0) {
  66. throw new ArgumentError.value(width, 'width cannot be negative');
  67. }
  68. if (height != null && height < 0) {
  69. throw new ArgumentError.value(height, 'height cannot be negative');
  70. }
  71. if (quality < 0 || quality > 100) {
  72. throw new ArgumentError.value(
  73. quality, 'quality should be in range 0-100');
  74. }
  75. Completer completer = new Completer<ByteData>();
  76. BinaryMessages.setMessageHandler(_thumbChannel, (ByteData message) {
  77. completer.complete(message);
  78. BinaryMessages.setMessageHandler(_thumbChannel, null);
  79. });
  80. MultiImagePicker.requestThumbnail(_identifier, width, height, quality);
  81. return completer.future;
  82. }
  83. /// Requests the original image for that asset.
  84. ///
  85. /// You can also pass the optional parameter [quality] to reduce the quality
  86. /// and the size of the returned image if needed. The value should be between
  87. /// 0 and 100. By default it set to 100 (max quality).
  88. ///
  89. /// The method returns a Future with the [ByteData] for the image,
  90. /// as well as storing it in the _imageData property which can be requested
  91. /// later again, without need to call this method again.
  92. ///
  93. /// Once you don't need this data it is a good practice to release it,
  94. /// by calling releaseOriginal() method.
  95. Future<ByteData> requestOriginal({int quality = 100}) {
  96. if (quality < 0 || quality > 100) {
  97. throw new ArgumentError.value(
  98. quality, 'quality should be in range 0-100');
  99. }
  100. Completer completer = new Completer<ByteData>();
  101. BinaryMessages.setMessageHandler(_originalChannel, (ByteData message) {
  102. completer.complete(message);
  103. BinaryMessages.setMessageHandler(_originalChannel, null);
  104. });
  105. MultiImagePicker.requestOriginal(_identifier, quality);
  106. return completer.future;
  107. }
  108. /// Requests the original image meta data
  109. Future<Metadata> requestMetadata() {
  110. return MultiImagePicker.requestMetadata(_identifier);
  111. }
  112. }