import 'dart:io'; import 'package:camera/camera.dart'; import 'package:cathaypay_mobile/Register/register.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import '../api/api.dart'; import 'camera_card_id.dart'; import 'id_card_model.dart'; class RegisterEkycPage extends StatefulWidget { const RegisterEkycPage({Key? key}) : super(key: key); @override State createState() => _RegisterPicturePageState(); } class _RegisterPicturePageState extends State { // Ensure that plugin services are initialized so that `availableCameras()` // can be called before `runApp()` CameraController? _controller; Future? _initializeControllerFuture; var imageFront = ""; var progressLoading = false; @override void initState() { super.initState(); // To display the current output from the Camera, // create a CameraController. _controller = CameraController( // Get a specific camera from the list of available cameras. CameraApp.last, // Define the resolution to use. ResolutionPreset.medium, ); // Next, initialize the controller. This returns a Future. _initializeControllerFuture = _controller?.initialize(); } @override void dispose() { // Dispose of the controller when the widget is disposed. _controller?.dispose(); super.dispose(); } postImage(File image) { setState(() { progressLoading = true; }); print("postImage"); Api.imageUpload(Api.image_face_detect, image) .then((value) => {backToResult(value)}); } refresh() { setState(() {}); } var title = "สแกนใบหน้า"; backToResult(value) { print(value); setState(() { progressLoading = false; }); if (value != null) { if (value["face_detect"] == "True") { Navigator.pushNamed(context, '/RegisterPicturePage'); } else { title = "ไม่พบใบหน้า กรุณาลองใหม่"; } } } // Widget cameraWidget(context) { // final size = MediaQuery.of(context).size; // final deviceRatio = size.width / size.height; // return Stack(children: [ // Center( // child: Transform.scale( // scale: _controller!.value.aspectRatio / deviceRatio, // child: new AspectRatio( // aspectRatio: _controller!.value.aspectRatio, // child: new CameraPreview(_controller!), // ), // ), // ), // ]); // } Widget cameraWidget(context) { var camera = _controller!.value; // fetch screen size // final size = MediaQuery.of(context).size; final size = _getSizes(); // calculate scale depending on screen and camera ratios // this is actually size.aspectRatio / (1 / camera.aspectRatio) // because camera preview size is received as landscape // but we're calculating for portrait orientation var scale = size.aspectRatio * camera.aspectRatio; // to prevent scaling down, invert the value if (scale < 1) scale = 1 / scale; return Transform.scale( scale: scale, child: Center( child: CameraPreview(_controller!), ), ); } GlobalKey _keyRed = GlobalKey(); _getSizes() { final RenderBox renderBoxRed = _keyRed.currentContext?.findRenderObject() as RenderBox; final sizeRed = renderBoxRed.size; print("SIZE of Red: $sizeRed"); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: AppBar( backgroundColor: Colors.transparent, actions: [ IconButton( onPressed: () { Navigator.of(context).pop(); }, icon: const Icon( Icons.clear, color: Colors.grey, ), ), ], elevation: 0, centerTitle: true, title: Text( "Verify your identity".tr(), textAlign: TextAlign.center, style: GoogleFonts.kanit( color: Colors.black, fontSize: 20, ), ), ), // appBar: AppBar(title: Text('Take a picture')), // Wait until the controller is initialized before displaying the // camera preview. Use a FutureBuilder to display a loading spinner // until the controller has finished initializing. body: SafeArea( child: FutureBuilder( future: _initializeControllerFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { // If the Future is complete, display the preview. return Column( children: [ register_step(index: 3), Expanded( child: Stack( key: _keyRed, children: [ Container( width: double.infinity, child: new CameraPreview(_controller!), ), Container( width: double.infinity, color: Colors.black.withOpacity(0.5), padding: EdgeInsets.all(20), child: Text(title, textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontSize: 24), ), ), progressLoading ? Container( color: Colors.black54, child: Center( child: CircularProgressIndicator(), ), ) : Container() ], ), ), Container( margin: EdgeInsets.all(20), width: double.infinity, child: InkWell( onTap: () async { try { // Ensure that the camera is initialized. await _initializeControllerFuture; // Attempt to take a picture and get the file `image` // where it was saved. final image = await _controller!.takePicture(); setState(() { imageFront = image.path; postImage(File(imageFront)); }); // If the picture was taken, display it on a new screen. // Navigator.push( // context, // MaterialPageRoute( // builder: (context) => CameraResultPage( // // Pass the automatically generated path to // // the DisplayPictureScreen widget. // // imagePath: image?.path, // ), // ), // ); } catch (e) { // If an error occurs, log the error to the console. print(e); } }, child: Container( padding: EdgeInsets.all(5), decoration: BoxDecoration( borderRadius: BorderRadius.circular(100), color: Color(0xff9d001b), ), child: Text( "ถ่ายรูป", textAlign: TextAlign.center, style: GoogleFonts.kanit( color: Colors.white, fontSize: 20, fontWeight: FontWeight.w500, ), ), ), ), ), /*Container( padding: EdgeInsets.only(left: 10, right: 10, bottom: 20), child: Container( width: double.infinity, margin: EdgeInsets.only(top: 20, left: 20, right: 20), decoration: BoxDecoration( color: ColorCustom.primaryColor, borderRadius: BorderRadius.circular(8)), child: TextButton( onPressed: () async { // Take the Picture in a try / catch block. If anything goes wrong, // catch the error. try { // Ensure that the camera is initialized. await _initializeControllerFuture; // Attempt to take a picture and get the file `image` // where it was saved. final image = await _controller!.takePicture(); // If the picture was taken, display it on a new screen. // Navigator.push( // context, // MaterialPageRoute( // builder: (context) => CameraResultPage( // // Pass the automatically generated path to // // the DisplayPictureScreen widget. // // imagePath: image?.path, // ), // ), // ); } catch (e) { // If an error occurs, log the error to the console. print(e); } }, child: Text( 'แสกนบัตร', style: TextStyle(color: Colors.white, fontSize: 18), ), ), ), ),*/ ], ); } else { // Otherwise, display a loading indicator. return Center(child: CircularProgressIndicator()); } }, ), ), // floatingActionButton: FloatingActionButton( // child: Icon(Icons.camera_alt), // // Provide an onPressed callback. // onPressed: () async { // // Take the Picture in a try / catch block. If anything goes wrong, // // catch the error. // try { // // Ensure that the camera is initialized. // await _initializeControllerFuture; // // // Attempt to take a picture and get the file `image` // // where it was saved. // final image = await _controller.takePicture(); // // // If the picture was taken, display it on a new screen. // Navigator.push( // context, // MaterialPageRoute( // builder: (context) => DisplayPictureScreen( // // Pass the automatically generated path to // // the DisplayPictureScreen widget. // imagePath: image?.path, // ), // ), // ); // } catch (e) { // // If an error occurs, log the error to the console. // print(e); // } // }, // ), ); } }