我使用jQuery Webcam Plugin与此代码:PHP代码Asp.Net C#
$("#camera").webcam({
width: 250,
height: 375,
mode: "save",
/*swffile: "js/jscam_canvas_only.swf",*/
swffile: "js/jscam.swf",
onTick: function(remain) {
if (0 == remain) {
jQuery("#status").text("Cheese!");
} else {
jQuery("#status").text(remain + " seconds remaining...");
}
},
onSave: function() { },
onCapture: function() {
webcam.save('/upload.ashx');
},
debug: function() { },
onLoad: function() { }
});
插件使用PHP这样的:
<?php
$str = file_get_contents("php://input");
file_put_contents("/tmp/upload.jpg", pack("H*", $str));
?>
和我upload.ashx:
public void ProcessRequest(HttpContext context)
{
System.IO.Stream str = context.Request.InputStream;
int strLen = Convert.ToInt32(str.Length);
byte[] strArr = new byte[strLen];
str.Read(strArr, 0, strLen);
//string st = BitConverter.ToString(strArr); // try 1
//string st = BitConverter.ToString(strArr).Replace("-",""); // try 2
//string st = ByteArrayToString(strArr); //try 3
string st = String.Concat(Array.ConvertAll(strArr, x => x.ToString("X2"))); // try 4
File.WriteAllText(context.Server.MapPath("~/img/Webcam" + DateTime.Now.Ticks.ToString() + ".jpg"), st);
}
public static string ByteArrayToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
I als o尝试读取Bitmap
对象的字节数组并将其保存到磁盘,但这也不起作用。我真的在这里缺少的东西...
编辑感谢Onkelborg,
我忘了提,代码不给错误的,它保存文件。但图像已损坏。无法在Windows照片查看器或Adobe Photoshop中查看它们。
编辑2这也行不通。 (也腐败图片) Save Image From Webrequest in C#
EDIT3我用这个字符串转换为高字节第一个十六进制:
public static byte[] ToHexByte(byte[] arstr)
{
byte[] data = new byte[arstr.Length];
int end = arstr.Length;
for (int i = 0; i < end; i++)
{
byte ch = arstr[i];
byte highNibble = (byte)((ch & 0xf0) >> 4);
byte lowNibble = (byte)((ch & 0x0f) << 4);
data[i] = (byte)(highNibble | lowNibble);
}
return data;
}
Edit4
我发现这个资源http://www.kirupa.com/forum/showthread.php?300792-XML.sendAndLoad%28%29-not-working-IIS7.-ASP.Net-2.0-%28C-3.0%29 并设置ValidateRequest="false"
在我的页面指令。发现因为我从https://github.com/infusion/jQuery-webcam/blob/master/src/jscam.as 找到183行,我感觉我现在正在接近。
什么是 “不工作”?例外?损坏的图像? – Onkelborg
损坏的图像,代码编译,没有例外,只是无法在Windows照片查看器或Adobe Photoshop –