Skip to content

Commit 064f2ca

Browse files
committed
Get Image from Camera
1 parent 36848d5 commit 064f2ca

File tree

7 files changed

+203
-2
lines changed

7 files changed

+203
-2
lines changed
0 Bytes
Binary file not shown.

app/src/main/AndroidManifest.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.dragosholban.androidfacedetection">
44

5+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6+
57
<application
68
android:allowBackup="true"
79
android:icon="@mipmap/ic_launcher"
@@ -16,6 +18,18 @@
1618
<category android:name="android.intent.category.LAUNCHER" />
1719
</intent-filter>
1820
</activity>
21+
22+
<provider
23+
android:name="android.support.v4.content.FileProvider"
24+
android:authorities="${applicationId}.fileprovider"
25+
android:exported="false"
26+
android:grantUriPermissions="true">
27+
<meta-data
28+
android:name="android.support.FILE_PROVIDER_PATHS"
29+
android:resource="@xml/file_paths" />
30+
</provider>
31+
32+
<activity android:name=".FaceDetectionActivity"></activity>
1933
</application>
2034

2135
</manifest>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.dragosholban.androidfacedetection;
2+
3+
import android.content.Intent;
4+
import android.graphics.Bitmap;
5+
import android.graphics.BitmapFactory;
6+
import android.graphics.Matrix;
7+
import android.media.ExifInterface;
8+
import android.support.v7.app.AppCompatActivity;
9+
import android.os.Bundle;
10+
import android.widget.ImageView;
11+
import android.widget.Toast;
12+
13+
import java.io.IOException;
14+
15+
public class FaceDetectionActivity extends AppCompatActivity {
16+
17+
@Override
18+
protected void onCreate(Bundle savedInstanceState) {
19+
super.onCreate(savedInstanceState);
20+
setContentView(R.layout.activity_face_detection);
21+
22+
Intent intent = getIntent();
23+
final ImageView imageView = findViewById(R.id.imageView);
24+
final String mCurrentPhotoPath = intent.getStringExtra("mCurrentPhotoPath");
25+
26+
// run image related code after the view was laid out
27+
// to have all dimensions calculated
28+
imageView.post(new Runnable() {
29+
@Override
30+
public void run() {
31+
if (mCurrentPhotoPath != null) {
32+
Bitmap bitmap = getBitmapFromPathForImageView(mCurrentPhotoPath, imageView);
33+
imageView.setImageBitmap(bitmap);
34+
}
35+
}
36+
});
37+
}
38+
39+
private Bitmap getBitmapFromPathForImageView(String mCurrentPhotoPath, ImageView imageView) {
40+
// Get the dimensions of the View
41+
int targetW = imageView.getWidth();
42+
int targetH = imageView.getHeight();
43+
44+
// Get the dimensions of the bitmap
45+
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
46+
bmOptions.inJustDecodeBounds = true;
47+
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
48+
int photoW = bmOptions.outWidth;
49+
int photoH = bmOptions.outHeight;
50+
51+
// Determine how much to scale down the image
52+
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
53+
54+
// Decode the image file into a Bitmap sized to fill the View
55+
bmOptions.inJustDecodeBounds = false;
56+
bmOptions.inSampleSize = scaleFactor;
57+
bmOptions.inPurgeable = true;
58+
59+
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
60+
Bitmap rotatedBitmap = bitmap;
61+
62+
// rotate bitmap if needed
63+
try {
64+
ExifInterface ei = new ExifInterface(mCurrentPhotoPath);
65+
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
66+
switch (orientation) {
67+
case ExifInterface.ORIENTATION_ROTATE_90:
68+
rotatedBitmap = rotateImage(bitmap, 90);
69+
break;
70+
case ExifInterface.ORIENTATION_ROTATE_180:
71+
rotatedBitmap = rotateImage(bitmap, 180);
72+
break;
73+
case ExifInterface.ORIENTATION_ROTATE_270:
74+
rotatedBitmap = rotateImage(bitmap, 270);
75+
break;
76+
}
77+
} catch (IOException e) {
78+
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
79+
}
80+
81+
return rotatedBitmap;
82+
}
83+
84+
public static Bitmap rotateImage(Bitmap source, float angle) {
85+
Matrix matrix = new Matrix();
86+
matrix.postRotate(angle);
87+
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
88+
}
89+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,83 @@
11
package com.dragosholban.androidfacedetection;
22

3+
import android.Manifest;
4+
import android.content.Intent;
5+
import android.content.pm.PackageManager;
6+
import android.net.Uri;
7+
import android.os.Environment;
8+
import android.provider.MediaStore;
9+
import android.support.v4.app.ActivityCompat;
10+
import android.support.v4.content.ContextCompat;
11+
import android.support.v4.content.FileProvider;
312
import android.support.v7.app.AppCompatActivity;
413
import android.os.Bundle;
14+
import android.view.View;
15+
import android.widget.Toast;
16+
17+
import java.io.File;
18+
import java.io.IOException;
19+
import java.text.SimpleDateFormat;
20+
import java.util.Date;
521

622
public class MainActivity extends AppCompatActivity {
723

24+
String mCurrentPhotoPath;
25+
26+
private static final int REQUEST_IMAGE_CAPTURE = 1;
27+
private static final int REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE = 2;
28+
829
@Override
930
protected void onCreate(Bundle savedInstanceState) {
1031
super.onCreate(savedInstanceState);
1132
setContentView(R.layout.activity_main);
1233
}
34+
35+
public void onImageFromCameraClick(View view) {
36+
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
37+
if (intent.resolveActivity(getPackageManager()) != null) {
38+
File photoFile = null;
39+
try {
40+
photoFile = createImageFile();
41+
} catch (IOException e) {
42+
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG);
43+
}
44+
45+
if (photoFile != null) {
46+
Uri photoUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".fileprovider", photoFile);
47+
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
48+
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
49+
}
50+
}
51+
}
52+
53+
private File createImageFile() throws IOException {
54+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
55+
// permission not granted, initiate request
56+
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);
57+
} else {
58+
// Create an image file name
59+
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
60+
String imageFileName = "JPEG_" + timeStamp + "_";
61+
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
62+
File image = File.createTempFile(
63+
imageFileName, /* prefix */
64+
".jpg", /* suffix */
65+
storageDir /* directory */
66+
);
67+
mCurrentPhotoPath = image.getAbsolutePath(); // save this to use in the intent
68+
69+
return image;
70+
}
71+
72+
return null;
73+
}
74+
75+
@Override
76+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
77+
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
78+
Intent intent = new Intent(this, FaceDetectionActivity.class);
79+
intent.putExtra("mCurrentPhotoPath", mCurrentPhotoPath);
80+
startActivity(intent);
81+
}
82+
}
1383
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".FaceDetectionActivity">
8+
9+
<ImageView
10+
android:id="@+id/imageView"
11+
android:layout_width="0dp"
12+
android:layout_height="0dp"
13+
android:layout_marginBottom="8dp"
14+
android:layout_marginRight="8dp"
15+
android:layout_marginLeft="8dp"
16+
android:layout_marginTop="8dp"
17+
app:layout_constraintBottom_toBottomOf="parent"
18+
app:layout_constraintEnd_toEndOf="parent"
19+
app:layout_constraintLeft_toLeftOf="parent"
20+
app:layout_constraintTop_toTopOf="parent" />
21+
22+
</android.support.constraint.ConstraintLayout>

app/src/main/res/layout/activity_main.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<TextView
9+
<Button
10+
android:id="@+id/button"
1011
android:layout_width="wrap_content"
1112
android:layout_height="wrap_content"
12-
android:text="Hello World!"
13+
android:onClick="onImageFromCameraClick"
14+
android:text="Get Image from Camera"
1315
app:layout_constraintBottom_toBottomOf="parent"
1416
app:layout_constraintLeft_toLeftOf="parent"
1517
app:layout_constraintRight_toRightOf="parent"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<paths xmlns:android="http://schemas.android.com/apk/res/android">
3+
<external-path name="my_images" path="." />
4+
</paths>

0 commit comments

Comments
 (0)