-
Notifications
You must be signed in to change notification settings - Fork 447
Expand file tree
/
Copy pathMainActivity.java
More file actions
239 lines (194 loc) · 7.91 KB
/
MainActivity.java
File metadata and controls
239 lines (194 loc) · 7.91 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
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.emojify;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import timber.log.Timber;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int REQUEST_STORAGE_PERMISSION = 1;
private static final String FILE_PROVIDER_AUTHORITY = "com.example.android.fileprovider";
@BindView(R.id.image_view) ImageView mImageView;
@BindView(R.id.emojify_button) Button mEmojifyButton;
@BindView(R.id.share_button) FloatingActionButton mShareFab;
@BindView(R.id.save_button) FloatingActionButton mSaveFab;
@BindView(R.id.clear_button) FloatingActionButton mClearFab;
@BindView(R.id.title_text_view) TextView mTitleTextView;
private String mTempPhotoPath;
private Bitmap mResultsBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Bind the views
ButterKnife.bind(this);
// Set up Timber
Timber.plant(new Timber.DebugTree());
}
/**
* OnClick method for "Emojify Me!" Button. Launches the camera app.
*/
@OnClick(R.id.emojify_button)
public void emojifyMe() {
// Check for the external storage permission
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// If you do not have permission, request it
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_STORAGE_PERMISSION);
} else {
// Launch the camera if the permission exists
launchCamera();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
// Called when you request permission to read and write to external storage
switch (requestCode) {
case REQUEST_STORAGE_PERMISSION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// If you get permission, launch the camera
launchCamera();
} else {
// If you do not get permission, show a Toast
Toast.makeText(this, R.string.permission_denied, Toast.LENGTH_SHORT).show();
}
break;
}
}
}
/**
* Creates a temporary image file and captures a picture to store in it.
*/
private void launchCamera() {
// Create the capture image intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the temporary File where the photo should go
File photoFile = null;
try {
photoFile = BitmapUtils.createTempImageFile(this);
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
// Get the path of the temporary file
mTempPhotoPath = photoFile.getAbsolutePath();
// Get the content URI for the image file
Uri photoURI = FileProvider.getUriForFile(this,
FILE_PROVIDER_AUTHORITY,
photoFile);
// Add the URI so the camera can store the image
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
// Launch the camera activity
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// If the image capture activity was called and was successful
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
// Process the image and set it to the TextView
processAndSetImage();
} else {
// Otherwise, delete the temporary image file
BitmapUtils.deleteImageFile(this, mTempPhotoPath);
}
}
/**
* Method for processing the captured image and setting it to the TextView.
*/
private void processAndSetImage() {
// Toggle Visibility of the views
mEmojifyButton.setVisibility(View.GONE);
mTitleTextView.setVisibility(View.GONE);
mSaveFab.setVisibility(View.VISIBLE);
mShareFab.setVisibility(View.VISIBLE);
mClearFab.setVisibility(View.VISIBLE);
// Resample the saved image to fit the ImageView
mResultsBitmap = BitmapUtils.resamplePic(this, mTempPhotoPath);
// Detect the faces and overlay the appropriate emoji
mResultsBitmap = Emojifier.detectFacesandOverlayEmoji(this, mResultsBitmap);
// Set the new bitmap to the ImageView
mImageView.setImageBitmap(mResultsBitmap);
}
/**
* OnClick method for the save button.
*/
@OnClick(R.id.save_button)
public void saveMe() {
// Delete the temporary image file
BitmapUtils.deleteImageFile(this, mTempPhotoPath);
// Save the image
BitmapUtils.saveImage(this, mResultsBitmap);
}
/**
* OnClick method for the share button, saves and shares the new bitmap.
*/
@OnClick(R.id.share_button)
public void shareMe() {
// Delete the temporary image file
BitmapUtils.deleteImageFile(this, mTempPhotoPath);
// Save the image
BitmapUtils.saveImage(this, mResultsBitmap);
// Share the image
BitmapUtils.shareImage(this, mTempPhotoPath);
}
/**
* OnClick for the clear button, resets the app to original state.
*/
@OnClick(R.id.clear_button)
public void clearImage() {
// Clear the image and toggle the view visibility
mImageView.setImageResource(0);
mEmojifyButton.setVisibility(View.VISIBLE);
mTitleTextView.setVisibility(View.VISIBLE);
mShareFab.setVisibility(View.GONE);
mSaveFab.setVisibility(View.GONE);
mClearFab.setVisibility(View.GONE);
// Delete the temporary image file
BitmapUtils.deleteImageFile(this, mTempPhotoPath);
}
}