1

我已阅读线程 Google Goggles API。和 From NotAnotherCodeBlog使用示例代码创建了 ac# 实现。最近有没有人能够让它工作?我知道 API 没有记录,因此可能已经更改,因此代码可能有效,但现在没有,或者我的代码有问题。我不断收到一个异常,给我一个协议错误。(异常中的错误 500 和异常状态中的协议错误)代码如下。

 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
public class Goggles
{         // The POST body required to validate the CSSID.         
    private static byte[] CssidPostBody = new byte[] { 34, 0, 98, 60, 10, 19, 34,
        2, 101, 110, 186, 211, 240, 59, 10, 8, 1, 16, 1, 40, 1, 48, 0, 56, 1, 18,             
        29, 10, 9, 105, 80, 104, 111, 110, 101, 32, 79, 83, 18, 3, 52, 46, 49,              
        26, 0, 34, 9, 105, 80, 104, 111, 110, 101, 51, 71, 83, 26, 2, 8, 2, 34,             
        2, 8, 1 };
    // Bytes trailing the image byte array. Look at the next code snippet to see         
    // where it is used in SendPhoto() method.         
    private static byte[] TrailingBytes = new byte[] { 24, 75, 32, 1, 48, 0, 146,
        236, 244, 59, 9, 24, 0, 56, 198, 151, 220, 223, 247, 37, 34, 0 };
    // Generates a cssid.         
    private static string Cssid
    {
        get
        {
            Random random = new Random((int)DateTime.Now.Ticks);
            return string.Format(
                "{0}{1}",
                random.Next().ToString("X8"),
                random.Next().ToString("X8"));
        }
    }
    static void Main(string[] args)
    {
        string cssid = Goggles.Cssid;
        Goggles.ValidateCSSID(cssid);
        // See next code snippet for SendPhoto()             
        //Goggles.SendPhoto(cssid, yourImageByteArray);             
        Console.ReadLine();
    }
    // Validates the CSSID we just created, by POSTing it to Goggles.         
    private static void ValidateCSSID(string cssid)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format("http://www.google.com/goggles/container_proto?cssid={0}", cssid));
        Goggles.AddHeaders(request); request.Method = "POST";
        using (Stream stream = request.GetRequestStream())
        {
            stream.Write(Goggles.CssidPostBody, 0, Goggles.CssidPostBody.Length);
            stream.Flush();
        } 
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    }
    private static byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        return ms.ToArray();
    }
    private static void AddHeaders(HttpWebRequest request)
    {
        request.ContentType = "application/x-protobuffer";
        request.Headers["Pragma"] = "no-cache";
        request.KeepAlive = true;
    }
    public static HttpWebResponse SendPhoto(string file)
    {
        System.Drawing.Image oimg = System.Drawing.Image.FromFile(file);

        return (SendPhoto(imageToByteArray(oimg)));
    }
    public static   HttpWebResponse  SendPhoto(byte[] image)
    {
        string cssid = null;
        HttpWebResponse response;
        SendPhoto(ref cssid, image, out response);
        return response;
    }
    public static void SendPhoto( ref string cssid, byte[] image, out HttpWebResponse pHttpWebResponse) 
    { 
        if (cssid == null || cssid == "") 
                cssid = Goggles.Cssid;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(

            string.Format(        
            "http://www.google.com/goggles/container_proto?cssid={0}",
            cssid));
        Goggles.AddHeaders(request);
        request.Method = "POST";       
        // x = image size
        int x = image.Length;
        byte[] xVarint = Goggles.ToVarint32(x).ToArray<byte>();
        // a = x + 32     
        byte[] aVarint = Goggles.ToVarint32(x + 32).ToArray<byte>();
        // b = x + 14     
        byte[] bVarint = Goggles.ToVarint32(x + 14).ToArray<byte>();
        // c = x + 10     
        byte[] cVarint = Goggles.ToVarint32(x + 10).ToArray<byte>();
        // 0A [a] 0A [b] 0A [c] 0A [x] [image bytes]     
        using (Stream stream = request.GetRequestStream())     
        {         
            // 0x0A         
            stream.Write(new byte[] { 10 }, 0, 1);
                // a         
            stream.Write(aVarint, 0, aVarint.Length);
            // 0x0A         
            stream.Write(new byte[] { 10 }, 0, 1);
            // b         
            stream.Write(bVarint, 0, bVarint.Length);           
            // 0x0A         
            stream.Write(new byte[] { 10 }, 0, 1);
            // c         
            stream.Write(cVarint, 0, cVarint.Length);
            // 0x0A         
            stream.Write(new byte[] { 10 }, 0, 1);
            // x         
            stream.Write(xVarint, 0, xVarint.Length);
            // Write image          
            stream.Write(image, 0, image.Length);
            // Write trailing bytes         
            stream.Write(             
                Goggles.TrailingBytes,
                0,              Goggles.TrailingBytes.Length);
            stream.Flush();
        } try
        {
            pHttpWebResponse = null;
            pHttpWebResponse = (HttpWebResponse)request.GetResponse();
        }
        catch (Exception ex)
        {
            pHttpWebResponse = null;

        }
    }
    // Encodes an int32 into varint32. 
    public static IEnumerable<byte> ToVarint32(int value) 
    {     
        int index = 0;
        while ((0x7F & value) != 0)
        {         
            int i = (0x7F & value);
            if ((0x7F & (value >> 7)) != 0)
            {             
                i += 128;         
            }     

            yield return ((byte)i);         
            value = value >> 7;
            index++;     
        } 
    }
}

干杯蒂姆

4

1 回答 1

2

一个问题可能是您发送的是 .gif 格式而不是 .jpg 格式。我认为这就是 Fadi 所说的他已经测试过的内容

于 2012-01-28T19:48:06.543 回答