From 4ac90a6d4015333e86fda74b4fba6b1017d3e890 Mon Sep 17 00:00:00 2001 From: faithoflifedev Date: Mon, 2 Sep 2024 11:29:37 -0400 Subject: [PATCH] singleton --- packages/google_vision/CHANGELOG.md | 8 ++ packages/google_vision/README.md | 32 ++++--- .../example/document_text_detection_file.dart | 2 +- .../example/doument_text_detection.dart | 2 +- packages/google_vision/example/example.dart | 2 +- .../google_vision/example/face_detection.dart | 5 +- .../example/label_detection.dart | 4 +- .../example/landmark_detection.dart | 2 +- .../google_vision/example/logo_detection.dart | 2 +- .../google_vision/example/text_detection.dart | 2 +- .../google_vision/example/web_detection.dart | 2 +- packages/google_vision/lib/google_vision.dart | 1 - packages/google_vision/lib/meta.dart | 2 +- .../lib/src/cmd/vision_crop_hint_command.dart | 2 +- .../lib/src/cmd/vision_helper_command.dart | 2 +- .../src/cmd/vision_safe_search_command.dart | 2 +- .../lib/src/google_vision_base.dart | 85 ++++++++++--------- .../lib/src/util/logging_interceptors.dart | 48 ----------- packages/google_vision/pubspec.yaml | 5 +- packages/google_vision/tool/README.md | 30 ++++--- 20 files changed, 112 insertions(+), 128 deletions(-) delete mode 100644 packages/google_vision/lib/src/util/logging_interceptors.dart diff --git a/packages/google_vision/CHANGELOG.md b/packages/google_vision/CHANGELOG.md index edbbce6..f65e9bd 100644 --- a/packages/google_vision/CHANGELOG.md +++ b/packages/google_vision/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 1.4.0 + +* switch to Singleton + +## 1.3.0+4 + +* switch to Singleton + ## 1.3.0+4 * readme update diff --git a/packages/google_vision/README.md b/packages/google_vision/README.md index a8a41f4..d07d9cc 100644 --- a/packages/google_vision/README.md +++ b/packages/google_vision/README.md @@ -4,17 +4,19 @@ [![pub package](https://img.shields.io/pub/v/google_vision.svg)](https://pub.dartlang.org/packages/google_vision) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) -Native [Dart](https://dart.dev/) package that integrates Google Vision features, including image labeling, face, logo, and landmark detection, optical character recognition (OCR), and detection of explicit content, into applications. +Native [Dart](https://dart.dev/) package that integrates Google Vision features, including image labeling, face, logo, and landmark detection, optical character recognition (OCR), and detection of explicit content, into your applications. + +**If you are looking at integrating the Google Vision API into your `Flutter` SDK application then you might want to take a look at my related package [google_vision_flutter](https://pub.dev/packages/google_vision_flutter), which provides a widget that wraps the functionality provided by this `Dart` SDK focussed package.** - [Google Vision Images REST API Client](#google-vision-images-rest-api-client) - [Project Status](#project-status) - [Recent Changes](#recent-changes) + - [New for v1.4.0](#new-for-v140) + - [New for v1.3.0](#new-for-v130) - [New for v1.2.0](#new-for-v120) - - [New for v1.0.8](#new-for-v108) - - [New for v1.0.7](#new-for-v107) - [Getting Started](#getting-started) - [pubspec.yaml](#pubspecyaml) - - [Obtaining Authorization Credentials](#obtaining-authorization-credentials) + - [Obtaining Authentication/Authorization Credentials](#obtaining-authenticationauthorization-credentials) - [Usage of the Cloud Vision API](#usage-of-the-cloud-vision-api) - [New Helper Methods](#new-helper-methods) - [Usage with Flutter](#usage-with-flutter) @@ -23,8 +25,8 @@ Native [Dart](https://dart.dev/) package that integrates Google Vision features, - [Contributors](#contributors) - [Contributing](#contributing) -## Project Status +## Project Status [![Build Status](https://github.com/faithoflifedev/google_vision/workflows/Dart/badge.svg)](https://github.com/faithoflifedev/google_vision/actions) [![github last commit](https://shields.io/github/last-commit/faithoflifedev/google_vision)](https://shields.io/github/last-commit/faithoflifedev/google_vision) [![github build](https://img.shields.io/github/actions/workflow/status/faithoflifedev/google_vision_workspace/dart.yaml?branch=main)](https://shields.io/github/workflow/status/faithoflifedev/google_vision/Dart) [![github issues](https://shields.io/github/issues/faithoflifedev/google_vision)](https://shields.io/github/issues/faithoflifedev/google_vision) @@ -34,6 +36,17 @@ Please feel free to submit PRs for any additional helper methods, or report an [ ## Recent Changes +### New for v1.4.0 + - A breaking change from the previous version is that the `GoogleVision` class now follows the Singleton design pattern. Now the object is instantiated as follows: +```dart + +// Old method from v1.3.x and earlier +// final googleVision = await GoogleVision.withJwtFile('service_credentials.json'); + +// New +final googleVision = await GoogleVision().withJwtFile('service_credentials.json'); +``` + ### New for v1.3.0 - This version of the package supports both the `image` and `file` annotation APIs for Google Vision. The previous versions of the package supported only the `image` API. - A number of methods and classes have been **Deprecated** in this version. All the provided examples still work without any changes, so the changes in this package should not cause any issue to existing code. @@ -41,10 +54,7 @@ Please feel free to submit PRs for any additional helper methods, or report an [ ### New for v1.2.0 - helper methods that simplify any `single` detection so a simple face detection can be performed with the `faceDetection(JsonImage jsonImage)` method, see the table below. - -### New for v1.0.8 - - web entities and pages detection [https://cloud.google.com/vision/docs/detecting-web](https://cloud.google.com/vision/docs/detecting-web), provides urls of web pages that match the specified image - + ## Getting Started ### pubspec.yaml @@ -54,7 +64,7 @@ To use this package, add the dependency to your `pubspec.yaml` file: ```yaml dependencies: ... - google_vision: ^1.3.0+4 + google_vision: ^1.4.0 ``` ### Obtaining Authentication/Authorization Credentials @@ -68,7 +78,7 @@ Both of the authorization/authentication methods listed above assume that you al ### Usage of the Cloud Vision API ```dart -final googleVision = await GoogleVision.withApiKey( +final googleVision = await GoogleVision().withApiKey( Platform.environment['GOOGLE_VISION_API_KEY'] ?? '[YOUR API KEY]', // additionalHeaders: {'com.xxx.xxx': 'X-Ios-Bundle-Identifier'}, ); diff --git a/packages/google_vision/example/document_text_detection_file.dart b/packages/google_vision/example/document_text_detection_file.dart index c59c7ea..f36dfb5 100644 --- a/packages/google_vision/example/document_text_detection_file.dart +++ b/packages/google_vision/example/document_text_detection_file.dart @@ -3,7 +3,7 @@ import 'package:universal_io/io.dart'; void main() async { final googleVision = - await GoogleVision.withJwtFile('service_credentials.json'); + await GoogleVision().withJwtFile('service_credentials.json'); print('checking...'); diff --git a/packages/google_vision/example/doument_text_detection.dart b/packages/google_vision/example/doument_text_detection.dart index 83be48b..4f17a64 100644 --- a/packages/google_vision/example/doument_text_detection.dart +++ b/packages/google_vision/example/doument_text_detection.dart @@ -2,7 +2,7 @@ import 'package:google_vision/google_vision.dart'; void main() async { final googleVision = - await GoogleVision.withJwtFile('service_credentials.json'); + await GoogleVision().withJwtFile('service_credentials.json'); print('checking...'); diff --git a/packages/google_vision/example/example.dart b/packages/google_vision/example/example.dart index 499ecd9..b8bf56f 100644 --- a/packages/google_vision/example/example.dart +++ b/packages/google_vision/example/example.dart @@ -2,7 +2,7 @@ import 'package:google_vision/google_vision.dart'; void main() async { final googleVision = - await GoogleVision.withJwtFile('service_credentials.json'); + await GoogleVision().withJwtFile('service_credentials.json'); final requests = AnnotationRequests(requests: [ AnnotationRequest( diff --git a/packages/google_vision/example/face_detection.dart b/packages/google_vision/example/face_detection.dart index c90a0b3..65c4469 100644 --- a/packages/google_vision/example/face_detection.dart +++ b/packages/google_vision/example/face_detection.dart @@ -2,10 +2,7 @@ import 'package:google_vision/google_vision.dart'; import 'package:universal_io/io.dart'; void main() async { - // final googleVision = - // await GoogleVision.withJwtFile('service_credentials.json'); - - final googleVision = GoogleVision.withApiKey( + final googleVision = GoogleVision().withApiKey( Platform.environment['GOOGLE_VISION_API_KEY'] ?? '[YOUR API KEY]', // additionalHeaders: {'com.xxx.xxx': 'X-Ios-Bundle-Identifier'}, ); diff --git a/packages/google_vision/example/label_detection.dart b/packages/google_vision/example/label_detection.dart index 4d1e42c..f65cf94 100644 --- a/packages/google_vision/example/label_detection.dart +++ b/packages/google_vision/example/label_detection.dart @@ -2,7 +2,7 @@ import 'package:google_vision/google_vision.dart'; void main() async { final googleVision = - await GoogleVision.withJwtFile('service_credentials.json'); + await GoogleVision().withJwtFile('service_credentials.json'); print('checking...'); @@ -13,7 +13,7 @@ void main() async { for (var entityAnnotation in entityAnnotations) { print('Description: - ${entityAnnotation.description}'); - print('Score: - ${entityAnnotation.boundingPoly?.normalizedVertices}'); + print('Score: - ${entityAnnotation.score}'); } print('done.'); diff --git a/packages/google_vision/example/landmark_detection.dart b/packages/google_vision/example/landmark_detection.dart index 9132c33..2f4d4a6 100644 --- a/packages/google_vision/example/landmark_detection.dart +++ b/packages/google_vision/example/landmark_detection.dart @@ -2,7 +2,7 @@ import 'package:google_vision/google_vision.dart'; void main() async { final googleVision = - await GoogleVision.withJwtFile('service_credentials.json'); + await GoogleVision().withJwtFile('service_credentials.json'); print('checking...'); diff --git a/packages/google_vision/example/logo_detection.dart b/packages/google_vision/example/logo_detection.dart index b453a49..f7f17ec 100644 --- a/packages/google_vision/example/logo_detection.dart +++ b/packages/google_vision/example/logo_detection.dart @@ -2,7 +2,7 @@ import 'package:google_vision/google_vision.dart'; void main() async { final googleVision = - await GoogleVision.withJwtFile('service_credentials.json'); + await GoogleVision().withJwtFile('service_credentials.json'); print('checking...'); diff --git a/packages/google_vision/example/text_detection.dart b/packages/google_vision/example/text_detection.dart index 29eca7f..5777e56 100644 --- a/packages/google_vision/example/text_detection.dart +++ b/packages/google_vision/example/text_detection.dart @@ -3,7 +3,7 @@ import 'package:universal_io/io.dart'; void main() async { final googleVision = - await GoogleVision.withJwtFile('service_credentials.json'); + await GoogleVision().withJwtFile('service_credentials.json'); final imageFile = File('sample_image/structures.png').readAsBytesSync(); diff --git a/packages/google_vision/example/web_detection.dart b/packages/google_vision/example/web_detection.dart index 7a93210..a07620c 100644 --- a/packages/google_vision/example/web_detection.dart +++ b/packages/google_vision/example/web_detection.dart @@ -3,7 +3,7 @@ import 'package:universal_io/io.dart'; void main() async { final googleVision = - await GoogleVision.withJwtFile('service_credentials.json'); + await GoogleVision().withJwtFile('service_credentials.json'); final imageFile = File('sample_image/structures.png').readAsBytesSync(); diff --git a/packages/google_vision/lib/google_vision.dart b/packages/google_vision/lib/google_vision.dart index c7959af..dfe3c2d 100644 --- a/packages/google_vision/lib/google_vision.dart +++ b/packages/google_vision/lib/google_vision.dart @@ -74,6 +74,5 @@ export 'src/provider/files.dart'; export 'src/provider/images.dart'; export 'src/provider/oauth.dart'; -export 'src/util/logging_interceptors.dart'; export 'src/util/serializable_image.dart'; export 'src/util/util.dart'; diff --git a/packages/google_vision/lib/meta.dart b/packages/google_vision/lib/meta.dart index c9d0049..0c42dbf 100644 --- a/packages/google_vision/lib/meta.dart +++ b/packages/google_vision/lib/meta.dart @@ -6,4 +6,4 @@ library meta; import 'dart:convert' show json; final pubSpec = json.decode( - '{"name":"google_vision","version":"1.3.0+4","homepage":"https://github.com/faithoflifedev/google_vision/tree/main/packages/google_vision","environment":{"sdk":">=3.2.0 <4.0.0"},"description":"Allows you to add Google Visions image labeling, face, logo, and landmark detection, OCR, and detection of explicit content, into cross platform applications.","dependencies":{"args":"^2.5.0","collection":"^1.18.0","crypto_keys_plus":"^0.4.0","dio":"^5.6.0","http":"^1.2.2","image":"^4.1.7","jose_plus":"^0.4.6","json_annotation":"^4.9.0","loggy":"^2.0.3","mime":"^1.0.6","retrofit":"^4.2.0","universal_io":"^2.2.2"},"dev_dependencies":{"build_runner":"^2.4.11","grinder":"^0.9.5","json_serializable":"^6.8.0","lints":"^4.0.0","publish_tools":"^1.0.0+4","retrofit_generator":"^8.2.0"},"executables":{"vision":""},"repository":"https://github.com/faithoflifedev/google_vision","funding":["https://www.buymeacoffee.com/faithoflif2"]}'); + '{"name":"google_vision","version":"1.4.0","homepage":"https://github.com/faithoflifedev/google_vision/tree/main/packages/google_vision","environment":{"sdk":">=3.2.0 <4.0.0"},"description":"Allows you to add Google Visions image labeling, face, logo, and landmark detection, OCR, and detection of explicit content, into cross platform applications.","dependencies":{"args":"^2.5.0","collection":"^1.18.0","crypto_keys_plus":"^0.4.0","dio":"^5.6.0","http":"^1.2.2","image":"^4.1.7","jose_plus":"^0.4.6","json_annotation":"^4.9.0","mime":"^1.0.6","retrofit":"^4.2.0","universal_io":"^2.2.2"},"dev_dependencies":{"build_runner":"^2.4.11","grinder":"^0.9.5","json_serializable":"^6.8.0","lints":"^4.0.0","publish_tools":"^1.0.0+4","retrofit_generator":"^8.2.1"},"executables":{"vision":""},"repository":"https://github.com/faithoflifedev/google_vision","funding":["https://www.buymeacoffee.com/faithoflif2"]}'); diff --git a/packages/google_vision/lib/src/cmd/vision_crop_hint_command.dart b/packages/google_vision/lib/src/cmd/vision_crop_hint_command.dart index 85c3fab..ea2b0e4 100644 --- a/packages/google_vision/lib/src/cmd/vision_crop_hint_command.dart +++ b/packages/google_vision/lib/src/cmd/vision_crop_hint_command.dart @@ -25,7 +25,7 @@ class VisionCropHintCommand extends VisionHelper { @override void run() async { - final googleVision = await GoogleVision.withJwtFile( + final googleVision = await GoogleVision().withJwtFile( globalResults!['credential-file'], 'https://www.googleapis.com/auth/cloud-vision'); diff --git a/packages/google_vision/lib/src/cmd/vision_helper_command.dart b/packages/google_vision/lib/src/cmd/vision_helper_command.dart index d04b907..4827d84 100644 --- a/packages/google_vision/lib/src/cmd/vision_helper_command.dart +++ b/packages/google_vision/lib/src/cmd/vision_helper_command.dart @@ -24,7 +24,7 @@ abstract class VisionHelper extends Command { Future initializeGoogleVision() async { _googleVision = - await GoogleVision.withJwtFile(globalResults!['credential-file']); + await GoogleVision().withJwtFile(globalResults!['credential-file']); pages = (argResults!['pages'] as String?)?.split(',').map(int.parse).toList(); diff --git a/packages/google_vision/lib/src/cmd/vision_safe_search_command.dart b/packages/google_vision/lib/src/cmd/vision_safe_search_command.dart index dd9960b..8bb9820 100644 --- a/packages/google_vision/lib/src/cmd/vision_safe_search_command.dart +++ b/packages/google_vision/lib/src/cmd/vision_safe_search_command.dart @@ -22,7 +22,7 @@ class VisionSafeSearchCommand extends VisionHelper { @override void run() async { - final googleVision = await GoogleVision.withJwtFile( + final googleVision = await GoogleVision().withJwtFile( globalResults!['credential-file'], 'https://www.googleapis.com/auth/cloud-vision'); diff --git a/packages/google_vision/lib/src/google_vision_base.dart b/packages/google_vision/lib/src/google_vision_base.dart index c6618bf..b576941 100644 --- a/packages/google_vision/lib/src/google_vision_base.dart +++ b/packages/google_vision/lib/src/google_vision_base.dart @@ -2,49 +2,48 @@ import 'dart:io'; import 'package:dio/dio.dart'; import 'package:google_vision/google_vision.dart'; -import 'package:loggy/loggy.dart'; /// Integrates Google Vision features, including painter labeling, face, logo, /// and landmark detection, optical character recognition (OCR), and detection /// of explicit content, into applications. -class GoogleVision with UiLoggy { - static final dio = Dio(); - static final DateTime tokenExpiry = DateTime(2010, 0, 0); - static final accept = 'application/json'; - static final contentType = 'application/json; charset=UTF-8'; +class GoogleVision { + static final GoogleVision _instance = GoogleVision._internal(); - static String? _apiKey; - static String? _token; - static TokenGenerator? tokenGenerator; + final dio = Dio(); + final tokenExpiry = DateTime(2010, 0, 0); - static final _imagesClient = ImagesClient(dio); - final _filesClient = FilesClient(dio); + late final _imagesClient = ImagesClient(dio); + late final _filesClient = FilesClient(dio); + + static const accept = 'application/json'; + static const contentType = 'application/json; charset=UTF-8'; + + TokenGenerator? tokenGenerator; + String? _apiKey; + String? _token; GoogleVisionImage get image => GoogleVisionImage(this, _imagesClient); GoogleVisionFile get file => GoogleVisionFile(this, _filesClient); - GoogleVision() { - Loggy.initLoggy( - logPrinter: const PrettyPrinter(), - logOptions: LogOptions(LogLevel.off), - ); + set apiKey(String apiKey) => _apiKey = apiKey; - GoogleVision.dio.interceptors.add(LoggingInterceptors()); - } + // Private constructor + GoogleVision._internal(); + + factory GoogleVision() => _instance; void setAuthHeader() { if (_token != null) { - GoogleVision.dio.options.headers[HttpHeaders.authorizationHeader] = - 'Bearer $_token'; + dio.options.headers[HttpHeaders.authorizationHeader] = 'Bearer $_token'; } if (_apiKey != null) { - GoogleVision.dio.options.queryParameters['key'] = _apiKey; + dio.options.queryParameters['key'] = _apiKey; } } - static Future _confirmToken() async { + Future confirmToken() async { if (tokenGenerator == null) { throw Exception(); } else { @@ -59,52 +58,62 @@ class GoogleVision with UiLoggy { } /// Authenticate using an API key. - static GoogleVision withApiKey( + GoogleVision withApiKey( String apiKey, { Map? additionalHeaders, }) { - _apiKey = apiKey; + this.apiKey = apiKey; if (additionalHeaders != null) { dio.options.headers.addAll(additionalHeaders); } - return GoogleVision(); + return this; } /// Authenticate using the supplied token generator - static Future withGenerator(TokenGenerator generator) async { + Future withGenerator(TokenGenerator generator) async { final googleVision = GoogleVision(); - GoogleVision.tokenGenerator = generator; + googleVision.tokenGenerator = generator; - await _confirmToken(); + await googleVision.confirmToken(); return googleVision; } /// Authenticated with JWT. - static Future withJwt(String credentials, - [String scope = 'https://www.googleapis.com/auth/cloud-platform']) async { + Future withJwt( + String credentials, [ + String scope = 'https://www.googleapis.com/auth/cloud-platform', + ]) async { GoogleVision googleVision = GoogleVision(); - tokenGenerator = - JwtGenerator(credentials: credentials, scope: scope, dio: dio); + googleVision.tokenGenerator = JwtGenerator( + credentials: credentials, + scope: scope, + dio: googleVision.dio, + ); - await _confirmToken(); + await googleVision.confirmToken(); return googleVision; } /// Authenticated with JWT. - static Future withJwtFile(String credentialsFileName, - [String scope = 'https://www.googleapis.com/auth/cloud-platform']) async { + Future withJwtFile( + String credentialsFileName, [ + String scope = 'https://www.googleapis.com/auth/cloud-platform', + ]) async { GoogleVision googleVision = GoogleVision(); - tokenGenerator = JwtGenerator.fromFile( - credentialsFile: credentialsFileName, scope: scope, dio: dio); + googleVision.tokenGenerator = JwtGenerator.fromFile( + credentialsFile: credentialsFileName, + scope: scope, + dio: googleVision.dio, + ); - await _confirmToken(); + await googleVision.confirmToken(); return googleVision; } diff --git a/packages/google_vision/lib/src/util/logging_interceptors.dart b/packages/google_vision/lib/src/util/logging_interceptors.dart deleted file mode 100644 index ba5b452..0000000 --- a/packages/google_vision/lib/src/util/logging_interceptors.dart +++ /dev/null @@ -1,48 +0,0 @@ -import 'dart:async'; -import 'dart:typed_data'; - -import 'package:dio/dio.dart'; -import 'package:loggy/loggy.dart'; - -/// Dio instance may have one or more interceptors by which you can intercept -/// requests/responses/errors before they are handled by `then` or `catchError`. -class LoggingInterceptors extends Interceptor with UiLoggy { - /// The callback will be executed before the request is initiated. - @override - FutureOr onRequest( - RequestOptions options, - RequestInterceptorHandler handler, - ) { - loggy.debug('\nURI: ${options.uri}'); - - loggy.debug('\nREQUEST:\n${options.data}'); - - super.onRequest(options, handler); - } - - /// The callback will be executed on error. - @override - FutureOr onError( - DioException err, - ErrorInterceptorHandler handler, - ) { - loggy.error('\nERROR:\n$err'); - - handler.next(err); - } - - /// The callback will be executed on success. - @override - FutureOr onResponse( - Response response, - ResponseInterceptorHandler handler, - ) { - if (response.data is! Uint8List) { - loggy.debug('\nRESPONSE:\n${response.data}'); - } else { - loggy.debug('\nRESPONSE:\n'); - } - - super.onResponse(response, handler); - } -} diff --git a/packages/google_vision/pubspec.yaml b/packages/google_vision/pubspec.yaml index 431f6a1..9ff5944 100644 --- a/packages/google_vision/pubspec.yaml +++ b/packages/google_vision/pubspec.yaml @@ -1,6 +1,6 @@ name: google_vision description: Allows you to add Google Visions image labeling, face, logo, and landmark detection, OCR, and detection of explicit content, into cross platform applications. -version: 1.3.0+4 +version: 1.4.0 repository: https://github.com/faithoflifedev/google_vision homepage: https://github.com/faithoflifedev/google_vision/tree/main/packages/google_vision @@ -19,7 +19,6 @@ dependencies: image: ^4.1.7 jose_plus: ^0.4.6 json_annotation: ^4.9.0 - loggy: ^2.0.3 mime: ^1.0.6 retrofit: ^4.2.0 universal_io: ^2.2.2 @@ -30,7 +29,7 @@ dev_dependencies: json_serializable: ^6.8.0 lints: ^4.0.0 publish_tools: ^1.0.0+4 - retrofit_generator: ^8.2.0 + retrofit_generator: ^8.2.1 executables: vision: diff --git a/packages/google_vision/tool/README.md b/packages/google_vision/tool/README.md index 9a34b12..2aae7c4 100644 --- a/packages/google_vision/tool/README.md +++ b/packages/google_vision/tool/README.md @@ -4,17 +4,19 @@ [![pub package](https://img.shields.io/pub/v/google_vision.svg)](https://pub.dartlang.org/packages/google_vision) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) -Native [Dart](https://dart.dev/) package that integrates Google Vision features, including image labeling, face, logo, and landmark detection, optical character recognition (OCR), and detection of explicit content, into applications. +Native [Dart](https://dart.dev/) package that integrates Google Vision features, including image labeling, face, logo, and landmark detection, optical character recognition (OCR), and detection of explicit content, into your applications. + +**If you are looking at integrating the Google Vision API into your `Flutter` SDK application then you might want to take a look at my related package [google_vision_flutter](https://pub.dev/packages/google_vision_flutter), which provides a widget that wraps the functionality provided by this `Dart` SDK focussed package.** - [Google Vision Images REST API Client](#google-vision-images-rest-api-client) - [Project Status](#project-status) - [Recent Changes](#recent-changes) + - [New for v1.4.0](#new-for-v140) + - [New for v1.3.0](#new-for-v130) - [New for v1.2.0](#new-for-v120) - - [New for v1.0.8](#new-for-v108) - - [New for v1.0.7](#new-for-v107) - [Getting Started](#getting-started) - [pubspec.yaml](#pubspecyaml) - - [Obtaining Authorization Credentials](#obtaining-authorization-credentials) + - [Obtaining Authentication/Authorization Credentials](#obtaining-authenticationauthorization-credentials) - [Usage of the Cloud Vision API](#usage-of-the-cloud-vision-api) - [New Helper Methods](#new-helper-methods) - [Usage with Flutter](#usage-with-flutter) @@ -23,8 +25,8 @@ Native [Dart](https://dart.dev/) package that integrates Google Vision features, - [Contributors](#contributors) - [Contributing](#contributing) -## Project Status +## Project Status [![Build Status](https://github.com/faithoflifedev/google_vision/workflows/Dart/badge.svg)](https://github.com/faithoflifedev/google_vision/actions) [![github last commit](https://shields.io/github/last-commit/faithoflifedev/google_vision)](https://shields.io/github/last-commit/faithoflifedev/google_vision) [![github build](https://img.shields.io/github/actions/workflow/status/faithoflifedev/google_vision_workspace/dart.yaml?branch=main)](https://shields.io/github/workflow/status/faithoflifedev/google_vision/Dart) [![github issues](https://shields.io/github/issues/faithoflifedev/google_vision)](https://shields.io/github/issues/faithoflifedev/google_vision) @@ -34,6 +36,17 @@ Please feel free to submit PRs for any additional helper methods, or report an [ ## Recent Changes +### New for v1.4.0 + - A breaking change from the previous version is that the `GoogleVision` class now follows the Singleton design pattern. Now the object is instantiated as follows: +```dart + +// Old method from v1.3.x and earlier +// final googleVision = await GoogleVision.withJwtFile('service_credentials.json'); + +// New +final googleVision = await GoogleVision().withJwtFile('service_credentials.json'); +``` + ### New for v1.3.0 - This version of the package supports both the `image` and `file` annotation APIs for Google Vision. The previous versions of the package supported only the `image` API. - A number of methods and classes have been **Deprecated** in this version. All the provided examples still work without any changes, so the changes in this package should not cause any issue to existing code. @@ -41,10 +54,7 @@ Please feel free to submit PRs for any additional helper methods, or report an [ ### New for v1.2.0 - helper methods that simplify any `single` detection so a simple face detection can be performed with the `faceDetection(JsonImage jsonImage)` method, see the table below. - -### New for v1.0.8 - - web entities and pages detection [https://cloud.google.com/vision/docs/detecting-web](https://cloud.google.com/vision/docs/detecting-web), provides urls of web pages that match the specified image - + ## Getting Started ### pubspec.yaml @@ -68,7 +78,7 @@ Both of the authorization/authentication methods listed above assume that you al ### Usage of the Cloud Vision API ```dart -final googleVision = await GoogleVision.withApiKey( +final googleVision = await GoogleVision().withApiKey( Platform.environment['GOOGLE_VISION_API_KEY'] ?? '[YOUR API KEY]', // additionalHeaders: {'com.xxx.xxx': 'X-Ios-Bundle-Identifier'}, );