/// <summary> /// Recognizes an encoded image (e.g., JPG, PNG, etc) provided as an array of bytes /// </summary> /// <param name="image_bytes">The raw bytes that compose the image</param> /// <returns>An AlprResponse object that contains the results of the recognition</returns> /// <exception cref="InvalidOperationException">Thrown when the library has not been initialized</exception> public AlprResponse Recognize(byte[] image_bytes) { check_initialization(); NativeROI roi; roi.x = 0; roi.y = 0; roi.width = REALLY_BIG_PIXEL_WIDTH; roi.height = REALLY_BIG_PIXEL_WIDTH; IntPtr unmanagedArray = Marshal.AllocHGlobal(image_bytes.Length); Marshal.Copy(image_bytes, 0, unmanagedArray, image_bytes.Length); IntPtr resp_ptr = openalpr_recognize_encodedimage(this.native_instance, unmanagedArray, image_bytes.Length, roi); Marshal.FreeHGlobal(unmanagedArray); // Commented out test JSON string //string json = @"{""version"":2,""data_type"":""alpr_results"",""epoch_time"":1476716853320,""img_width"":600,""img_height"":600,""processing_time_ms"":116.557533,""regions_of_interest"":[{""x"":0,""y"":0,""width"":600,""height"":600}],""results"":[{""plate"":""627WWI"",""confidence"":94.338623,""matches_template"":1,""plate_index"":0,""region"":""wa"",""region_confidence"":82,""processing_time_ms"":50.445648,""requested_topn"":10,""coordinates"":[{""x"":242,""y"":360},{""x"":358,""y"":362},{""x"":359,""y"":412},{""x"":241,""y"":408}],""candidates"":[{""plate"":""627WWI"",""confidence"":94.338623,""matches_template"":1},{""plate"":""627WKI"",""confidence"":80.588486,""matches_template"":1},{""plate"":""627WI"",""confidence"":79.943542,""matches_template"":0},{""plate"":""627WVI"",""confidence"":79.348930,""matches_template"":1},{""plate"":""627WRI"",""confidence"":79.196785,""matches_template"":1},{""plate"":""627WNI"",""confidence"":79.165802,""matches_template"":1}]}]}"; string json = Marshal.PtrToStringAnsi(resp_ptr); AlprResponse response = JsonConvert.DeserializeObject <AlprResponse>(json); openalpr_free_response_string(resp_ptr); return(response); }
/// <summary> /// Recognizes an image from a .NET Bitmap object /// </summary> /// <param name="bitmap">The .NET Bitmap object to recognize</param> /// <returns>An AlprResponse object that contains the results of the recognition</returns> /// <exception cref="InvalidOperationException">Thrown when the library has not been initialized</exception> public AlprResponse Recognize(System.Drawing.Image image) { check_initialization(); System.Drawing.Bitmap clone = new System.Drawing.Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(clone)) { gr.DrawImage(image, new System.Drawing.Rectangle(0, 0, clone.Width, clone.Height)); } System.Drawing.Imaging.BitmapData locked_bmp = clone.LockBits(new System.Drawing.Rectangle(0, 0, clone.Width, clone.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, clone.PixelFormat); byte[] raw_bytes = new byte[locked_bmp.Stride * locked_bmp.Height]; System.Runtime.InteropServices.Marshal.Copy(locked_bmp.Scan0, raw_bytes, 0, raw_bytes.Length); clone.UnlockBits(locked_bmp); int bytes_per_pixel = System.Drawing.Image.GetPixelFormatSize(clone.PixelFormat) / 8; NativeROI roi; roi.x = 0; roi.y = 0; roi.width = image.Width; roi.height = image.Height; IntPtr unmanagedArray = Marshal.AllocHGlobal(raw_bytes.Length); Marshal.Copy(raw_bytes, 0, unmanagedArray, raw_bytes.Length); IntPtr resp_ptr = openalpr_recognize_rawimage(this.native_instance, unmanagedArray, bytes_per_pixel, image.Width, image.Height, roi); Marshal.FreeHGlobal(unmanagedArray); string json = Marshal.PtrToStringAnsi(resp_ptr); AlprResponse response = JsonConvert.DeserializeObject <AlprResponse>(json); openalpr_free_response_string(resp_ptr); return(response); }