2011-11-25 75 views
0

我使用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行,我感覺我現在正在接近。

+1

什麼是 「不工作」?例外?損壞的圖像? – Onkelborg

+0

損壞的圖像,代碼編譯,沒有例外,只是無法在Windows照片查看器或Adobe Photoshop –

回答

1

答案是:http://code.google.com/p/jpegcam/ ,因爲很難找出如何解碼從flash文件接收到的字節。

現在我只需要在我*.ashx文件的Asp.Net C#兩行代碼:

byte[] data = context.Request.BinaryRead(context.Request.TotalBytes); 

File.WriteAllBytes(context.Server.MapPath("~/img/cam" + DateTime.Now.Ticks + ".jpg"), data); 
1

第一個也是最大的問題是,你嘗試從字節轉換爲字符串,這是錯誤的。您應該直接保存這些字節,而不用以任何方式「轉換」它們。

接下來的問題是您正在以錯誤的方式讀取您的流。請參閱:How do I copy the contents of one stream to another?

+0

中打開圖像,所以這就足夠了:byte [] data = Request.BinaryRead(Request.TotalBytes); (「img /」+ DateTime.Now.ToString(「dd_MMM_yymmss」)+「.jpg」),data); –

+0

我不知道BinaryRead究竟是如何工作的,但它聽起來像是應該起作用 – Onkelborg

+0

嗨Onkelborg,PHP函數首先將它轉換爲十六進制,然後是高位半字節http://php.net/manual/en/function.pack.php所以我必須使我的C#代碼像PHP代碼一樣行動 –