PHP code到Asp.Net C#
我使用的是的jQuery插件摄像头与这code
I am using the jQuery Webcam Plugin with this code:
$("#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 also tried to read the byte array to the Bitmap
object and save that to the disk, but that also does not work. I am really missing something here...
修改感谢Onkelborg,
Edit Thanks Onkelborg,
我忘了提,code不给错误,保存文件。但图像被破坏。在Windows照片查看器或Adobe Photoshop无法查看它们。
I forgot to mention that the code does not give errors, it saves files. But the images are corrupted. Can't view them in Windows Photo Viewer or Adobe Photoshop.
EDIT2 这也不起作用。 (也损坏图像)
保存图像从WebRequest类在C#
Edit2 this also does not work. (also corrupt images) Save Image From Webrequest in C#
EDIT3 我用这个字符串转换为高字节第一个十六进制:
Edit3 I use this to convert the string to high nibble first hex:
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 =在我的页面指令假
。发现,因为我发现183线从 https://github.com /infusion/jQuery-webcam/blob/master/src/jscam.as
我有我更接近现在得到的感觉。
I found this resource http://www.kirupa.com/forum/showthread.php?300792-XML.sendAndLoad%28%29-not-working-IIS7.-ASP.Net-2.0-%28C-3.0%29
and set ValidateRequest="false"
in my page directive. found that because i found line 183 from https://github.com/infusion/jQuery-webcam/blob/master/src/jscam.as
i have the feeling that i am getting closer now.
答案是: HTTP://$c$c.google.com/p/jpegcam/
因为它是很难找出如何去code你的Flash文件接收的字节数。
The answer is: http://code.google.com/p/jpegcam/ because it is hard to find out how to decode the bytes you receive from the flash file.
现在我只是需要Asp.Net C#code两线在我的 * ASHX
文件:
Now I just needed two lines of Asp.Net C# code in my *.ashx
file:
byte[] data = context.Request.BinaryRead(context.Request.TotalBytes);
File.WriteAllBytes(context.Server.MapPath("~/img/cam" + DateTime.Now.Ticks + ".jpg"), data);