Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

c# - How can I scan a document using ASP.net MVC 5 with the help of Twain

Please help me out by sharing the step by step procedure to achieve the scanning functionality using Twain in ASP.Net MVC5. Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solution is here:

  1. In ASP.Net/Core Project you send message to call winform project:

            var start = function () {
            var i = 0;
            var wsImpl = window.WebSocket || window.MozWebSocket;
            window.ws = new wsImpl('ws://localhost:8181/');
            ws.onmessage = function (e) {
                $('#submit').hide();
                $('#scanBtn').hide();
                $('.loader').show();
                if (typeof e.data === "string") {
                    //IF Received Data is String
                }
                else if (e.data instanceof ArrayBuffer) {
                    //IF Received Data is ArrayBuffer
                }
                else if (e.data instanceof Blob) {
                    i++;
                    var f = e.data;
                    f.name = "File" + i;
                    storedFiles.push(f);
                    formdata.append(f.name, f);
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var html = "<div class="col-sm-2 text-center" 
                    style="border: 1px solid black; margin-left: 2px;"><img 
                    height="200px" width="200px" src="" + e.target.result + "" 
                    data-file='" + f.name + "' class='selFile' title='Click to 
                    remove'><br/>" + i + "</div>";
                        selDiv.append(html);
                        $('#submit').show();
                        $('#scanBtn').show();
                        $('.loader').hide();
                    }
                    reader.readAsDataURL(f);
                }
            };
            ws.onopen = function () {
                //Do whatever u want when connected succesfully
            };
            ws.onclose = function () {
                $('.dalert').modal('show');
            };
        }
        window.onload = start;
        function scanImage() {
            ws.send("1100");
        };
    

https://javascript.info/websocket

  1. In Winforms Project you scan document and send graphic data back to Asp.Net/Core project:

    public partial class Form1 : Form { ImageCodecInfo _tiffCodecInfo; TwainSession _twain; bool _stopScan; bool _loadingCaps; List allSockets; WebSocketServer server; public Form1() { InitializeComponent();

     if (NTwain.PlatformInfo.Current.IsApp64Bit)
    {
        Text = Text + " (64bit)";
    }
    else
    {
        Text = Text + " (32bit)";
    }
    foreach (var enc in ImageCodecInfo.GetImageEncoders())
    {
        if (enc.MimeType == "image/tiff") { _tiffCodecInfo = enc; break; }
    }
    
    this.WindowState = FormWindowState.Minimized;
    this.ShowInTaskbar = false;
    
    allSockets = new List<IWebSocketConnection>();
    server = new WebSocketServer("ws://0.0.0.0:8181");
    server.Start(socket =>
    {
        socket.OnOpen = () =>
        {
            Console.WriteLine("Open!");
            allSockets.Add(socket);
        };
        socket.OnClose = () =>
        {
            Console.WriteLine("Close!");
            allSockets.Remove(socket);
        };
        socket.OnMessage = message =>
        {
            if (message == "1100")
            {
                this.Invoke(new Action(()=> {
                    this.WindowState = FormWindowState.Normal;
                }));
            }
        };
    });
    

    }

Link to project.

https://github.com/mgriit/ScanAppForWeb

You can remake this project, as you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...