How to capture image in webcam and insert in Table

Posted by Shail under ASP.NET MVC on 7/8/2014 | Points: 10 | Views : 3072 | Status : [Member] | Replies : 8
How to capture image in webcam and insert in Table using asp.net mvc4.0 razor
plz help me




Responses

Posted by: Vuyiswamb on: 7/8/2014 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Here is the perfect Solution
http://weblogs.asp.net/gunnarpeipman/using-jquery-webcam-plugin-with-asp-net-mvc

Thank you for posting at Dotnetfunda
[Administrator]

Shail, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shail on: 7/8/2014 [Member] Starter | Points: 25

Up
0
Down
i am use same code but how can insert image in table
my code is
public ActionResult AddVisitor()
{
//dropdownbind ddbind = new dropdownbind();
//List<tblFaculty> item = db.tblFaculties.ToList();
//List<tblEmployee> item1 = db.tblEmployees.ToList();


return View();
}
[HttpPost]
public ActionResult AddVisitor(tblReception model)
{

if (ModelState.IsValid)
{

db.tblReceptions.Add(model);
db.SaveChanges();
ModelState.Clear();
ViewBag.msg = "Visitor Record Insert";
}

return View();
}
public void Capture()
{
var stream = Request.InputStream;
string dump;

using (var reader = new StreamReader(stream))
dump = reader.ReadToEnd();

var path = Server.MapPath("~/visitorimages/);
System.IO.File.WriteAllBytes(path, String_To_Bytes2(dump));
}

private byte[] String_To_Bytes2(string strInput)
{
int numBytes = (strInput.Length) / 2;
byte[] bytes = new byte[numBytes];

for (int x = 0; x < numBytes; ++x)
{
bytes[x] = Convert.ToByte(strInput.Substring(x * 2, 2), 16);
}

return bytes;
}
}
}


Shail, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 7/8/2014 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
use this

http://byatool.com/mvc/asp-net-mvc-upload-image-to-database-and-show-image-dynamically-using-a-view/

Thank you for posting at Dotnetfunda
[Administrator]

Shail, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shail on: 7/8/2014 [Member] Starter | Points: 25

Up
0
Down
hi Vuyiswamb Sir
image path come in ....var path = Server.MapPath("~/visitorimages/);
i want ask how can insert this path in table
thanks

Shail, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 7/8/2014 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
The next part is Simple write a Stored Procedure that can insert data into the table

you must have a table that has "varbinary" Type to store an image like the one below

CREATE TABLE [dbo].[Image](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Caption] [nvarchar](50) NOT NULL,
[Content] [varbinary](max) NOT NULL,
)


and the Stored Procedure to store should look something like this

CREATE PROC InsertNewImage
@Caption varchar(50),
@Content varbinary(max)
AS
INSERT INTO [DummyDatabase].[dbo].[Image] ([Caption], [Content])
VALUES (@Caption, @Content)


and the Stored Procedure to retrieve should look like this

CREATE PROC GetImageByID
@ID int
AS
SELECT Caption, Content FROM Image WHERE ID = @ID



now in your code you should have a Function like this

public void InsertImage(string imageName, string imagePath)
{
System.IO.FileStream fs = new System.IO.FileStream(imagePath, System.IO.FileMode.Open);
byte[] imageAsBytes = new byte[fs.Length];
fs.Read(imageAsBytes, 0, imageAsBytes.Length);
fs.Close();
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=DummyDatabase;Integrated Security=True");/
string insertString = "[dbo].[InsertNewImage]";

SqlCommand insertCommand = new SqlCommand(insertString, conn);
insertCommand.CommandType = CommandType.StoredProcedure;

insertCommand.Parameters.Add("@Caption", SqlDbType.VarChar).Value = imageName ;
insertCommand.Parameters.Add("@Content", SqlDbType.VarBinary).Value = imageAsBytes;

try
{
conn.Open();
insertCommand.ExecuteNonQuery();
}
catch(SqlException ex)
{
//handle the ex Exception
}
finally
{
conn.Close();
}
}


and retrieve the images the following code can be used


public Image GetImageById(int imageID)
{
Image result = null;
SqlDataReader imageReader;

SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=DummyDatabase;Integrated Security=True");
string selectString = "[dbo].[GetImageByID]";
SqlCommand selectCommand = new SqlCommand(selectString, conn);
selectCommand.CommandType = CommandType.StoredProcedure;
selectCommand.Parameters.AddWithValue("@ID", imageID.ToString());

conn.Open();
imageReader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection);
while(imageReader.Read())
{
result = Image.FromStream(new MemoryStream((byte[])imageReader.GetValue(1)));
}
return result;
}


in your code you would call your function like this


 public void Capture() 
{
var stream = Request.InputStream;
string dump;

using (var reader = new StreamReader(stream))
dump = reader.ReadToEnd();

var path = Server.MapPath("~/visitorimages/");
InsertImage("Visitors Image",path)
}


Thank you for posting at Dotnetfunda
[Administrator]

Shail, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shail on: 7/8/2014 [Member] Starter | Points: 25

Up
0
Down
Thanks Vuyiswamb Sir

Shail, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 7/8/2014 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Thank you for choosing Dotnetfunda.

Thank you for posting at Dotnetfunda
[Administrator]

Shail, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saineshwar on: 5/13/2015 [Member] Starter | Points: 25

Up
0
Down
refer this link you will find your answer

http://www.c-sharpcorner.com/UploadFile/4d9083/capturing-image-from-web-cam-in-Asp-Net-mvc139/

Shail, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response