我正在尝试从网络摄像头以及Android移动摄像头捕获图像,并将图像的二进制代码插入数据库。我正在使用Javascript函数捕获图像,现在捕获了图像并将其放入变量中,我需要将其存储到SQL数据库中并从数据库中显示它。
数据库为:
CREATE TABLE [dbo].[test] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[data] VARBINARY (MAX) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>WebcamJS Test Page</title>
<style type="text/css">
body { font-family: Helvetica, sans-serif; }
h2, h3 { margin-top:0; }
form { margin-top: 15px; }
form > input { margin-right: 15px; }
#results { float:right; margin:20px; padding:20px; border:1px solid; background:#ccc; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="results">Your captured image will appear here...</div>
<h1>WebcamJS Test Page</h1>
<h3>Demonstrates simple 320x240 capture & display</h3>
<div id="my_camera"></div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<!-- First, include the Webcam.js JavaScript Library -->
<%--<script type="text/javascript" src="../webcam.min.js"></script>--%>
<script src="webcam.min.js"></script>
<!-- Configure a few settings and attach camera -->
<script language="JavaScript">
Webcam.set({
width: 320,
height: 240,
image_format: 'jpeg',
jpeg_quality: 90
});
Webcam.attach('#my_camera');
</script>
<!-- A button for taking snaps -->
<form>
<input type=button value="Take Snapshot" onClick="take_snapshot()">
</form>
<!-- Code to handle taking the snapshot and displaying it locally -->
<script language="JavaScript">
function take_snapshot() {
// take snapshot and get image data
Webcam.snap(function (data_uri) {
// display results in page
document.getElementById('results').innerHTML =
'<h2>Here is your image:</h2>' +
'<img src="' + data_uri + '"/>';
document.getElementById('<%=TextBox1.ClientID %>').value = data_uri;
//document.getElementById["TextBox1"].innerHTML = data_uri;
});
}
</script>
</form>
</body>
</html>
答案 0 :(得分:0)
您可以在C#中执行此操作(因为如果需要,则无需使用C#或javascript)
插入
using(var connection = new SqlConnection("YOUR CONNECTION STRING"))
using (var command = new SqlCommand("INSERT INTO ([data]) VALUES(@imageData)", connection ))
{
connection.Open();
var parameter = new SqlParameter("@imageData", SqlDbType.Binary)
{
// here goes your binary data
Value = yourCSharpObjectImageData.GetTemplateBuffer().ToArray()
};
command.Parameters.Add(parameter);
int rowsAffected = command.ExecuteNonQuery();
// do something else ...
}
检索
using (var connection = new SqlConnection("YOUR CONNECTION STRING ..."))
using (var command = new SqlCommand("SELECT [data] THUMB FROM test WHERE ID = @ID", connection))
{
connection.Open();
command.Parameters.AddWithValue("@ID", yourIdHere);
return command.ExecuteScalar() as byte[];
}