docutain_sdk 1.0.0 docutain_sdk: ^1.0.0 copied to clipboard
The Docutain SDK for Flutter brings high quality document scanning, data extraction, text recognition and PDF creation features to your mobile apps, known from the world famous Docutain document manag [...]
import 'dart:io';
import 'package:docutain_sdk/docutain_sdk_document_datareader.dart';
import 'package:docutain_sdk/docutain_sdk_logger.dart';
import 'package:flutter/material.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
import 'package:docutain_sdk/docutain_sdk.dart';
import 'package:docutain_sdk/docutain_sdk_ui.dart';
import 'package:docutain_sdk/docutain_sdk_document.dart';
import 'package:tuple/tuple.dart';
void main() {
runApp(const MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _isDocutainPluginInitialized = false;
@override
void initState() {
// TODO: implement initState
super.initState();
initDocutainSdk();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('DocutainSDK'),
),
body: Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async {
var traceFile = await DocutainSdkLogger.getTraceFile();
OpenFile.open(traceFile?.path);
},
child: const Text('GET TRACE FILE')
),
ElevatedButton(
onPressed: () async {
//do not access sdk methods if it is not yet successfully initialized
if(!_isDocutainPluginInitialized){
//await _showMyDialog("Error", "Docutain SDK not yet initialized.");
_showSnackbar("Docutain SDK not initialized. Reason:\n${await DocutainSdk.getLastError()}");
return;
}
var scanConfig = DocumentScannerConfiguration();
scanConfig.colorConfig.colorPrimary = const Tuple2<Color, Color>(Colors.purple, Colors.purple);
scanConfig.colorConfig.colorSecondary = const Tuple2<Color, Color>(Colors.purple, Colors.purple);
scanConfig.colorConfig.colorOnSecondary = const Tuple2<Color, Color>(Colors.white, Colors.black);
scanConfig.colorConfig.colorScanButtonsLayoutBackground = const Tuple2<Color, Color>(Colors.white, Colors.black);
scanConfig.colorConfig.colorScanButtonsForeground = const Tuple2<Color, Color>(Colors.black, Colors.white);
scanConfig.colorConfig.colorScanPolygon = const Tuple2<Color, Color>(Colors.purple, Colors.purple);
scanConfig.colorConfig.colorBottomBarBackground = const Tuple2<Color, Color>(Colors.purple, Colors.black);
scanConfig.colorConfig.colorBottomBarForeground = const Tuple2<Color, Color>(Colors.white, Colors.white);
scanConfig.colorConfig.colorTopBarBackground = const Tuple2<Color, Color>(Colors.purple, Colors.black);
scanConfig.colorConfig.colorTopBarForeground = const Tuple2<Color, Color>(Colors.white, Colors.white);
//optionally enable switching capture mode
scanConfig.allowCaptureModeSetting = true;
//start the document scanner and wait for the result
//true if user finished the scan process successfully, false if user canceled
bool rcScan = await DocutainSdkUi.scanDocument(scanConfig);
if(!rcScan){
_showSnackbar("User canceled scan process.");
return;
}
//get the page count of the currently scanned document
final pageCount = await DocutainSdkDocument.pageCount();
//get the detected text of the currently scanned document, if any available
final text = await DocutainSdkDocumentDataReader.getText();
//get the detected data of the currently scanned document, if any available
final data = await DocutainSdkDocumentDataReader.analyze();
//get the application documents directory where we want to save the pdf or image file
final directory = await getApplicationDocumentsDirectory();
//generate the pdf from the scanned document
//returns the file if pdf was successfully generated, returns null if pdf creation failed
File? pdfFile = await DocutainSdkDocument.writePDF(
directory.path, "testPDF");
if (pdfFile != null) {
//open the generated pdf file for demonstration purposes
OpenFile.open(pdfFile.path);
} else{
//pdf creation failed, get the last error
_showSnackbar("PDF creation failed. Reason:\n${await DocutainSdk.getLastError()}");
}
},
child: const Text('START SCAN'))
]
)
),
);
}
void initDocutainSdk() async {
//initialize the sdk with a sample key
bool isDocutainPluginInitialized = await DocutainSdk.initSDK(
"YOUR_LICENSE_KEY");
if(!isDocutainPluginInitialized){
//get the last error message
String error = await DocutainSdk.getLastError();
//implement handling to avoid accessing Docutain SDK when it is not initialized
}
var analyzeConfig = AnalyzeConfiguration();
analyzeConfig.readBIC = true;
analyzeConfig.readPaymentState = true;
if(!await DocutainSdkDocumentDataReader.setAnalyzeConfiguration(analyzeConfig)){
//get the last error message
String error = await DocutainSdk.getLastError();
}
//set the log level depending on your needs
await DocutainSdkLogger.setLogLevel(Level.verbose);
if (!mounted) return;
setState(() {
_isDocutainPluginInitialized = isDocutainPluginInitialized;
});
}
void _showSnackbar(String message){
SnackBar snackBar = SnackBar(
content: Text(message),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}