Skip to content

Commit d8800b4

Browse files
committed
push latest updates
1 parent 5868158 commit d8800b4

15 files changed

+85
-129
lines changed

dotnet/InkRecognition/wpf-app/src/HTTP/HttpManager.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Contoso.NoteTaker.JSON;
2-
using Contoso.NoteTaker.JSON.Format;
32
using System;
43
using System.Net;
54
using System.Net.Http;
@@ -23,23 +22,24 @@ public HttpManager(string appKey, string baseAddress, string destinationUrl)
2322

2423
public async Task<HttpResponseMessage> PutAsync(string jsonRequest)
2524
{
26-
var httpContent = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
27-
var httpResponse = await httpClient.PutAsync(destinationUrl, httpContent);
28-
29-
// Throw exception for malformed/unauthorized http requests
30-
if (httpResponse.StatusCode == HttpStatusCode.BadRequest || httpResponse.StatusCode == HttpStatusCode.NotFound || httpResponse.StatusCode == HttpStatusCode.Unauthorized)
25+
try
3126
{
32-
var errorJson = await httpResponse.Content.ReadAsStringAsync();
27+
var httpContent = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
28+
var httpResponse = await httpClient.PutAsync(destinationUrl, httpContent);
3329

34-
var errDetail = JSONProcessor.ParseInkRecognitionError(errorJson);
35-
ThrowExceptionForHttpError(errDetail);
30+
// Throw exception for malformed/unauthorized http requests
31+
if (httpResponse.StatusCode == HttpStatusCode.BadRequest || httpResponse.StatusCode == HttpStatusCode.Unauthorized)
32+
{
33+
var errorJson = await httpResponse.Content.ReadAsStringAsync();
34+
var errDetail = JSONProcessor.ParseInkRecognitionError(errorJson);
35+
throw new HttpRequestException(errDetail.ToString());
36+
}
37+
return httpResponse;
38+
}
39+
catch(Exception e)
40+
{
41+
throw e;
3642
}
37-
return httpResponse;
38-
}
39-
40-
private void ThrowExceptionForHttpError(HttpErrorDetails httpError)
41-
{
42-
throw new HttpRequestException(httpError.ToString());
4343
}
4444
}
4545
}

dotnet/InkRecognition/wpf-app/src/Helpers/InkPointHelper.cs

Lines changed: 0 additions & 49 deletions
This file was deleted.

dotnet/InkRecognition/wpf-app/src/JSON/Converter/InkPointsToStringConverter.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
using Newtonsoft.Json;
2-
using Contoso.NoteTaker.Helpers;
32
using System;
43
using System.Collections.Generic;
54
using Windows.UI.Input.Inking;
5+
using System.Linq;
6+
using System.Reflection;
67

78
namespace Contoso.NoteTaker.JSON.Converter
89
{
910
public class InkPointsToStringConverter : JsonConverter
1011
{
1112
public override bool CanConvert(Type objectType)
1213
{
13-
throw new NotImplementedException();
14+
return typeof(IReadOnlyList<InkPoint>).IsAssignableFrom(objectType);
1415
}
1516

1617
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
1718
{
19+
// Since CanRead flag is false, this function will not be called
1820
throw new NotImplementedException();
1921
}
2022

@@ -23,7 +25,10 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
2325
var points = value as IReadOnlyList<InkPoint>;
2426
if (points != null)
2527
{
26-
var pointsStr = InkPointHelper.InkPointsToString(points);
28+
var pointsStr = string.Join(",",
29+
points.Select(p => Convert.ToSingle(p.Position.X) + "," +
30+
Convert.ToSingle(p.Position.Y))
31+
);
2732
serializer.Serialize(writer, pointsStr);
2833
}
2934
else

dotnet/InkRecognition/wpf-app/src/JSON/Converter/InkRecognitionResponseConverter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public override bool CanConvert(Type objectType)
1515

1616
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
1717
{
18+
// Since CanWrite flag is false, this function will not be called
1819
throw new NotImplementedException();
1920
}
2021

dotnet/InkRecognition/wpf-app/src/JSON/Format/HttpErrorDetails.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,9 @@ public class HttpErrorDetails
1919
public override string ToString()
2020
{
2121
string msg = "";
22-
if (ErrorCode != null)
23-
{
24-
msg += "Http Error code: " + ErrorCode;
25-
}
26-
27-
if (Target != null)
28-
{
29-
msg += " Target: " + Target;
30-
}
31-
32-
if (Message != null)
33-
{
34-
msg += " Message : " + Message;
35-
}
22+
msg += (ErrorCode != null) ? " Http Error code : " + ErrorCode : "";
23+
msg += (Target != null) ? " Target : " + Target : "";
24+
msg += (Message != null) ? " Message : " + Message : "";
3625

3726
if (Details != null)
3827
{

dotnet/InkRecognition/wpf-app/src/JSON/Format/InkRecognitionUnit.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using Newtonsoft.Json;
2-
using Contoso.NoteTaker.JSON.Converter;
2+
using System;
33
using System.Collections.Generic;
44
using System.Runtime.Serialization;
5-
using Windows.Foundation;
6-
using System;
75

86
namespace Contoso.NoteTaker.JSON.Format
97
{

dotnet/InkRecognition/wpf-app/src/JSON/Format/InkRecognizerStroke.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-

2-
using Contoso.NoteTaker.Helpers;
3-
using Contoso.NoteTaker.JSON.Converter;
1+
using Contoso.NoteTaker.JSON.Converter;
42
using Newtonsoft.Json;
53
using System;
64
using System.Collections.Generic;
75
using System.Runtime.Serialization;
6+
using Windows.Foundation;
87
using Windows.UI.Input.Inking;
98

109
namespace Contoso.NoteTaker.JSON.Format
1110
{
1211
public class InkRecognizerStroke
1312
{
1413
[JsonIgnore]
15-
private InkStroke InkStroke { get; set; }
14+
private InkStroke inkStrokeInternal { get; set; }
1615

1716
[JsonProperty(PropertyName = "id")]
18-
public UInt64 Id { get { return InkStroke.Id; } }
17+
public UInt64 Id { get { return inkStrokeInternal.Id; } }
1918

2019
[JsonProperty(PropertyName = "language", NullValueHandling = NullValueHandling.Ignore)]
2120
public string Language { get; set; } = null;
@@ -26,20 +25,31 @@ public class InkRecognizerStroke
2625
[JsonProperty(PropertyName = "points"), JsonConverter(typeof(InkPointsToStringConverter))]
2726
public IReadOnlyList<InkPoint> Points { get; protected set; }
2827

29-
[JsonProperty(PropertyName = "drawingAttributes")]
30-
public InkDrawingAttributes DrawingAttributes { get { return InkStroke.DrawingAttributes; } }
31-
3228
public InkRecognizerStroke(InkStroke stroke, float DpiX, float DpiY)
3329
{
34-
InkStroke = stroke;
30+
inkStrokeInternal = stroke;
3531

36-
var pointsInPixels = GetInkPoints();
37-
Points = InkPointHelper.ConvertPixelsToMillimeters(pointsInPixels, DpiX, DpiY).AsReadOnly();
32+
var pointsInPixels = inkStrokeInternal.GetInkPoints();
33+
Points = ConvertPixelsToMillimeters(pointsInPixels, DpiX, DpiY).AsReadOnly();
3834
}
3935

40-
public IReadOnlyList<InkPoint> GetInkPoints()
36+
private List<InkPoint> ConvertPixelsToMillimeters(IReadOnlyList<InkPoint> pointsInPixels, float DpiX, float DpiY)
4137
{
42-
return InkStroke.GetInkPoints();
38+
var transformedInkPoints = new List<InkPoint>();
39+
const float inchToMillimeterFactor = 25.4f;
40+
41+
42+
foreach (var point in pointsInPixels)
43+
{
44+
var transformedX = (point.Position.X / DpiX) * inchToMillimeterFactor;
45+
var transformedY = (point.Position.Y / DpiY) * inchToMillimeterFactor;
46+
var transformedPoint = new Point(transformedX, transformedY);
47+
var transformedInkPoint = new InkPoint(transformedPoint, point.Pressure);
48+
49+
transformedInkPoints.Add(transformedInkPoint);
50+
}
51+
52+
return transformedInkPoints;
4353
}
4454
}
4555

dotnet/InkRecognition/wpf-app/src/JSON/Format/RecognitionUnitKind.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ namespace Contoso.NoteTaker.JSON.Format
55
public enum RecognitionUnitKind
66
{
77
[EnumMember(Value = "writingRegion")]
8-
InkWritingRegion,
8+
WritingRegion,
99

1010
[EnumMember(Value = "paragraph")]
11-
InkParagraph,
11+
Paragraph,
1212

1313
[EnumMember(Value = "line")]
14-
InkLine,
14+
Line,
1515

1616
[EnumMember(Value = "inkWord")]
1717
InkWord,
@@ -20,7 +20,7 @@ public enum RecognitionUnitKind
2020
InkDrawing,
2121

2222
[EnumMember(Value = "listItem")]
23-
InkListItem,
23+
ListItem,
2424

2525
[EnumMember(Value = "inkBullet")]
2626
InkBullet,

dotnet/InkRecognition/wpf-app/src/JSON/Format/Rectangle.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ namespace Contoso.NoteTaker.JSON.Format
55
public class Rectangle
66
{
77
[JsonProperty(PropertyName = "topX")]
8-
public float topX { get; set; }
8+
public float TopX { get; set; }
99

1010
[JsonProperty(PropertyName = "topY")]
11-
public float topY { get; set; }
11+
public float TopY { get; set; }
1212

1313
[JsonProperty(PropertyName = "width")]
14-
public float width { get; set; }
14+
public float Width { get; set; }
1515

1616
[JsonProperty(PropertyName = "height")]
17-
public float height { get; set; }
17+
public float Height { get; set; }
1818
}
1919
}

dotnet/InkRecognition/wpf-app/src/JSON/JSONProcessor.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static InkRecognitionRoot ParseInkRecognizerResponse(string responseJson)
2828
try
2929
{
3030
var responseObj = JsonConvert.DeserializeObject<InkRecognitionResponse>(responseJson,
31-
new InkRecognitionResponseConverter());
31+
new InkRecognitionResponseConverter());
3232
var result = new InkRecognitionRoot(responseObj);
3333
return result;
3434
}
@@ -40,8 +40,16 @@ public static InkRecognitionRoot ParseInkRecognizerResponse(string responseJson)
4040

4141
public static HttpErrorDetails ParseInkRecognitionError(string errorJson)
4242
{
43-
var error = JsonConvert.DeserializeObject<HttpErrorDetails>(errorJson);
44-
return error;
43+
try
44+
{
45+
var error = JsonConvert.DeserializeObject<HttpErrorDetails>(errorJson);
46+
return error;
47+
}
48+
catch(Exception e)
49+
{
50+
throw new JsonReaderException(e.Message);
51+
}
52+
4553
}
4654
}
4755
}

0 commit comments

Comments
 (0)