谷歌眼镜API/或解决方案



我已经读了这个线程Google Goggles API。从NotAnotherCodeBlog已经使用样例代码创建了一个c#实现。最近有人修好它了吗?我知道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++;     
        } 
    }
}

干杯蒂姆。

一个问题可能是你发送的是。gif格式而不是。jpg格式。我想这就是法帝说的他测试过的

最新更新