PHP 代码到 Asp.Net C#

发布于 2024-12-18 03:42:51 字数 3347 浏览 0 评论 0 原文

我正在使用 jQuery 网络摄像头插件 和以下代码:

$("#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 () { }
        });

该插件使用 < strong>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 对象并将其保存到磁盘,但是那也行不通。我真的在这里遗漏了一些东西...

编辑谢谢Onkelborg,

我忘了提及代码不会给出错误,它会保存文件。但图像已损坏。无法在 Windows 照片查看器或 Adob​​e Photoshop 中查看它们。

Edit2 这也不起作用。 (也损坏图像) 用 C# 保存来自 Webrequest 的图像

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 找到了第 183 行。 com/infusion/jQuery-webcam/blob/master/src/jscam.as 我感觉我现在越来越近了。

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 () { }
        });

The plugin uses PHP like this:

<?php
    $str = file_get_contents("php://input");
    file_put_contents("/tmp/upload.jpg", pack("H*", $str));
?>

and my 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...

Edit Thanks Onkelborg,

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 this also does not work. (also corrupt images)
Save Image From Webrequest in C#

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅蓝的眸勾画不出的柔情 2024-12-25 03:42:51

第一个也是最大的问题是您尝试从字节转换为字符串,这是错误的。您应该直接保存这些字节,而不以任何方式“转换”它们。

下一个问题是您以错误的方式读取流。请参阅:如何将一个流的内容复制到另一个?

The first, and biggest, problem is the fact that you try to convert from bytes to strings, that's wrong. You should save those bytes directly without "converting" them in any way.

The next problem is that you are reading your stream in the wrong way. See: How do I copy the contents of one stream to another?

淡淡绿茶香 2024-12-25 03:42:51

答案是:http://code.google.com/p/jpegcam/
因为很难找出如何解码从闪存文件接收到的字节。

现在我只需要在 *.ashx 文件中添加两行 Asp.Net C# 代码:

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

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

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.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文