Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Camera Tutorial


  • Please log in to reply
1 reply to this topic

#1 BrandonLayton


    CC Newcomer

  • Member
  • PipPip
  • 18 posts

Posted 29 February 2012 - 04:19 PM

I recently created a tutorial on my website that goes over Android's Camera Hardware.
Here is a link to the tutorial: link

I know, at least in my personal experience, that it is quite hard to find GOOD Android tutorials out there. Especially with specific things, like the Camera option. So to appease the masses I decided to make this one of my first tutorials.

First off there are a few things you need to know about.


Camera (Hardware)

Permissions to put into AndroidManifest.xml:

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

First we have to make a new class. So you can either make a totally new .class or put this in the bottom of another (I like making seperate .classes).

public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
It needs to extend SurfaceView (self explanatory later on, since you need to DISPLAY the camera) and implement SurfaceHolder.Callback (Look at the link I put above for more information on them).

Now lets declare a couple of variables.

private SurfaceHolder holder;

 private Camera camera;

These will be used later.
public CameraView(Context context) {


	holder = this.getHolder();





super(context) just creates it. then we gotta setup the holder for later. You have to set the type so that you don't receive an Overlay error.

Now lets start initiating the methods that are needed for a SurfaceView.

surfaceChanged first shall we?
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

	Camera.Parameters params = camera.getParameters();//Getting paramaters.

	if(size!=null){ //make sure we don't pull a NullException.

		params.setPreviewSize(width, height);//gotta set the size, since we know it. 


	camera.setParameters(params);//gotta set the paramaters now. 

 	camera.startPreview();//starting the preview.


The comments should explain most of it. But basically we are setting up the camera.
public void surfaceCreated(SurfaceHolder holder) {


		camera = Camera.open();//setting the camera up.

		camera.setPreviewDisplay(holder);//making the display our current SurfaceHolder

	} catch (IOException e){

		e.printStackTrace();//printing out the error message if it happens.



Pretty self-explanatory. Gotta setup the camera when it first starts up.

public void surfaceDestroyed(SurfaceHolder holder) {

	//gotta release it and start anew.



	camera = null;


Now we gotta make a simple method that we will use later from outside the class that will return the camera.
public Camera getCamera(){

 	return camera;


Now that is our whole class. Yay! One step closer. Now lets actually use it!
Button takePicButton = (Button)findViewById(R.id.takePicButton);

takePicButton.setOnClickListener(new OnClickListener(){

	public void onClick(View v) {

		cam = cv.getCamera(); //notice how we used that method? ;)




Notice we don't have PictureCallback_? Well lets make it.

Camera.PictureCallback PictureCallback_ = new Camera.PictureCallback() {

 	public void onPictureTaken(byte[] imageData, Camera c) {

		//do what you want with the imageData. Like make it into a bmp (ill show how to below)

 		InputStream is = new ByteArrayInputStream(imageData);

		Bitmap bmp = BitmapFactory.decodeStream(is); //now do what you want with it. 



That is it! Now by using this basic setup you can easily make a camera and with further research maybe a video camera. Got questions? Go to the Contact page!

C'yall later!,

Hopefully it helps some people out :),

Edited by BrandonLayton, 04 March 2012 - 02:46 PM.

  • 0

#2 Rakso


    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 335 posts
  • Programming Language:PHP, JavaScript
  • Learning:C++, PHP, Python, JavaScript

Posted 03 April 2012 - 02:38 AM

Good tutorial!
  • 0

I am a freelance web developer & web designer. I loves to make websites and would be happy to work with you.

Check out my portfolio: http://oskarmendel.me/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download