In this example we are going to cover of basics of taking picture with you camera on your android device and show it on screen after taking it.
ButterKnife library
First we add our favorite ButterKnife library in our build.gradle app file and sync the project:
1 2 3 4 |
dependencies { implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' } |
AndroidManifest XML:
We must add camera permissions to our AndroidManifest in order to call the camera in our app:
1 2 3 4 5 6 |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.denofdevelopers.takepicture"> <uses-permission android:name="android.permission.CAMERA" /> </manifest> |
The Main Activity XML:
In this Activity, there is nothing more than to show the captured image on screen. Because of that we use only one defined ImageView, one descriptive TextView and one Button for calling the camera:
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 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:id="@+id/image" android:layout_width="0dip" android:layout_height="250dp" android:background="@color/colorPrimaryLight" android:contentDescription="@string/image_view_content_description" android:elevation="5dp" android:paddingTop="10dp" android:paddingBottom="10dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/overlayText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:elevation="7.1dp" android:text="@string/your_image" android:textAllCaps="true" android:textColor="@color/white" android:textSize="22sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="@id/image" app:layout_constraintEnd_toEndOf="@id/image" app:layout_constraintStart_toStartOf="@id/image" app:layout_constraintTop_toTopOf="@id/image" /> <TextView android:id="@+id/text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="25dp" android:layout_marginEnd="16dp" android:text="@string/click_to_take_photo" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/image" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="100dp" android:paddingStart="15dp" android:paddingEnd="15dp" android:text="@string/take_photo" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:elevation="7dp" android:text="@string/by_viktor" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </android.support.constraint.ConstraintLayout> |
The Main Activity Java:
Also in the Main Activity we call the camera on button click by starting an activity by intent for result. The app is starting the camera and receive the captured photo as a result.
1 2 3 4 5 6 7 |
// Create random int number for identifying the result request by calling the camera private static final int TAKE_PICTURE = 1111; private void startCameraIntent() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, TAKE_PICTURE); } |
In on activity result we compare if our request code (TAKE_PICTURE) is used, and by that we get the data as result for taking the photo with our camera:
1 2 3 4 5 6 7 8 9 |
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK) { Bitmap thumbnail = (Bitmap) Objects.requireNonNull(Objects.requireNonNull(data).getExtras()).get("data"); image.setImageBitmap(thumbnail); overlayTest.setVisibility(View.INVISIBLE); } } |
Of course here we have some run-time permissions for the camera (used from Android API 23) and that will be covered in later examples. We will not focus on permissions in this example.
When we get the requested image it is not received as image but as data. From that data we must extract the image and map it on Bitmap object. This is done using (data).getExtras()).get(“data”), and Objects.requireNonNull before it, is safe check if data is null. After we map it on to our Bitmap object, thumbnail, then we set it on our previously created ImageView called image by calling the setImageBitmap method and passing thumbnail as an parameter.
Here is the whole MainActivity:
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 |
public class MainActivity extends AppCompatActivity { @BindView(R.id.image) ImageView image; @BindView(R.id.overlayText) TextView overlayTest; private static final int TAKE_PICTURE = 1111; private static final int CAMERA_REQUEST_PERMISSION_CODE = 1112; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick(R.id.button) public void onGoToCameraClick() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { requestCameraPermission(); } else { startCameraIntent(); } } private void startCameraIntent() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, TAKE_PICTURE); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK) { Bitmap thumbnail = (Bitmap) Objects.requireNonNull(Objects.requireNonNull(data).getExtras()).get("data"); image.setImageBitmap(thumbnail); overlayTest.setVisibility(View.INVISIBLE); } } @TargetApi(Build.VERSION_CODES.M) private void requestCameraPermission() { requestPermissions(new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST_PERMISSION_CODE); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == CAMERA_REQUEST_PERMISSION_CODE) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { startCameraIntent(); } } } } |
You can get the whole project on GitHub /TakePictures