Thursday, 27 August 2015

Android Programming Tutorials 24 : Camera In Android

Camera:

In this Tutorial we are discussing how Camera work in Androd.


Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: CameraApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

                                   

     1.Simply Copy and Past the Code which is display below in the  MainActivity.java 

    package com.ambilpursunil.newapp;

    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;

    public class MainActivity extends Activity implements OnClickListener {
               
                        Button b;
                        ImageView img;

                       @Override
                       protected void onCreate(Bundle savedInstanceState) {
                       super.onCreate(savedInstanceState);
                       setContentView(R.layout.activity_main);
    img = (ImageView) findViewById(R.id.snapshotImageView);
                       b = (Button) findViewById(R.id.cameraButton);
                       b.setOnClickListener(this);
                       }
                       @Override
                       public void onClick(View v) {
    Intent in = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(in, 0);
                        }
                       @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
                       if(resultCode == 0){
    Bitmap image = (Bitmap)data.getExtras().get("data");
                       img.setImageBitmap(image);
                       }else
                      if(resultCode == RESULT_CANCELED){
    Toast.makeText(getApplicationContext(), "Error Occured During Opening Camera",                     Toast.LENGTH_SHORT).show();
                    }
                    }
             }

    2.Simple Copy and Past the below code:  actvitiy_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/cameraButton"
            android:layout_width="102dp"
            android:layout_height="wrap_content"
            android:text="Capture" />

        <ImageView
            android:id="@+id/snapshotImageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher" />

    </LinearLayout>

    Note: Place any Image in drawable folde, Here i placed image.jpg image.

    3.Copy and past the code for manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.ambilpursunil.newapp"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />

        <uses-permission android:name="android.permission.INTERNET"/>

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.ambilpursunil.newapp.MainActivity"
                android:label="@string/app_name" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
            </activity>
        </application>

    </manifest>

    4.Right click on the project and Run As-->Android Application


    OutPut:

    When our application launch click on the  Capture Button Then it will redirected to our predefined Camera option of our mobile Just Capture the image it will ask to save or Delete at the bottom on the image,Click on Ok go to gallery and see the image.

    Stay Tune For Next Tutorial... Intent In Android:


    Please Send Your Comments To ambilpura.sunil@gmail.com




    No comments:

    Post a Comment