Imtiaz
Saifullah
Android Guide
with
Imtiaz Saifullah
Email: Imtiazsaifullah.m@gmail.com
FB: facebook.com/ImtiazSaif.Khan
Ping/Text: +92-331-7370872
Imtiaz
Saifullah
Android Using Camera
Imtiaz
Saifullah Android Using Camera
Camera is useful to capture the photos and videos in our applications. By using camera api we can control the
functionalities of camera based on our requirements.
The android framework provides a two ways such as android.hardware.camera2 api and camera intent to capture
the images and videos in our application.
By using startActivityForResult() method with intent action
parameter MediaStore.ACTION_IMAGE_CAPTURE, we can take the pictures from our android
applications.
Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cInt,Image_Capture_Code);
Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872
Imtiaz
Saifullah Android Using Camera
we create an application, open activity_main.xml file from \res\layout folder path and write the code like as
shown below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Button
android:id="@+id/btnTakePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take a Photo"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/capturedImage"
android:layout_above="@+id/btnTakePicture"/>
</RelativeLayout>
Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872
Imtiaz
Saifullah Android Using Camera
we create an application, open activity_main.xml file from \res\layout folder path and write the code like as
shown below.
public class MainActivity extends AppCompatActivity {
private Button btnCapture;
private ImageView imgCapture;
private static final int Image_Capture_Code = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCapture =(Button)findViewById(R.id.btnTakePicture);
imgCapture = (ImageView) findViewById(R.id.capturedImage);
btnCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cInt,Image_Capture_Code);
} }); }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (requestCode == Image_Capture_Code) {
if (resultCode == RESULT_OK) {
Bitmap bp = (Bitmap) data.getExtras().get("data");
imgCapture.setImageBitmap(bp);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
}} }}
Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872
Imtiaz
Saifullah