Skip to content

how can we save documents once scanned #43

Open

Description

@baburao455

This needs a save option to phone, so will be more powerful.

Activity

  1. pritamgudekar commented on Feb 8, 2025

    @pritamgudekar

    @baburao455 That would be a good option.

    If you would like to do it independently then you need to use the path_provider plugin to access the device's storage and then write the scanned image data (usually in a format like JPEG or PNG) to a file using the dart:io library's File class; essentially, you'll get the path where to save the file, create a File object at that location, and write the image data to it.

    Following is the core part that you can do:

    import 'dart:io'; 
    import 'package:path_provider/path_provider.dart'; 
    
    Future<void> saveDocument(List<int> imageData) async {
     final directory = await getApplicationDocumentsDirectory(); 
     final filePath = '${directory.path}/scanned_document_name.jpg'; 
     final file = File(filePath);
     await file.writeAsBytes(imageData); 
    }
    
    

    This may help you to proceed further.

  2. baburao455 commented on Feb 8, 2025

    @baburao455
    Author
    Image

    hi @pritamgudekar ,

    When I checked the response of scannedDocument its a map and it doesn't have byte code to be saved as image, I think which is why I can't save it. Please correct me if am wrong. guide me to fix it or give a sample of code for scanned document to fetch image data.

    thanks again

  3. chuaz97 commented on Feb 8, 2025

    @chuaz97

    Totally agree with @baburao455
    We are unable to open that file.
    It would be very useful to have the bytes of the scanned file, hoping it's possible

    EDIT:
    I've checked whole source code and generated a copy with the addition of getting file bytes. You can use it

    flutter_doc_scanner:
    git:
    url: https://gitlab.com/dennis.turri/flutter_doc_scanner.git

    and inside your app

    Uint8List? pic = await FlutterDocScanner.scanDocument();

  4. pritamgudekar commented on Feb 9, 2025

    @pritamgudekar

    @baburao455 - I haven't used it for the image, instead I am using an api to send the file with multipart (the same way we usually sends the file data). I used the bytes and created a file which is in PDF format. Here it is the function to do it:

    // Gets the PDF from the assets
      Future<File> getFileFromAssets(String assetPath) async {
        final byteData = await rootBundle.load(assetPath);
        final file = File(assetPath);
    
        await file.writeAsBytes(byteData.buffer.asUint8List(), flush: true);
        return file;
      }
    

    But you can try it for image along with the above given. Get the file bytes and then create it on the device.

    I hope you will get some guidance from above combinations. Happy to get back from you :)

  5. baburao455 commented on Feb 9, 2025

    @baburao455
    Author

    @chuaz97 thank you so much for going extra mile, so in this case, I use your plug-in right.

    Any good tutorial to create my own plug-in, very new to flutter but trying my best to learn. Let me know if you need a hand I can try.

  6. baburao455 commented on Feb 9, 2025

    @baburao455
    Author

    @baburao455 - I haven't used it for the image, instead I am using an api to send the file with multipart (the same way we usually sends the file data). I used the bytes and created a file which is in PDF format. Here it is the function to do it:

    // Gets the PDF from the assets
      Future<File> getFileFromAssets(String assetPath) async {
        final byteData = await rootBundle.load(assetPath);
        final file = File(assetPath);
    
        await file.writeAsBytes(byteData.buffer.asUint8List(), flush: true);
        return file;
      }
    

    But you can try it for image along with the above given. Get the file bytes and then create it on the device.

    I hope you will get some guidance from above combinations. Happy to get back from you :)

    Thanks will check.

  7. baburao455 commented on Feb 9, 2025

    @baburao455
    Author

    @chuaz97 thank you so much for going extra mile, so in this case, I use your plug-in right.

    Any good tutorial to create my own plug-in, very new to flutter but trying my best to learn. Let me know if you need a hand I can try.

    @chuaz97 please also share fork access thanks

  8. chuaz97 commented on Feb 9, 2025

    @chuaz97

    @chuaz97 thank you so much for going extra mile, so in this case, I use your plug-in right.
    Any good tutorial to create my own plug-in, very new to flutter but trying my best to learn. Let me know if you need a hand I can try.

    @chuaz97 please also share fork access thanks

    I didn't fork your repo, you can access my source code here https://gitlab.com/dennis.turri/flutter_doc_scanner.git
    Mine repo code is much simpler than yours, you can check the .kt file and .swift file to see what I made to get file's bytes

  9. aleksander237 commented on Apr 11, 2025

    @aleksander237

    Hello,

    could anyone give me a "full" example of dealing with the scanned files? I'm preatty new in flutter. I'm in the point that I have my document scanned in variable scannedDocuments (like in the examples), so when I print it I see something like this:

    {pdfUri: file:///data/user/0/com.example.dge_flutter/cache/mlkit_docscan_ui_client/33019206465400.pdf, pageCount: 1}

    but when i try to create a file from this uri i'getting PathNotFoundException execption:

    File file = File(uri);

    how can I do something with this uri?

  10. chuaz97 commented on Apr 11, 2025

    @chuaz97

    Hello,

    could anyone give me a "full" example of dealing with the scanned files? I'm preatty new in flutter. I'm in the point that I have my document scanned in variable scannedDocuments (like in the examples), so when I print it I see something like this:

    {pdfUri: file:///data/user/0/com.example.dge_flutter/cache/mlkit_docscan_ui_client/33019206465400.pdf, pageCount: 1}

    but when i try to create a file from this uri i'getting PathNotFoundException execption:

    File file = File(uri);

    how can I do something with this uri?

    If you use my version posted in the comment above you can get the uint8list of the file, from there you can create a File
    Only issue is that on iOS you can scan multiple pages but only the first will be saved

  11. aleksander237 commented on Apr 11, 2025

    @aleksander237

    @chuaz97 thanks for your answer. Well, i dont really want to use a fork. So you trying to tell me, that is imposible in the "basic" version to obrain the file from this uri file:///data/user/0/com.example.dge_flutter/cache/mlkit_docscan_ui_client/33019206465400.pdf ?

  12. aleksander237 commented on Apr 11, 2025

    @aleksander237

    Ok, stupid me. I was using code like this:

    File file = File(scannedDocuments["pdfUri"]);
    List<int> fileInByte = file.readAsBytesSync();
    String fileInBase64 = base64Encode(fileInByte);
    

    and I should use code like this

    Uri uri = Uri.parse(scannedDocuments["pdfUri"]);
    File file = File.fromUri(uri);
    List<int> fileInByte = file.readAsBytesSync();
    String fileInBase64 = base64Encode(fileInByte);
    
  13. melewetwo commented on Jun 11, 2025

    @melewetwo
    Contributor

    @chuaz97 do you have any plans to create a PR in this repository from your edits on gitlab? Also your version on gitlab is missing a license, making it hard for anyone else to create a PR here from your changes.

  14. chuaz97 commented on Jun 11, 2025

    @chuaz97

    @chuaz97 do you have any plans to create a PR in this repository from your edits on gitlab? Also your version on gitlab is missing a license, making it hard for anyone else to create a PR here from your changes.

    I have no plans to create a PR. I've added a MIT License, you can do whatever you want with that code

  15. shirsh94 commented on Oct 31, 2025

    @shirsh94
    Owner

    If anyone has tested this @chuaz97 solution, please make a PR — I’d really appreciate your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions