-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.cs
More file actions
319 lines (280 loc) · 11.3 KB
/
MainActivity.cs
File metadata and controls
319 lines (280 loc) · 11.3 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;
using System;
using Android.Provider;
using Android.Graphics;
using System.IO;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text;
using Android.Database;
using Android.Views;
namespace HappinessChecker
{
[DataContract]
class FaceRectangle
{
[DataMember]
public float height { get; set; }
[DataMember]
public float left { get; set; }
[DataMember]
public float top { get; set; }
[DataMember]
public float width { get; set; }
}
[DataContract]
class Scores
{
[DataMember]
public Double anger { get; set; }
[DataMember]
public Double contempt { get; set; }
[DataMember]
public Double disgust { get; set; }
[DataMember]
public Double fear { get; set; }
[DataMember]
public Double happiness { get; set; }
[DataMember]
public Double neutral { get; set; }
[DataMember]
public Double sadness { get; set; }
[DataMember]
public Double surprise { get; set; }
}
[DataContract]
class EmotionData
{
[DataMember]
public FaceRectangle faceRectangle { get; set; }
[DataMember]
public Scores scores { get; set; }
[DataMember]
public Double ID { get; set; }
}
[DataContract]
class Error
{
[DataMember]
public string code { get; set; }
[DataMember]
public string message { get; set; }
}
[DataContract]
class ErrorData
{
[DataMember]
public Error error { get; set; }
}
//Androiddマニフェスト CAMERA WRITE_EXTERNAL_STRAGE INTERNET ACCESS_CORE_LOCATION ACCESS_FINE_LOCAYIONを許可
[Activity(Label = "Happiness Checker", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
public Java.IO.File file;
public Java.IO.File dir;
public Bitmap bitmap;
ImageView imageV1;
public static readonly int PickImageId = 1000;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var btnCamera = FindViewById<Android.Widget.ImageButton>(Resource.Id.btnCamera);
btnCamera.Click += btnCamera_Click;
var btnFolder = FindViewById<Android.Widget.ImageButton>(Resource.Id.btnFolder);
btnFolder.Click += btnFolder_Click;
var btnCognitive = FindViewById<Android.Widget.ImageButton>(Resource.Id.btnCognitive);
btnCognitive.Click += btnCognitive_Click;
imageV1 = FindViewById<ImageView>(Resource.Id.imageView1);
dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
}
private void btnCamera_Click(object sender, EventArgs e)
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
file = new Java.IO.File(dir, String.Format("{0}.jpg", System.DateTime.Now.ToString("HHmmss")));
intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(file));
StartActivityForResult(intent, 0);
}
private void btnFolder_Click(object sender, EventArgs e)
{
var intent = new Intent();
intent.SetType("image/*");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), PickImageId);
}
private async void btnCognitive_Click(object sender, EventArgs e)
{
var dialog = new ProgressDialog(this);
dialog.SetTitle("Happiness Checker");
dialog.SetMessage("Recognizing");
dialog.Indeterminate = false;
dialog.SetProgressStyle(ProgressDialogStyle.Spinner);
dialog.Max = 100;
dialog.IncrementProgressBy(0);
dialog.Show();
string json = await MakeRequest(file.Path);
PersJson(json);
dialog.Dismiss();
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (resultCode == Result.Ok)
{
switch (requestCode)
{
case 0:
base.OnActivityResult(requestCode, resultCode, data);
Intent intent = new Intent(Intent.ActionMediaScannerScanFile);
GetBitmap();
break;
case 1000:
if (data != null)
{
string imagePath = null;
string[] projection = new[] { Android.Provider.MediaStore.Audio.Media.InterfaceConsts.Data };
using (ICursor cursor = ManagedQuery(data.Data, projection, null, null, null))
{
if (cursor != null)
{
int columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Audio.Media.InterfaceConsts.Data);
cursor.MoveToFirst();
imagePath = cursor.GetString(columnIndex);
file = new Java.IO.File(imagePath);
GetBitmap();
}
}
}
break;
}
}
}
private void GetBitmap()
{
var uri = Android.Net.Uri.FromFile(file);
BitmapFactory.Options options = new BitmapFactory.Options();
options.InJustDecodeBounds = true;
BitmapFactory.DecodeFile(file.Path, options);
int imageWidth = options.OutWidth;
int imageHeight = options.OutHeight;
int viewWidth = imageV1.Width;
int viewHeight = imageV1.Height;
int scale = 1;
if (imageHeight > viewHeight || imageWidth > viewWidth)
{
if (imageWidth > imageHeight)
{
scale = imageHeight / viewHeight;
}
else
{
scale = imageWidth / viewWidth;
}
}
options.InJustDecodeBounds = false;
options.InSampleSize = scale;
using (Bitmap bitmap = BitmapFactory.DecodeFile(file.Path))
{
Matrix matrix = new Matrix();
matrix.PostRotate(0);
using (Bitmap bitmapRotate = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true))
{
imageV1.SetImageBitmap(bitmapRotate);
}
}
}
private void PersJson(string json)
{
if (json != "[]")
{
var settings = new DataContractJsonSerializerSettings();
settings.UseSimpleDictionaryFormat = true;
var serializer = new DataContractJsonSerializer(typeof(List<EmotionData>), settings);
var ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
var objEmotionData = (List<EmotionData>)serializer.ReadObject(ms);
var paint = new Paint { Color = Color.Blue };
paint.SetStyle(Paint.Style.Stroke);
paint.StrokeWidth = 4;
paint.TextSize = 30;
BitmapFactory.Options options = new BitmapFactory.Options();
options.InMutable = true;
Bitmap bitmap = BitmapFactory.DecodeFile(file.Path, options);
Canvas canvas = new Canvas(bitmap);
String happinessValue = "0%";
foreach (EmotionData data in objEmotionData)
{
float top = data.faceRectangle.top;
float left = data.faceRectangle.left;
float width = data.faceRectangle.width;
float height = data.faceRectangle.height;
canvas.DrawCircle(left + width / 2, top + width / 2, width / 2, paint);
if (data.scores.happiness > 0.01)
{
happinessValue = ((data.scores.happiness) * 100).ToString("#") + "%";
}
else
{
happinessValue = "0%";
}
if (objEmotionData.Count == 1)
{
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetTitle("Happiness Level");
alert.SetMessage(happinessValue);
alert.Show();
}
else
{
canvas.DrawText(happinessValue, left + width, top + width / 4, paint);
}
}
imageV1.SetImageBitmap(bitmap);
if (objEmotionData.Count == 0)
{
var errorSerializer = new DataContractJsonSerializer(typeof(ErrorData));
var errorMs = new MemoryStream(Encoding.UTF8.GetBytes(json));
var objErrorData = (ErrorData)errorSerializer.ReadObject(errorMs);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetTitle("Error");
alert.SetMessage(objErrorData.error.message);
alert.Show();
}
}
else
{
System.Console.WriteLine("Error");
Toast.MakeText(this, "Error", ToastLength.Long).Show();
}
}
static byte[] GetImageAsByteArray(string imageFilePath)
{
using (FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
{
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
}
static async Task<string> MakeRequest(string imageFilePath)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "your key");
string uri = "https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize";
HttpResponseMessage response;
string responseContent;
// ファイル参照
byte[] byteData = GetImageAsByteArray(imageFilePath);
var content = new ByteArrayContent(byteData);
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response = await client.PostAsync(uri, content);
responseContent = await response.Content.ReadAsStringAsync();
System.Console.WriteLine(responseContent);
return responseContent;
}
}
}