Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle Goggles API/Or workaround
    text
    copied!<p>i have read the thread <a href="https://stackoverflow.com/questions/2080731/google-goggles-api">Google Goggles API</a>. and From <a href="http://notanothercodeblog.blogspot.com/2011/02/google-goggles-api.html" rel="nofollow noreferrer">NotAnotherCodeBlog</a> have created a c# implementation using the sample code. Has anyone been able to get it working recently? i know the API isn't documented and as such may have changed so its possible the code worked and now doesn't OR that my code is faulty. I keep getting an exception thrown giving me a protocol error. (error 500 in the exception and protocol error in the exception status) Code below.</p> <pre><code> 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&lt;byte&gt;(); // a = x + 32 byte[] aVarint = Goggles.ToVarint32(x + 32).ToArray&lt;byte&gt;(); // b = x + 14 byte[] bVarint = Goggles.ToVarint32(x + 14).ToArray&lt;byte&gt;(); // c = x + 10 byte[] cVarint = Goggles.ToVarint32(x + 10).ToArray&lt;byte&gt;(); // 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&lt;byte&gt; ToVarint32(int value) { int index = 0; while ((0x7F &amp; value) != 0) { int i = (0x7F &amp; value); if ((0x7F &amp; (value &gt;&gt; 7)) != 0) { i += 128; } yield return ((byte)i); value = value &gt;&gt; 7; index++; } } } </code></pre> <p>Cheers Tim</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload