Android Camera Tutorial

Camera is use to capture the picture and record the video. In this tutorial you will learn how to create your own camera application so that you can capture images.

Step 1.

Create new application with any name from file menu option.

File → New Project → Application name → select API → Add Blank Activity → Activity Name → finish.

Step 2.

In activity_main.xml layout file design your application.

Add following Controls:-

camera tutorial

Step 3.

Import Package and write following coding in MainActivity.java class file.


    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    
    import java.io.IOException;


	public class MainActivity extends ActionBarActivity {
    ImageButton ib;
    ImageView iv;
    Button b;
    Intent i;
    final static int CameraResults=0;
    Bitmap bmp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ib=(ImageButton)findViewById(R.id.ibTakePic);
        iv=(ImageView)findViewById(R.id.ivReturnedPic);
        b=(Button)findViewById(R.id.bSetWall);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try
                {
                    getApplicationContext().setWallpaper(bmp);
                }
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        ib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
           i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
           startActivityForResult(i,CameraResults);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK)
        {
            Bundle extra=data.getExtras();
            bmp=(Bitmap)extra.get("data");
            iv.setImageBitmap(bmp);
        }

    }
      

Step 4. Final Output

You have to test this Application in your Mobile Phone. Copy apk file in your mobile and install it.

Post Your Comment