-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTireLabelDecoder.cs
72 lines (60 loc) · 2.29 KB
/
TireLabelDecoder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Newtonsoft.Json;
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Tire_Label_Decoder.Types;
namespace Tire_Label_Decoder
{
public class TireLabelDecoder
{
private const string _baseUrl = @"https://eprel.ec.europa.eu/api/products/tyres/";
public TireLabel GetByQrCodeUrl(string qrCodeUrl)
{
WebClient client = new WebClient();
var stream = client.OpenRead(qrCodeUrl);
var image = Image.FromStream(stream) as Bitmap;
stream.Close();
var url = GetEprelDataFromQrCode(image);
if (!string.IsNullOrEmpty(url))
return CreateTireLabel(url);
else
return null;
}
public TireLabel GetByQrData (string qrData)
{
return CreateTireLabel(qrData);
}
private string GetEprelDataFromQrCode(Bitmap qrCode)
{
try
{
var qrCodeReader = new ZXing.QrCode.QRCodeReader();
var score = new ZXing.BitmapLuminanceSource(qrCode);
var binarizer = new ZXing.Common.HybridBinarizer(score);
var binaryBitmap = new ZXing.BinaryBitmap(binarizer);
var result = qrCodeReader.decode(binaryBitmap);
var eprelData = result.Text.Split('/').Last();
return eprelData;
}
catch (Exception ex)
{
Console.WriteLine("Cannot read qrCode " + ex.Message);
return string.Empty;
}
}
private TireLabel CreateTireLabel(string eprelCode)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(@"application/json"));
client.DefaultRequestHeaders.Add("User-Agent", @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36");
var response = client.GetStringAsync(_baseUrl + eprelCode);
var tireLabel = JsonConvert.DeserializeObject<TireLabel>(response.Result);
return tireLabel;
}
}
}