Skip to content

Commit 7861e7a

Browse files
authored
Merge pull request #39 from weta-vn/dev_/hotfix20211015
Dev /hotfix20211015
2 parents 98c1852 + fc14b0a commit 7861e7a

File tree

9 files changed

+188
-69
lines changed

9 files changed

+188
-69
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [0.1.5+5]
2+
3+
* **Fixbugs:**
4+
* Check permission for camera & photo gallery
5+
* Fixbug about OCR text editting
6+
17
## [0.1.5+1]
28

39
* **New features:**

analysis_options.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ analyzer:
1717
# Don't cast implicitly.
1818
implicit-casts: false
1919
# Don't fall back to 'dynamic' implicitly.
20-
implicit-dynamic: false
20+
# implicit-dynamic: false
2121
errors:
2222
# Without ignore here, we cause import of all_lint_rules to warn, because some rules conflict.
2323
# We explicitly enabled even conflicting rules and are fixing the conflicts in this file.
2424
included_file_warning: ignore
2525

2626
# Treat missing required parameters as an error, not as a hint or a warning.
27-
missing_required_param: error
27+
# missing_required_param: error
2828

2929
# Treat missing returns as an error, not as a hint or a warning.
3030
missing_return: error

example/ios/Flutter/AppFrameworkInfo.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
<key>CFBundleVersion</key>
2222
<string>1.0</string>
2323
<key>MinimumOSVersion</key>
24-
<string>8.0</string>
24+
<string>9.0</string>
2525
</dict>
2626
</plist>

example/ios/Podfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ EXTERNAL SOURCES:
4646
:path: ".symlinks/plugins/photo_manager/ios"
4747

4848
SPEC CHECKSUMS:
49-
camera: 3164201dc344383e62282964016528c4f5a9ad50
50-
Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c
49+
camera: b9eb73c055259c6f808a9da4a0d43780f6645281
50+
Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a
5151
flutter_native_image: 9c0b7451838484458e5b0fae007b86a4c2d4bdfe
5252
image_cropper: f1668dd8d2cad2d357955caad15a40547856edcb
5353
image_editor: c1d038630eedea60d2dee9c14f36aa66c7f9cfab
54-
path_provider: abfe2b5c733d04e238b0d8691db0cfd63a27a93c
54+
path_provider: d1e9807085df1f9cc9318206cd649dc0b76be3de
5555
photo_manager: 84fa94fbeb82e607333ea9a13c43b58e0903a463
5656
TOCropViewController: 3105367e808b7d3d886a74ff59bf4804e7d3ab38
5757

5858
PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c
5959

60-
COCOAPODS: 1.10.1
60+
COCOAPODS: 1.11.0

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
path: ".."
88
relative: true
99
source: path
10-
version: "0.1.5+1"
10+
version: "0.1.5+5"
1111
archive:
1212
dependency: transitive
1313
description:

lib/configs/image_picker_configs.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,26 @@ class ImagePickerConfigs {
494494
/// Defaults to "OCR".
495495
String get textOCR => getTranslatedString("image_picker_ocr", "OCR");
496496

497+
/// Get localized text for label "image_picker_request_permission".
498+
///
499+
/// Defaults to "Request Permission".
500+
String get textRequestPermission => getTranslatedString(
501+
"image_picker_request_permission", "Request Permission");
502+
503+
/// Get localized text for label "image_picker_request_camera_permission".
504+
///
505+
/// Defaults to "You need allow camera permission.".
506+
String get textRequestCameraPermission => getTranslatedString(
507+
"image_picker_request_camera_permission",
508+
"You need allow camera permission.");
509+
510+
/// Get localized text for label "image_picker_request_gallery_permission".
511+
///
512+
/// Defaults to "You need allow photo gallery permission.".
513+
String get textRequestGalleryPermission => getTranslatedString(
514+
"image_picker_request_gallery_permission",
515+
"You need allow photo gallery permission.");
516+
497517
/// Translate string by translateFunc.
498518
String getTranslatedString(String name, String defaultValue) {
499519
return translateFunc.call(name, defaultValue);

lib/widgets/picker/image_picker.dart

Lines changed: 149 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ class _ImagePickerState extends State<ImagePicker>
108108
/// Flag indicating state of image selecting.
109109
bool _isImageSelectedDone = false;
110110

111+
/// Flag indicating status of permission to access cameras
112+
bool _isCameraPermissionOK = false;
113+
114+
/// Flag indicating status of permission to access photo libray
115+
bool _isGalleryPermissionOK = false;
116+
111117
/// Image configuration.
112118
ImagePickerConfigs _configs = ImagePickerConfigs();
113119

@@ -228,16 +234,20 @@ class _ImagePickerState extends State<ImagePicker>
228234
Future<void> _initPhotoCapture() async {
229235
LogUtils.log("[_initPhotoCapture] start");
230236

231-
// List all cameras in current device.
232-
_cameras = await availableCameras();
233-
234-
// Select new camera for capturing.
235-
if (_cameras.isNotEmpty) {
236-
final CameraDescription? newDescription = _getCamera(
237-
_cameras, _getCameraDirection(_configs.cameraLensDirection));
238-
if (newDescription != null) {
239-
await _onNewCameraSelected(newDescription);
237+
try {
238+
// List all cameras in current device.
239+
_cameras = await availableCameras();
240+
241+
// Select new camera for capturing.
242+
if (_cameras.isNotEmpty) {
243+
final CameraDescription? newDescription = _getCamera(
244+
_cameras, _getCameraDirection(_configs.cameraLensDirection));
245+
if (newDescription != null) {
246+
await _onNewCameraSelected(newDescription);
247+
}
240248
}
249+
} on CameraException catch (e) {
250+
LogUtils.log('Camera error ${e.code}, ${e.description}');
241251
}
242252
}
243253

@@ -265,6 +275,43 @@ class _ImagePickerState extends State<ImagePicker>
265275
}
266276
}
267277

278+
/// Initialize current selected camera
279+
void _initCameraController() {
280+
// Create future object for initializing new camera controller.
281+
final cameraController = _controller!;
282+
_initializeControllerFuture =
283+
cameraController.initialize().then((value) async {
284+
LogUtils.log("[_onNewCameraSelected] cameraController initialized.");
285+
286+
_isCameraPermissionOK = true;
287+
288+
// After initialized, setting zoom & exposure values
289+
await Future.wait([
290+
cameraController.lockCaptureOrientation(DeviceOrientation.portraitUp),
291+
cameraController.setFlashMode(_configs.flashMode),
292+
cameraController
293+
.getMinExposureOffset()
294+
.then((value) => _minAvailableExposureOffset = value),
295+
cameraController
296+
.getMaxExposureOffset()
297+
.then((value) => _maxAvailableExposureOffset = value),
298+
cameraController
299+
.getMaxZoomLevel()
300+
.then((value) => _maxAvailableZoom = value),
301+
cameraController
302+
.getMinZoomLevel()
303+
.then((value) => _minAvailableZoom = value),
304+
]);
305+
306+
// Refresh screen for applying new updated
307+
if (mounted) {
308+
setState(() {});
309+
}
310+
}).catchError((e) {
311+
LogUtils.log('Camera error ${e.toString()}');
312+
});
313+
}
314+
268315
/// Select new camera for capturing
269316
Future<void> _onNewCameraSelected(CameraDescription cameraDescription) async {
270317
LogUtils.log("[_onNewCameraSelected] start");
@@ -281,64 +328,44 @@ class _ImagePickerState extends State<ImagePicker>
281328
);
282329
_controller = cameraController;
283330

331+
// Init selected camera
332+
_initCameraController();
333+
284334
// If the controller is updated then update the UI.
285-
cameraController.addListener(() {
335+
_controller!.addListener(() {
286336
if (mounted) setState(() {});
287337
if (cameraController.value.hasError) {
288338
LogUtils.log('Camera error ${cameraController.value.errorDescription}');
289339
}
290340
});
291-
292-
// Create future object for initializing new camera controller.
293-
try {
294-
_initializeControllerFuture =
295-
cameraController.initialize().then((value) async {
296-
LogUtils.log("[_onNewCameraSelected] cameraController initialized.");
297-
298-
// After initialized, setting zoom & exposure values
299-
await Future.wait([
300-
cameraController.lockCaptureOrientation(DeviceOrientation.portraitUp),
301-
cameraController.setFlashMode(_configs.flashMode),
302-
cameraController
303-
.getMinExposureOffset()
304-
.then((value) => _minAvailableExposureOffset = value),
305-
cameraController
306-
.getMaxExposureOffset()
307-
.then((value) => _maxAvailableExposureOffset = value),
308-
cameraController
309-
.getMaxZoomLevel()
310-
.then((value) => _maxAvailableZoom = value),
311-
cameraController
312-
.getMinZoomLevel()
313-
.then((value) => _minAvailableZoom = value),
314-
]);
315-
316-
// Refresh screen for applying new updated
317-
if (mounted) {
318-
setState(() {});
319-
}
320-
});
321-
} on CameraException catch (e) {
322-
LogUtils.log('Camera error ${e.code}, ${e.description}');
323-
}
324341
}
325342

326343
/// Init photo gallery for image selecting
327344
Future<void> _initPhotoGallery() async {
328345
LogUtils.log("[_initPhotoGallery] start");
329346

330-
// Request permission for image selecting
331-
final result = await PhotoManager.requestPermission();
332-
if (result) {
333-
// Get albums then set first album as current album
334-
_albums = await PhotoManager.getAssetPathList(type: RequestType.image);
335-
if (_albums.isNotEmpty) {
336-
final isAllAlbum = _albums.firstWhere((element) => element.isAll,
337-
orElse: () => _albums.first);
338-
setState(() {
339-
_currentAlbum = isAllAlbum;
340-
});
347+
try {
348+
// Request permission for image selecting
349+
final result = await PhotoManager.requestPermissionExtend();
350+
if (result.isAuth) {
351+
LogUtils.log('PhotoGallery permission allowed');
352+
353+
_isGalleryPermissionOK = true;
354+
355+
// Get albums then set first album as current album
356+
_albums = await PhotoManager.getAssetPathList(type: RequestType.image);
357+
if (_albums.isNotEmpty) {
358+
final isAllAlbum = _albums.firstWhere((element) => element.isAll,
359+
orElse: () => _albums.first);
360+
setState(() {
361+
_currentAlbum = isAllAlbum;
362+
});
363+
}
364+
} else {
365+
LogUtils.log('PhotoGallery permission not allowed');
341366
}
367+
} catch (e) {
368+
LogUtils.log('PhotoGallery error ${e.toString()}');
342369
}
343370
}
344371

@@ -607,9 +634,13 @@ class _ImagePickerState extends State<ImagePicker>
607634
return Stack(children: [
608635
SizedBox(height: size.height, width: size.width),
609636
if (_mode == PickerMode.Camera)
610-
Center(child: _buildCameraPreview(context))
637+
_isCameraPermissionOK
638+
? Center(child: _buildCameraPreview(context))
639+
: _buildCameraRequestPermissionView(context)
611640
else
612-
_buildAlbumPreview(context),
641+
_isGalleryPermissionOK
642+
? _buildAlbumPreview(context)
643+
: _builGalleryRequestPermissionView(context),
613644
if (_mode == PickerMode.Camera) ...[
614645
Positioned(
615646
bottom: bottomHeight.toDouble(),
@@ -778,6 +809,37 @@ class _ImagePickerState extends State<ImagePicker>
778809
: container;
779810
}
780811

812+
/// Build camera request permission view
813+
Widget _buildCameraRequestPermissionView(BuildContext context) {
814+
final size = MediaQuery.of(context).size;
815+
final bottomHeight = (widget.maxCount == 1)
816+
? (kBottomControlPanelHeight - 40)
817+
: kBottomControlPanelHeight;
818+
return SizedBox(
819+
width: size.width,
820+
height: size.height - bottomHeight,
821+
child: Column(
822+
mainAxisAlignment: MainAxisAlignment.center,
823+
children: [
824+
TextButton(
825+
style: TextButton.styleFrom(
826+
backgroundColor: Colors.grey.shade400,
827+
padding: const EdgeInsets.symmetric(horizontal: 12),
828+
shape: const RoundedRectangleBorder(
829+
borderRadius: BorderRadius.all(Radius.circular(30)),
830+
),
831+
),
832+
onPressed: _initCameraController,
833+
child: Text(_configs.textRequestPermission,
834+
style: const TextStyle(color: Colors.black)),
835+
),
836+
Text(_configs.textRequestCameraPermission,
837+
style: const TextStyle(color: Colors.grey))
838+
],
839+
),
840+
);
841+
}
842+
781843
/// Build camera preview widget.
782844
Widget _buildCameraPreview(BuildContext context) {
783845
LogUtils.log("[_buildCameraPreview] start");
@@ -859,6 +921,33 @@ class _ImagePickerState extends State<ImagePicker>
859921
});
860922
}
861923

924+
/// Build camera request permission view
925+
Widget _builGalleryRequestPermissionView(BuildContext context) {
926+
final size = MediaQuery.of(context).size;
927+
final bottomHeight = (widget.maxCount == 1)
928+
? (kBottomControlPanelHeight - 40)
929+
: kBottomControlPanelHeight;
930+
return SizedBox(
931+
width: size.width,
932+
height: size.height - bottomHeight,
933+
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
934+
TextButton(
935+
style: TextButton.styleFrom(
936+
backgroundColor: Colors.grey.shade400,
937+
padding: const EdgeInsets.symmetric(horizontal: 12),
938+
shape: const RoundedRectangleBorder(
939+
borderRadius: BorderRadius.all(Radius.circular(30)),
940+
),
941+
),
942+
onPressed: _initPhotoGallery,
943+
child: Text(_configs.textRequestPermission,
944+
style: const TextStyle(color: Colors.black)),
945+
),
946+
Text(_configs.textRequestGalleryPermission,
947+
style: const TextStyle(color: Colors.grey))
948+
]));
949+
}
950+
862951
/// Build album preview widget.
863952
Widget _buildAlbumPreview(BuildContext context) {
864953
LogUtils.log("[_buildAlbumPreview] start");
@@ -1344,9 +1433,11 @@ class _ImagePickerState extends State<ImagePicker>
13441433
groupValue: _mode,
13451434
onValueChanged: (dynamic val) async {
13461435
if (_mode != val) {
1347-
if (val == PickerMode.Camera && _cameras.isEmpty) {
1436+
if (val == PickerMode.Camera &&
1437+
(_cameras.isEmpty || !_isCameraPermissionOK)) {
13481438
await _initPhotoCapture();
1349-
} else if (val == PickerMode.Album && _albums.isEmpty) {
1439+
} else if (val == PickerMode.Album &&
1440+
(_albums.isEmpty || !_isGalleryPermissionOK)) {
13501441
await _initPhotoGallery();
13511442
}
13521443

lib/widgets/viewer/image_viewer.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,15 @@ class _ImageViewerState extends State<ImageViewer>
452452
final retImg = await ImageUtils.getImageInfo(image);
453453

454454
// Get detected objects
455-
if (_configs.labelDetectFunc != null) {
455+
if (_configs.labelDetectFunc != null &&
456+
(retImg.recognitions?.isEmpty ?? true)) {
456457
final objs = await _configs.labelDetectFunc!.call(retImg.modifiedPath);
457458
if (objs.isNotEmpty) retImg.recognitions = objs;
458459
}
459460

460461
// Get OCR from image
461-
if (_configs.ocrExtractFunc != null) {
462+
if (_configs.ocrExtractFunc != null &&
463+
(retImg.ocrText?.isEmpty ?? true)) {
462464
final text = await _configs.ocrExtractFunc!.call(retImg.modifiedPath);
463465
if (text.isNotEmpty) retImg.ocrText = text;
464466
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: advance_image_picker
22
description: Flutter plugin in pure Dart code for selecting/editing multiple images from the Android/iOS image library and taking new pictures with the camera in the same view.
3-
version: 0.1.5+1
3+
version: 0.1.5+5
44
homepage: https://freemar.vn
55
repository: https://github.com/weta-vn/advance_image_picker
66

0 commit comments

Comments
 (0)