Я использую jQuery Webcam Plugin с этим кодом:
$("#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();
}
Я также пытался прочитать байтовый массив в объект Bitmap
и сохранить его на диск, но это также не работает. Я действительно что-то здесь упускаю ...
Редактировать Спасибо, Онкельборг,
Я забыл упомянуть, что код не выдает ошибок, он сохраняет файлы. Но изображения повреждены. Не удается просмотреть их в средстве просмотра фотографий Windows или Adobe Photoshop.
Edit2 это также не работает. (также поврежденные изображения)
Сохранить изображение из веб-запроса в 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"
в директиве моей страницы. нашел, потому что я нашел строку 183 из https://github.com/infusion/jQuery-webcam/blob/master/src/jscam.as
у меня такое чувство, что я сейчас ближе.