如何使用asp.net命名由web cam捕获的图像? [英] how to name the image captured by web cam using asp.net?
问题描述
嗨先生
我在学生管理系统工作。它工作正常。但我需要使用他们的图像创建学生ID,这是由Web Cam捕获的
我从Mr.Mudhasar获得了示例代码(http://www.aspsnippets。 com /),我根据自己的需要改变了。一切都好。但我无法通过studnetID命名图像,我无法在网页的文本框中捕获路径。
目前图像保存为日期时间名称。
以下是我的代码
对不起英语不好。
请帮帮我
Maideen
Vb.Code
Hi sir
I am working in Student management system. It works fine. But I need to created the Student ID using their Image which is captured by Web Cam
I got sample code from Mr.Mudhasar (http://www.aspsnippets.com/), I have changed as per my needs. Every thing is fine. But I could not name the image by studnetID and I could not capture the path in my text box in web page.
Currently image saved as datetime name.
Below is my code
Sorry for poor english.
Pls Help me
Maideen
Vb.Code
Imports dbConnection
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager
Imports System.Configuration
Imports System.Web.UI.Control
Imports System.Web.UI.ScriptManager
Imports System.Data.OleDb
Imports System.IO
Imports System.Web.UI.HtmlControls.HtmlGenericControl
Imports Microsoft.Reporting.WebForms
Imports System.Globalization
Imports System.Web.Services
Partial Class mod_Student_ID_ID_Card
Inherits System.Web.UI.Page
Dim cmd As New SqlCommand
Dim data As SqlDataReader
Dim cls As New cls_Student_Details
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.txtID.Text = Session("stud_id")
loaddata()
captureLive()
End If
End Sub
Private Sub loaddata()
Dim errmsg As String = String.Empty
Dim ds As DataSet = cls.loaddata(Me.txtID.Text)
If ds.Tables.Count > 0 Then
If ds.Tables(0).Rows.Count > 0 Then
Me.txtSID.Text = ds.Tables(0).Rows(0).Item("sid").ToString
Me.txtName.Text = ds.Tables(0).Rows(0).Item("name").ToString
Me.txtCourseCode.Text = ds.Tables(0).Rows(0).Item("coursecode").ToString
Me.txtIntake.Text = ds.Tables(0).Rows(0).Item("intakem").ToString & " - " & ds.Tables(0).Rows(0).Item("intakey").ToString
Me.txtNRIC.Text = (ds.Tables(0).Rows(0).Item("nric").ToString)
Me.txtAddress.Text = ds.Tables(0).Rows(0).Item("caddress").ToString
Else
errmsg = "Error Loading Data"
End If
Else
errmsg = "Error Loading Data"
End If
End Sub
Protected Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
Response.Redirect("default.aspx")
End Sub
Sub captureLive()
If Request.InputStream.Length > 0 Then
Using reader As New StreamReader(Request.InputStream)
Dim hexString As String = Server.UrlEncode(reader.ReadToEnd())
Dim imageName As String = DateTime.Now.ToString("dd-MM-yy hh-mm-ss") '' ( I need image name by Student ID)
Dim imagePath As String = String.Format("../Captures/{0}.png", imageName)
File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString))
Session("CapturedImage") = ResolveUrl(imagePath)
End Using
End If
End Sub
<WebMethod(EnableSession:=True)> _
Public Shared Function GetCapturedImage() As String
Dim url As String = HttpContext.Current.Session("CapturedImage").ToString()
HttpContext.Current.Session("CapturedImage") = Nothing
Return url
End Function
Private Shared Function ConvertHexToBytes(hex As String) As Byte()
Dim bytes As Byte() = New Byte(hex.Length / 2 - 1) {}
For i As Integer = 0 To hex.Length - 1 Step 2
bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
Next
Return bytes
End Function
Private Sub SaveMode()
' Read the file and convert it to Byte Array
Dim filePath As String = Me.txtImageName.Text '(Here I need Path and file name in order to save in Database)
Dim filename As String = Path.GetFileName(filePath)
Dim ext As String = Path.GetExtension(filename)
Dim contenttype As String = String.Empty
'Set the contenttype based on File Extension
Select Case ext
Case ".doc"
contenttype = "application/vnd.ms-word"
Exit Select
Case ".docx"
contenttype = "application/vnd.ms-word"
Exit Select
Case ".xls"
contenttype = "application/vnd.ms-excel"
Exit Select
Case ".xlsx"
contenttype = "application/vnd.ms-excel"
Exit Select
Case ".jpg"
contenttype = "image/jpg"
Exit Select
Case ".png"
contenttype = "image/png"
Exit Select
Case ".gif"
contenttype = "image/gif"
Exit Select
Case ".pdf"
contenttype = "application/pdf"
Exit Select
Case ".ico"
contenttype = "image/ico"
Exit Select
Case ".bmp"
contenttype = "image/bmp"
Exit Select
End Select
If contenttype <> String.Empty Then
Dim fs As Stream = FileUpload1.PostedFile.InputStream
Dim br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(fs.Length)
'insert the file into database
Dim strQuery As String = "insert into tbl_student_image (imagename, imagetype, imagedata) values (@imagename, @imagetype, @imagedata)"
Dim cmd As New SqlCommand(strQuery)
cmd.Parameters.Add("@imagename", SqlDbType.VarChar).Value = filename
cmd.Parameters.Add("@imagetype", SqlDbType.VarChar).Value = contenttype
cmd.Parameters.Add("@imagedata", SqlDbType.Binary).Value = bytes
InsertUpdateData(cmd)
lblMessage.ForeColor = System.Drawing.Color.Green
lblMessage.Text = "File Uploaded Successfully"
Else
lblMessage.ForeColor = System.Drawing.Color.Red
lblMessage.Text = "File format not recognised. Upload Image/Word/PDF/Excel formats"
End If
End Sub
Public Function InsertUpdateData(ByVal cmd As SqlCommand) As Boolean
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ControlDataConnection").ConnectionString
Dim con As New SqlConnection(strConnString)
cmd.CommandType = CommandType.Text
cmd.Connection = con
Try
con.Open()
cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
'Response.Write(ex.Message)
Return False
Finally
con.Close()
con.Dispose()
End Try
End Function
Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
SaveMode()
End Sub
End Class
推荐答案
只需更改代码即可保存名称与学生ID 相同的图像。
类似于 -
Just change the code to save the image with name same as Student ID.
Something like-
Dim imageName As String = Session("stud_id").ToString
注意:您需要确保在此声明之前可以使用``Session(stud_id)``。
要查找保存在应用程序文件夹中的文件的路径,您可以se
HttpServerUtility .MapPath方法 [ ^ ]
以下内容可能会有所帮助 -
Note: You need to make sure that ``Session("stud_id")`` is available prior to this statement.
To find the path of the file saved in your application folder, you can use
HttpServerUtility.MapPath Method[^]
Something like following may help-
HttpContext.Current.Server.MapPath("../Captures/"+Session["stud_id"].ToString+".png")
希望,有帮助:)
Hope, it helps :)
这篇关于如何使用asp.net命名由web cam捕获的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!