ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3416|ตอบกลับ: 1

[VB.NET] การ Capture ภาพจากกล้อง WebCam Touchless SDK และแปลงเป็นไฟล์ PDF ด้วย Syncfusion

[คัดลอกลิงก์]

309

กระทู้

500

โพสต์

6030

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6030



สำหรับรายละเอียดของเรื่อง Touchless SDK สามารถไปดูได้ที่ [VB.NET] การจับภาพและบันทึกไฟล์ภาพจากกล้องเว็บแคมด้วยการใช้งาน Touchless SDK ... ส่วนในบทความนี้จะเป็นการนำภาพที่แคปเจอร์มาใส่ PictureBox แล้วแปลงเป็นไฟล์ PDF ซึ่งต้อง Add References Syncfusion.Pdf.Base ของฟรีจากค่าย Syncfusion เข้ามาใช้งานก่อนด้วยครับ สามารถดูรายละเอียดได้จาก [VB.NET] โค้ดการใช้งาน XlsIO ของ Syncfusion เพื่อทำการแปลงไฟล์ Excel ให้เป็น PDF และ PNG ...

มาดูโค้ดกันเถอะ ...
  1. Imports TouchlessLib
  2. Imports System.IO
  3. Imports Syncfusion.Pdf
  4. Imports Syncfusion.Pdf.Graphics

  5. Public Class frmCamera
  6.     Dim WebCamMgr As TouchlessLib.TouchlessMgr
  7.     Dim strPDFPath As String

  8.     Private Sub frmCamera_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  9.         Try
  10.             Timer1.Enabled = False
  11.             WebCamMgr.CurrentCamera.Dispose()
  12.             WebCamMgr.Cameras.Item(cmbCamera.SelectedIndex).Dispose()
  13.             WebCamMgr.Dispose()
  14.         Catch ex As Exception
  15.             MessageBox.Show(ex.Message)
  16.         End Try
  17.     End Sub

  18.     Private Sub frmCamera_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  19.         WebCamMgr = New TouchlessLib.TouchlessMgr
  20.         Label2.Text = ""
  21.         For i As Integer = 0 To WebCamMgr.Cameras.Count - 1
  22.             cmbCamera.Items.Add(WebCamMgr.Cameras(i).ToString)
  23.         Next
  24.         If cmbCamera.Items.Count >= 0 Then
  25.             cmbCamera.SelectedIndex = 0
  26.             Timer1.Enabled = True
  27.             btnSave.Enabled = False
  28.             '// Create a folder in VB if it doesn't exist.
  29.             strPDFPath = MyPath(Application.StartupPath) & "PDF"
  30.             If (Not System.IO.Directory.Exists(strPDFPath)) Then System.IO.Directory.CreateDirectory(strPDFPath)
  31.             '// Initialized and load images into DataGridView.
  32.             Call InitDataGridView()
  33.             Call LoadPDF2DatagridView()
  34.         Else
  35.             MessageBox.Show("No Web Camera, This application needs a webcam.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  36.             Me.Close()
  37.         End If
  38.     End Sub

  39.     ' / --------------------------------------------------------------------------
  40.     Private Sub LoadPDF2DatagridView()
  41.         Dim directory As New IO.DirectoryInfo(strPDFPath)
  42.         If directory.Exists Then
  43.             Dim pdfFiles() As IO.FileInfo = directory.GetFiles("*.pdf")
  44.             Dim row As String()
  45.             Dim iArr() As String
  46.             For Each pdfFile As IO.FileInfo In pdfFiles
  47.                 iArr = Split(pdfFile.FullName, "")
  48.                 row = New String() {iArr(UBound(iArr))}
  49.                 dgvData.Rows.Add(row)
  50.             Next
  51.         End If
  52.         Label2.Text = "Total PDF : " & Me.dgvData.Rows.Count
  53.     End Sub

  54.     ' / --------------------------------------------------------------------------
  55.     Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
  56.         If picPreview Is Nothing Then Exit Sub
  57.         Try
  58.             '/ Create file name
  59.             Dim sPicName As String = strPDFPath & Format(Now, "ddMMyy-hhmmss") & ".png"
  60.             Dim b As Bitmap = picPreview.Image
  61.             b.Save(sPicName, System.Drawing.Imaging.ImageFormat.Png)
  62.             '// Copy filename and change to PDF extension.
  63.             Dim sPDF As String = Mid(sPicName, 1, Len(sPicName) - 3) & "pdf"
  64.             '/ Create a new PDF document.
  65.             Dim doc As New PdfDocument()
  66.             '/ Add a page to the document
  67.             Dim page As PdfPage = doc.Pages.Add()
  68.             '/ Getting page size to draw the image which fits the page.
  69.             Dim pageSize As SizeF = page.GetClientSize()
  70.             '/ Create PDF graphics for the page.
  71.             Dim graphics As PdfGraphics = page.Graphics
  72.             '/ Load the image from the disk.
  73.             Dim image As New PdfBitmap(sPicName)
  74.             '/ Draw the image
  75.             graphics.DrawImage(image, New RectangleF(0, 0, pageSize.Width, pageSize.Height))
  76.             '/ Save the document
  77.             doc.Save(sPDF)
  78.             '/ Close the document
  79.             doc.Close(True)
  80.             '// Show File name in the DataGridView.
  81.             Dim iArr() As String
  82.             iArr = Split(sPDF, "")
  83.             Dim row As String()
  84.             row = New String() {iArr(UBound(iArr))}
  85.             dgvData.Rows.Add(row)
  86.             '//
  87.             Label2.Text = "Total PDF : " & Me.dgvData.Rows.Count
  88.             Me.dgvData.Focus()
  89.             SendKeys.Send("^{HOME}")
  90.             SendKeys.Send("^{DOWN}")
  91.         Catch ex As Exception
  92.             MessageBox.Show(ex.Message)
  93.         End Try
  94.     End Sub

  95.     ' / --------------------------------------------------------------------------
  96.     Private Sub dgvData_DoubleClick(sender As Object, e As System.EventArgs) Handles dgvData.DoubleClick
  97.         If Me.dgvData.Rows.Count = 0 Then Return
  98.         Process.Start(strPDFPath & dgvData.CurrentRow.Cells(0).Value.ToString)
  99.     End Sub

  100.     ' / --------------------------------------------------------------------------
  101.     Private Sub InitDataGridView()
  102.         Me.dgvData.Columns.Clear()
  103.         '// Initialize DataGridView Control
  104.         With dgvData
  105.             .AllowUserToAddRows = False
  106.             .AllowUserToDeleteRows = False
  107.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  108.             .AutoResizeColumns()
  109.             .AllowUserToResizeColumns = True
  110.             .AllowUserToResizeRows = True
  111.             .SelectionMode = DataGridViewSelectionMode.FullRowSelect
  112.         End With
  113.         '// Declare columns type.
  114.         '// Add 1th column (Index = 0), Show image file name.
  115.         Dim FName As New DataGridViewTextBoxColumn()
  116.         dgvData.Columns.Add(FName)
  117.         With FName
  118.             .HeaderText = "File Name"
  119.             .ReadOnly = True
  120.             .Visible = True
  121.         End With
  122.         '//
  123.         Me.dgvData.Focus()
  124.     End Sub

  125.     Private Sub cmbCamera_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbCamera.SelectedIndexChanged
  126.         WebCamMgr.CurrentCamera = WebCamMgr.Cameras.ElementAt(cmbCamera.SelectedIndex)
  127.         '//
  128.         'WebCamMgr.CurrentCamera.CaptureHeight = 480
  129.         'WebCamMgr.CurrentCamera.CaptureWidth = 640
  130.     End Sub

  131.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
  132.         picFeed.Image = WebCamMgr.CurrentCamera.GetCurrentImage()
  133.     End Sub

  134.     Private Sub btnCapture_Click(sender As System.Object, e As System.EventArgs) Handles btnCapture.Click
  135.         picPreview.Image = WebCamMgr.CurrentCamera.GetCurrentImage()
  136.         btnSave.Enabled = True
  137.     End Sub

  138.     '// Delete PDF & PNG File.
  139.     Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
  140.         If Me.dgvData.Rows.Count = 0 Then Return
  141.         Try
  142.             Dim sPic As String = dgvData.CurrentRow.Cells(0).Value.ToString
  143.             sPic = Mid(sPic, 1, Len(sPic) - 3) & "png"
  144.             '// Delete files in folder.
  145.             FileSystem.Kill(strPDFPath & dgvData.CurrentRow.Cells(0).Value.ToString)
  146.             FileSystem.Kill(strPDFPath & sPic)
  147.             '// Delete current row from dgvData
  148.             dgvData.Rows.Remove(dgvData.CurrentRow)
  149.         Catch ex As Exception
  150.             MessageBox.Show(ex.Message)
  151.         End Try
  152.     End Sub

  153. End Class
คัดลอกไปที่คลิปบอร์ด

ฟังค์ชั่นการกำหนดตำแหน่งพาธ ... โมดูลหากิน 5555+
  1. Module modFunction
  2.     ' / Get my project path
  3.     ' / Ex.
  4.     ' / AppPath = C:\My Project\bin\debug
  5.     ' / Replace "\bin\debug" with ""
  6.     ' / Return : C:\My Project\
  7.     Public Function MyPath(AppPath As String) As String
  8.         '/ MessageBox.Show(AppPath);
  9.         AppPath = AppPath.ToLower()
  10.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "")
  11.         '// Check the backslash symbol (ASCII Code = 92) on the far right. If not, add one at the end.
  12.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  13.     End Function

  14. End Module
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

58

โพสต์

10

เครดิต

Member

Rank: 2

เครดิต
10
โพสต์ 2022-10-25 15:35:54 | ดูโพสต์ทั้งหมด

ขอบคุณครับ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-19 13:40 , Processed in 0.275893 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้