Как сохранить SANE отсканированные изображения в базу данных?
В этом разделе
Вот C#/VB.NET код, который демонстрирует, как получить изображения от SANE сканера и сохранить изображения в памяти в виде многостраничного PDF документа. Позже PDF документ может быть сохранен в базу данных или передан на сервер.
/// <summary>
/// Acquires images from SANE device and saves images in a database.
/// </summary>
public void AcquireImagesFromSaneDeviceAndSaveImagesInDatabase()
{
// create SANE device manager
using (Vintasoft.Sane.SaneLocalDeviceManager deviceManager = new Vintasoft.Sane.SaneLocalDeviceManager())
{
// open SANE device manager
deviceManager.Open();
// get count of SANE devices
int deviceCount = deviceManager.Devices.Count;
if (deviceCount == 0)
{
System.Console.WriteLine("Devices are not found.");
return;
}
// select the first SANE device
Vintasoft.Sane.SaneLocalDevice device = deviceManager.Devices[0];
// open SANE device
device.Open();
System.IO.MemoryStream pdfDocumentInMemory = new System.IO.MemoryStream();
Vintasoft.Sane.SaneAcquiredImage acquiredImage;
do
{
try
{
// acquire image from SANE device
acquiredImage = device.AcquireImageSync();
// if image is received
if (acquiredImage != null)
{
System.Console.WriteLine("Image is acquired.");
// create TIFF encoder settings
Vintasoft.Sane.ImageEncoders.SanePdfEncoderSettings pdfEncoderSettings =
new Vintasoft.Sane.ImageEncoders.SanePdfEncoderSettings();
// specify that image must be added to a PDF file
pdfEncoderSettings.PdfMultiPage = true;
// specify image must be compressed with ZIP compression
pdfEncoderSettings.PdfImageCompression = Vintasoft.Sane.ImageEncoders.SanePdfImageCompression.ZIP;
// process acquired image
acquiredImage.Save(pdfDocumentInMemory, pdfEncoderSettings);
}
// if image is not received
else
{
System.Console.WriteLine("Scan is completed.");
// save memory stream pdfDocumentInMemory to a database
break;
}
}
catch (System.Exception ex)
{
System.Console.WriteLine(string.Format("Scan is failed: {0}", ex.Message));
break;
}
}
// while device has more images to scan
while (device.HasMoreImagesToScan);
// close SANE device
device.Close();
// close SANE device manager
deviceManager.Close();
}
System.Console.ReadLine();
}
''' <summary>
''' Acquires images from SANE device and saves images in a database.
''' </summary>
Public Sub AcquireImagesFromSaneDeviceAndSaveImagesInDatabase()
' create SANE device manager
Using deviceManager As New Vintasoft.Sane.SaneLocalDeviceManager()
' open SANE device manager
deviceManager.Open()
' get count of SANE devices
Dim deviceCount As Integer = deviceManager.Devices.Count
If deviceCount = 0 Then
System.Console.WriteLine("Devices are not found.")
Return
End If
' select the first SANE device
Dim device As Vintasoft.Sane.SaneLocalDevice = deviceManager.Devices(0)
' open SANE device
device.Open()
Dim pdfDocumentInMemory As New System.IO.MemoryStream()
Dim acquiredImage As Vintasoft.Sane.SaneAcquiredImage
Do
Try
' acquire image from SANE device
acquiredImage = device.AcquireImageSync()
' if image is received
If acquiredImage IsNot Nothing Then
System.Console.WriteLine("Image is acquired.")
' create TIFF encoder settings
Dim pdfEncoderSettings As New Vintasoft.Sane.ImageEncoders.SanePdfEncoderSettings()
' specify that image must be added to a PDF file
pdfEncoderSettings.PdfMultiPage = True
' specify image must be compressed with ZIP compression
pdfEncoderSettings.PdfImageCompression = Vintasoft.Sane.ImageEncoders.SanePdfImageCompression.ZIP
' process acquired image
acquiredImage.Save(pdfDocumentInMemory, pdfEncoderSettings)
Else
' if image is not received
System.Console.WriteLine("Scan is completed.")
' save memory stream pdfDocumentInMemory to a database
Exit Try
End If
Catch ex As System.Exception
System.Console.WriteLine(String.Format("Scan is failed: {0}", ex.Message))
Exit Try
End Try
' while device has more images to scan
Loop While device.HasMoreImagesToScan
' close SANE device
device.Close()
' close SANE device manager
deviceManager.Close()
End Using
System.Console.ReadLine()
End Sub