-
Notifications
You must be signed in to change notification settings - Fork 12
/
MainActivity.java
307 lines (270 loc) · 13.2 KB
/
MainActivity.java
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
/*
Code written by ishaanjav
github.com/ishaanjav
App on Google Play Store: https://play.google.com/store/apps/details?id=app.anany.faceanalyzer
*/
package com.example.anany.emotionrecognition;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Looper;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.CircularProgressDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.microsoft.projectoxford.face.FaceServiceClient;
import com.microsoft.projectoxford.face.FaceServiceRestClient;
import com.microsoft.projectoxford.face.contract.Emotion;
import com.microsoft.projectoxford.face.contract.Face;
import com.microsoft.projectoxford.face.contract.IdentifyResult;
import com.microsoft.projectoxford.face.rest.ClientException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button process, takePicture;
ImageView imageView, hidden;
private FaceServiceClient faceServiceClient;
Bitmap mBitmap;
Boolean ready = false;
CoordinatorLayout rel;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//IMPORTANT!!------------------------------------------------------------------------------
//Replace the below tags <> with your own endpoint and API Subscription Key.
//For help with this, read the project's README file.
faceServiceClient = new FaceServiceRestClient("<YOUR ENDPOINT HERE>", "<YOUR API SUBSCRIPTION KEY>");
takePicture = findViewById(R.id.takePic);
imageView = findViewById(R.id.imageView);
hidden = findViewById(R.id.hidden);
imageView.setVisibility(View.INVISIBLE);
process = findViewById(R.id.processClick);
takePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
} else {
//IMPORTANT: You may notice that the pictures from the camera are low quality.
//TO FIX THIS: You need to request permission to write external storage that way you can access the full-quality image
//from the device, rather than a low quality thumbnail.
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 100);
} else {
if (counter == 2 || (counter > 2 && counter % 2 == 0)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
} else //Requesting storage permissions so we can get a high quality image.
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
counter++;
//Comment out the else statement above if you don't want to keep getting asked for Storage permission (assuming you deny it every time)
//OLD Code below
// Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// startActivityForResult(intent, 100);
}
}
}
});
process.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ready) {
detectandFrame(mBitmap);
} else {
makeToast("Please take a picture.");
}
}
});
rel = findViewById(R.id.rel);
final Snackbar snackBar = Snackbar.make(rel, "This is an old version of the app. The new version is available on the Play Store.", 10500);
TextView tv = (TextView) snackBar.getView().findViewById(android.support.design.R.id.snackbar_text);
tv.setMaxLines(4);
snackBar.setAction("Show", new View.OnClickListener() {
@Override
public void onClick(View v) {
// Call your action method here
snackBar.dismiss();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=app.anany.faceanalyzer"));
startActivity(browserIntent);
}
});
snackBar.setActionTextColor(Color.parseColor("#a1e6ff"));
snackBar.show();
}
Uri imageUri;
ContentValues values;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) { //High Quality picture using URI and storage
if (resultCode == RESULT_OK) {
imageView.setVisibility(View.VISIBLE);
try {
mBitmap = MediaStore.Images.Media.getBitmap(
getContentResolver(), imageUri);
Bitmap rotatedBitmap = mBitmap;
ExifInterface ei = new ExifInterface(getRealPathFromURI(imageUri));
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(mBitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(mBitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(mBitmap, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = mBitmap;
}
imageView.setImageBitmap(rotatedBitmap);
} catch (IOException e) {
//Error getting high quality image --> Use low quality thumbnail.
makeToast("Error: " + e.toString());
//mBitmap = (Bitmap) data.getExtras().get("data");
e.printStackTrace();
imageView.setImageBitmap(mBitmap);
}
ready = true;
hidden.setVisibility(View.INVISIBLE);
}
}else if(requestCode == 1 && resultCode == RESULT_OK){
//Low Quality image
imageView.setVisibility(View.VISIBLE);
mBitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(mBitmap);
ready = true;
hidden.setVisibility(View.INVISIBLE);
}
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
makeToast("Latest, improved Face Analyzer app on the Play Store");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=app.anany.faceanalyzer"));
startActivity(browserIntent);
return super.onOptionsItemSelected(item);
}
private void detectandFrame(final Bitmap mBitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
final ByteArrayInputStream inputStream = new ByteArrayInputStream((outputStream.toByteArray()));
AsyncTask<InputStream, String, Face[]> detectTask = new AsyncTask<InputStream, String, Face[]>() {
ProgressDialog pd = new ProgressDialog(MainActivity.this);
@Override
protected Face[] doInBackground(InputStream... inputStreams) {
publishProgress("Detecting...");
//This is where you specify the FaceAttributes to detect. You can change this for your own use.
FaceServiceClient.FaceAttributeType[] faceAttr = new FaceServiceClient.FaceAttributeType[]{
FaceServiceClient.FaceAttributeType.HeadPose,
FaceServiceClient.FaceAttributeType.Age,
FaceServiceClient.FaceAttributeType.Gender,
FaceServiceClient.FaceAttributeType.Emotion,
FaceServiceClient.FaceAttributeType.FacialHair
};
try {
Face[] result = faceServiceClient.detect(inputStreams[0],
true,
false,
faceAttr);
if (result == null) {
publishProgress("Detection failed. Nothing detected.");
}
publishProgress(String.format("Detection Finished. %d face(s) detected", result.length));
return result;
} catch (Exception e) {
publishProgress("Detection Failed: " + e.getMessage());
return null;
}
}
@Override
protected void onPreExecute() {
pd.show();
}
@Override
protected void onProgressUpdate(String... values) {
pd.setMessage(values[0]);
}
@Override
protected void onPostExecute(Face[] faces) {
pd.dismiss();
Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
Gson gson = new Gson();
String data = gson.toJson(faces);
if (faces == null || faces.length == 0) {
makeToast("No faces detected. You may not have added the API Key or try retaking the picture.");
} else {
intent.putExtra("list_faces", data);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
intent.putExtra("image", byteArray);
startActivity(intent);
}
}
};
detectTask.execute(inputStream);
}
private void makeToast(String s) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}