Creating a Camera Application in Android Example

Here is a full example of building a camera app in Android that was taken from the Google tutorial.This is the complete tested camera code all in one place with no errors. There was a few errors i got from copying and pasting the code from the Google tutorial but they are fixed in this example. I also added code to display the last image taken so the user can open the picture in a image viewer. I removed excess code without testing so comment if you are having issues so I can make corrections.

This is image for an application I created so the layout below will not include the text or ads.

This is image for an application I created so the layout below will not include the text, ads or shutter image. Check out my app in the Google Market

CameraActivity

public class CameraActivity extends Activity{

    private static Camera mCamera;
    private static CameraPreview mPreview;

    static final int MEDIA_TYPE_IMAGE = 1;

    static Context context;

    /** setting last image taken **/
    static ImageView myImage;
    static File imgFile = null; 
    int duration = Toast.LENGTH_LONG;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_preview);

        mCamera = getCameraInstance();

        context = getApplicationContext();
        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);

        myImage = (ImageView) findViewById(R.id.lastPic);
        /** if last image is clicked it will open in an image viewer app **/
        myImage.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {

				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_VIEW);
				if(imgFile == null){
					 CharSequence text = "This is only an icon, take a photo"; 
                 	 Toast toast = Toast.makeText(context, text, duration);
                 	 toast.show();
				}
				else{
				intent.setDataAndType(Uri.parse("file://" +  imgFile.getAbsolutePath()), "image/*");
				startActivity(intent);
				}
				return false;
			}
        });

	/** A safe way to get an instance of the Camera object. */
	public static Camera getCameraInstance(){
	    Camera c = null;
	    try {
	        c = Camera.open(); // attempt to get a Camera instance
	    }
	    catch (Exception e){
	        // Camera is not available (in use or does not exist)
	    }
	    return c; // returns null if camera is unavailable
	}

	public void takePhoto(View v){
        mCamera.takePicture(null, null, mPicture);
	}

	private PictureCallback mPicture = new PictureCallback() {

	    @Override
	    public void onPictureTaken(byte[] data, Camera camera) {
	        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

	        if (pictureFile == null){
	            Log.d("ERROR", "Error creating media file, check storage permissions:" );
	        }

	        try {
	            FileOutputStream fos = new FileOutputStream(pictureFile);
	            fos.write(data);
	            fos.close();

	        } catch (FileNotFoundException e) {
	            Log.d("ERROR", "File not found: " + e.getMessage());
	        } catch (IOException e) {
	            Log.d("ERROR", "Error accessing file: " + e.getMessage());
	        }
	        setImage();

	        mCamera.startPreview();
	    }
	};

	/** Create a File for saving an image or video */
	private static File getOutputMediaFile(int type){

        Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
        if(!isSDPresent)
        {	
        	int duration = Toast.LENGTH_LONG;

        	Toast toast = Toast.makeText(context, "card not mounted", duration);
        	toast.show();

        	Log.d("ERROR", "Card not mounted");
        }
	    File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getPath() + "/cameraSpeed/");

	    if (! mediaStorageDir.exists()){
	        if (! mediaStorageDir.mkdirs()){

	            Log.d("MyCameraApp", "failed to create directory");
	            return null;
	        }
	    }

	    // Create a media file name
	    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
	    File mediaFile;
	    if (type == MEDIA_TYPE_IMAGE){
	        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
	        "IMG_"+ timeStamp + ".jpg");
	        imgFile = mediaFile;
	    } else {
	        return null;
	    }

	    return mediaFile;
	}

	public static void setImage(){
          if(imgFile !=null){
	        if(imgFile.exists()){
		    Bitmap myBitmap = decodeSampleImage(imgFile, 100, 100); // prevents memory out of memory exception
	            myImage.setImageBitmap(myBitmap);
	        }
          }
	}

    @Override
    protected void onPause() {
        super.onPause();
        releaseCamera();              // release the camera immediately on pause event
    }

    @Override
    protected void onResume() {
        super.onResume();  
        if(mCamera == null){
        mCamera = getCameraInstance();

        context = getApplicationContext();
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
        }
    }

    private void releaseCamera(){
        if (mCamera != null){
            mPreview.getHolder().removeCallback(mPreview);
            mCamera.release();
            mCamera = null;

        }
    }

    public static Bitmap decodeSampleImage(File f, int width, int height) { 	    
        try { 	        
             System.gc(); // First of all free some memory 	        // Decode image size 	        
             BitmapFactory.Options o = new BitmapFactory.Options(); 	        
              o.inJustDecodeBounds = true; 	        
              BitmapFactory.decodeStream(new FileInputStream(f), null, o); 	        // The new size we want to scale to 	                            final int requiredWidth = width; 	        
final int requiredHeight = height; 	        // Find the scale value (as a power of 2) 	        
int sampleScaleSize = 1; 	        
while (o.outWidth / sampleScaleSize / 2 >= requiredWidth && o.outHeight / sampleScaleSize / 2 >= requiredHeight)
	            sampleScaleSize *= 2;

	        // Decode with inSampleSize

	        BitmapFactory.Options o2 = new BitmapFactory.Options();
	        o2.inSampleSize = sampleScaleSize;

	        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
	    } catch (Exception e) {
	      //  Log.d(TAG, e.getMessage()); // We don't want the application to just throw an exception
	    }

	    return null;
	}
}

CameraPreview

package com.nathanhaze.speedcamera;

import java.io.IOException;

import android.content.Context;
import android.graphics.Canvas;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    private GlobalVar  gv = new GlobalVar();

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
        //	setWillNotDraw(false); 
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d("ERROR", "Error setting camera preview: " + e.getMessage());
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
    //	Paint temp = gv.getPaint();
    //	temp.setTextSize(150);
  	  //  canvas.drawText(Integer.toString(gv.getSpeed()), 50, 100, gv.getPaint());
        Log.w(this.getClass().getName(), "On Draw Called");
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d("ERROR", "Error starting camera preview: " + e.getMessage());
        }
    }
}

PhotoHandler

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Environment;
import android.widget.Toast;

public class PhotoHandler implements PictureCallback {

  private final Context context;

  public PhotoHandler(Context context) {
    this.context = context;
  }

  @Override
  public void onPictureTaken(byte[] data, Camera camera) {

    File pictureFileDir = getDir();

    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {

     // Log.d(MakePhotoActivity.DEBUG_TAG, "Can't create directory to save image.");
      Toast.makeText(context, "Can't create directory to save image.",
          Toast.LENGTH_LONG).show();
      return;

    }

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
    String date = dateFormat.format(new Date());
    String photoFile = "Picture_" + date + ".jpg";

    String filename = pictureFileDir.getPath() + File.separator + photoFile;

    File pictureFile = new File(filename);

    try {
      FileOutputStream fos = new FileOutputStream(pictureFile);
      fos.write(data);
      fos.close();
      Toast.makeText(context, "New Image saved:" + photoFile,
          Toast.LENGTH_LONG).show();
    } catch (Exception error) {
   //   Log.d(MakePhotoActivity.DEBUG_TAG, "File" + filename + "not saved: "
     //     + error.getMessage());
      Toast.makeText(context, "Image could not be saved.",
          Toast.LENGTH_LONG).show();
    }
  }

  private File getDir() {
    File sdDir = Environment
      .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return new File(sdDir, "CameraAPIDemo");
  }
}

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nathanhaze.speedcamera"
    android:versionCode="3"
    android:versionName="1.2" >

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

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

    <uses-feature android:name="android.hardware.camera" /]>
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >
        <activity
            android:name="com.nathanhaze.speedcamera.CameraActivity"
            android:screenOrientation="landscape">
          <intent-filter>
       		   <action android:name="android.intent.action.MAIN" />
     		   <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
        </activity>
    </application>

</manifest>

Layout

 <?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" >

         <FrameLayout
            android:id="@+id/camera_preview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

         </FrameLayout>

   <LinearLayout
      android:id="@+id/speedBackgound"
      android:layout_width="fill_parent"
      android:layout_height="80dp"
      android:layout_gravity="bottom"
      android:orientation="horizontal" >

       <ImageView
          android:id="@+id/lastPic"
          android:layout_width="70dp"
          android:layout_height="70dp"
          android:layout_gravity="center"
          android:layout_marginLeft="10dp"
          android:layout_weight="1" />

       <Button
          android:layout_marginLeft="10dp"
          android:id="@+id/button_capture"
          android:layout_width="80dp"
          android:layout_height="80dp"
          android:onClick="takePhoto" 
          android:layout_gravity="right"/>
   </LinearLayout>

 </LinearLayout>
  • Pingback: Adding Text to a Picture using a Camera Application (Android) | Android Plus More

  • Michall

    Hi. good work and app. please you can give donwload link on source codes ? CameraActivity is wrong. thx

  • nathan

    I am sorry I can’t, since I do it have a slim down version of it. What errors or problems are you running into?

  • emn

    awesome job! helped me a lot !

  • Rafaela Otte

    in camera activity its giving some errors :/

  • Ananth Venkatesh

    What is PhotoHandler, where is it being used?


Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on

  • wholesale nfl jerseys
  • wholesale nfl jerseys china
  • wholesale sports jerseys