capture image from camera android source code
hello, android developer once again welcomes back to another android app development tutorial in this article here I am going to show you to capture an image using your won application on a device. every device contains a default camera application if the camera is not the major part of your application you can use the inbuilt camera application on a device.so here in this article, I’m going to share with you how to use an inbuilt camera app to capture an image so let’s start.
how to create
follow the step by step method to create camera activity in android studio. also, I will prove to you the source code bellow. if you haven’t installed an android studio yet, go ahead and install an android studio before you begin.
- first, open an android studio and click on start a new Android Studio Project.
- let’s give the project name is “camera activity” leave the company domain as it is and click on the Next button.
- default it select phone and tablet leave as it is and now select the minimum SDK then click on next.
- next screen select blank activity template and then click next.
- now click on finish button and your application will be auto-generated for you.
add some
First, we have to Add this line inside AndroidManifest.xml of Android Studio for permissions to create the Camera application.
now open activity_main.xml file from the \res\layout folder path. then copy the activity_main.xml write the code like as shown below.
XML (activity_main.xml)
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 |
<?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=".CameraActivity"> <ImageView android:id="@+id/imageview6" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/btn_cap" /> <Button android:id="@+id/btn_cap" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="capture" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> |
android studio code of Android Camera app
Add following code inside MainActivity.java of Android Studio:
JAVA (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 |
import android.content.Intent; import android.graphics.Bitmap; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class CameraActivity extends AppCompatActivity { private ImageView image; private Button btn; private static final int RC_Request_code = 10; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); image = (android.widget.ImageView) findViewById(R.id.imageview6); btn = (Button)findViewById(R.id.btn_cap); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { captureImage(); } }); } private void captureImage() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePictureIntent, RC_Request_code); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode,resultCode,data); if (requestCode==RC_Request_code){ if (resultCode==RESULT_OK){ Bitmap bp = (Bitmap) data.getExtras().get("data"); image.setScaleType (ImageView.ScaleType.FIT_CENTER); image.setImageBitmap(bp); } else if (resultCode==RESULT_CANCELED){ android.widget.Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show(); } } } } |
now run the app and you will get the output as shown in the screenshot.