vidvocr_flutter_plugin 1.0.1 vidvocr_flutter_plugin: ^1.0.1 copied to clipboard
VIDVOCR Flutter Plugin
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/services.dart';
import 'package:vidvocr_flutter_plugin/vidvocr_flutter_plugin.dart';
import 'package:pretty_http_logger/pretty_http_logger.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _ocrResult = 'No result yet';
final _vidvocrFlutterPlugin = VidvocrFlutterPlugin();
// Example credentials, replace with real values
final String baseURL = 'https://www.valifystage.com';
final String bundleKey = 'ad44eb94ca6747beaf99eef02407221f';
final String userName = 'mobileusername';
final String password = 'q5YT54wuJ2#mbanR';
final String clientID = 'aKM21T4hXpgHFsgNJNTKFpaq4fFpoQvuBsNWuZoQ';
final String clientSecret = 'r0tLrtxTue8c4kNmPVgaAFNGSeCWvL4oOZfBnVXoQe2Ffp5rscXXAAhX50BaZEll8ZRtr2BlgD3Nk6QLOPGtjbGXYoCBL9Fn7QCu5CsMlRKDbtwSnUAfKEG30cIv8tdW';
// Function to generate a token using the provided credentials
Future<String?> getToken() async {
final String url = '$baseURL/api/o/token/';
HttpWithMiddleware httpWithMiddleware = HttpWithMiddleware.build(middlewares: [
HttpLogger(logLevel: LogLevel.BODY),
]);
final Map<String, String> headers = {
'Content-Type': 'application/x-www-form-urlencoded',
};
final String body =
'username=$userName&password=$password&client_id=$clientID&client_secret=$clientSecret&grant_type=password';
final http.Response response = await httpWithMiddleware.post(
Uri.parse(url),
headers: headers,
body: body,
);
if (response.statusCode == 200) {
final Map<String, dynamic> jsonResponse = json.decode(response.body);
return jsonResponse['access_token'];
} else {
print('Failed to retrieve token: ${response.statusCode}');
return null;
}
}
// Function to start the SDK after generating the token
Future<void> startSDK() async {
String? token;
try {
token = await getToken();
if (token == null) {
setState(() {
_ocrResult = 'Failed to get token';
});
return;
}
} catch (e) {
setState(() {
_ocrResult = 'Error retrieving token: $e';
});
return;
}
final Map<String, dynamic> params = {
"base_url": baseURL,
"access_token": token,
"bundle_key": bundleKey,
"language": "en",
"document_verification": false,
"review_data": false,
"capture_only_mode": false,
"manual_capture_mode": true,
"preview_captured_image": true,
"primary_color": "#FF0000",
"enable_logging": true,
"collect_user_info": false, // New parameter
"advanced_confidence": false, // New parameter
"profession_analysis": false, // New parameter
"document_verification_plus": false // New parameter
};
try {
final String? result = await VidvocrFlutterPlugin.startOCR(params);
setState(() {
print(result);
_ocrResult = result ?? 'Failed to start OCR process.';
});
} on PlatformException catch (e) {
setState(() {
_ocrResult = 'Failed to start SDK: ${e.message}';
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('OCR Plugin Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: startSDK,
child: const Text('Start OCR'),
),
const SizedBox(height: 20),
Expanded(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Text(
'OCR Result: $_ocrResult\n',
textAlign: TextAlign.left, // Adjust the alignment as needed
),
),
),
],
),
),
),
);
}
}