如何把文檔掃描保存到Google Drive中

咱們有時候須要使用掃描儀來把紙質文檔轉換成電子文檔用於保存。這篇文章介紹如何建立一個簡單的應用,把文件掃描成圖片,保存到Google的雲服務中。html

參考原文:git

How to Upload Scanned Images to Google Drive With Dynamic .NET TWAIN


準備工做

  1. 下載安裝 Dynamic .NET TWAIN SDKgithub

  2. 若是沒有掃描儀,能夠下載安裝虛擬掃描儀。 c#

掃描文件

在Visual Studio中建立一個WinForms的工程。
api

在引用中添加DynamicDotNetTWAIN.dllui

建立界面:左側的顯示區域是DynamicDotNetTWAIN控件。
google

獲取設備:spa

dynamicDotNetTwain.OpenSourceManager();
for (lngNum = 0; lngNum < dynamicDotNetTwain.SourceCount; lngNum++)
{     
   cmbSource.Items.Add(dynamicDotNetTwain.SourceNameItems(Convert.ToInt16(lngNum)));
}

獲取圖像,顯示在控件中:
code

try
            {
                dynamicDotNetTwain.SelectSourceByIndex(Convert.ToInt16(cmbSource.SelectedIndex));
                dynamicDotNetTwain.IfShowUI = false;
                dynamicDotNetTwain.OpenSource();
                dynamicDotNetTwain.IfDisableSourceAfterAcquire = true;
                dynamicDotNetTwain.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT_RGB;
                dynamicDotNetTwain.BitDepth = 24;
                dynamicDotNetTwain.Resolution = 300;
                dynamicDotNetTwain.AcquireImage();
            }
            catch (Dynamsoft.DotNet.TWAIN.TwainException exp)
            {
                String errorstr = "";
                errorstr += "Error " + exp.Code + "\r\n" + "Description: " + exp.Message + "\r\nPosition: " + exp.TargetSite + "\r\nHelp: " + exp.HelpLink + "\r\n";
                MessageBox.Show(errorstr);
            }

上傳到Google Drive

在Google開發者控制中心建立一個新的工程。orm

啓用Drive API。

選擇Credentials來建立一個新的客戶端ID。

在Visual Studio中安裝NuGet Package Manager (Tools -> Extensions and Updates)

運行Package Manager Console,輸入Install-Package Google.Apis.Drive.v2來安裝Drive API NuGet package。成功以後引用中多了Google的部分。

添加上傳代碼:

Image image = dynamicDotNetTwain.GetImage(dynamicDotNetTwain.CurrentImageIndexInBuffer); // Get image data from memory
string mimeType = "image/png";
 
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    new ClientSecrets
    {
        ClientId = " "CLIENT_ID_HERE",
        ClientSecret = "CLIENT_SECRET_HERE",
    },
    new[] { DriveService.Scope.Drive },
    "user",
    CancellationToken.None).Result;
 
// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "ScanUpload",
});
 
File body = new File();
body.Title = mFileTitle;
body.Description = "image";
body.MimeType = mimeType;
 
ImageConverter imageConverter = new ImageConverter();
byte[] byteArray = (byte[])imageConverter.ConvertTo(image, typeof(byte[])); // convert image to byte
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
 
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
request.Upload();

運行程序。

上傳以後,打開Google Drive查看。

源碼

https://github.com/DynamsoftRD/CloudScanner

git clone https://github.com/DynamsoftRD/CloudScanner.git

參考原文

How to Upload Scanned Images to Google Drive With Dynamic .NET TWAIN

相關文章
相關標籤/搜索