Android Open Source - AndroidHandsOn Custom Camera






From Project

Back to project page AndroidHandsOn.

License

The source code is released under:

Apache License

If you think the Android project AndroidHandsOn listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.globant.mobile.handson.media;
/*from   w ww. j a v  a  2 s  .  c  o  m*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.globant.mobile.handson.exception.CameraNotAvailableException;

public class CustomCamera extends SurfaceView implements SurfaceHolder.Callback{
  /**
   * Private attributes
   */
  private SurfaceHolder mHolder;
  private Camera mCamera;    
  private File storageDir;
  private String lastPictureTaken;
  /**
   * Static attributes
   */
  private static final int MEDIA_TYPE_IMAGE = 1;
  private static final int MEDIA_TYPE_VIDEO = 2;
  private static final String ALBUM_NAME = "HandsOn";

  public CustomCamera(Context context) throws Exception {
    super(context);    
    
    if(checkCameraHardware(context)){
      try{
        initCameraInstance();
        
      } catch(CameraNotAvailableException cnae){
        Log.d("HandsOn", "Error getting Camera: " + cnae.getMessage());
        throw new Exception(cnae);
      }
    }        
    /**
     * Install a SurfaceHolder.Callback so we get notified when
     * the underlying surface is created and destroyed.
     */
    mHolder = getHolder();
    mHolder.addCallback(this);
    /**
     * Setting required on Android versions prior to 3.0
     */
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }


  /**
   * Check if device has at least 1 camera
   * @param context
   * @return
   */
  private boolean checkCameraHardware(Context context){
    if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)){      
      return true;
    }else{
      return false;
    }
  }
  
  /**
   * Intantiates the camera
   * @throws CameraNotAvailableException
   */
  @SuppressLint("NewApi")
  public void initCameraInstance() throws CameraNotAvailableException{    
    
    try{
      if(mCamera == null)
      mCamera = Camera.open();
      
      if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
        mCamera.enableShutterSound(true);
      }
    } catch(Exception e){
      throw new CameraNotAvailableException("Camera is not available (in use or does not exist)");
    }    
  }


  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    
    if(mHolder.getSurface() == null){
      // preview surface does not exist
      return;
    }
    
    //stop preview before making changes
    try{
      mCamera.stopPreview();
    } catch(Exception e){
      //Nothing here
    }
    
    try{
      mCamera.setPreviewDisplay(holder);
      mCamera.startPreview();
    }catch(Exception e){
      Log.d("HandsOn", "Error starting camera preview: " + e.getMessage());
    }
  }


  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    try{
      mCamera.setPreviewDisplay(holder);
      mCamera.startPreview();
    }catch(IOException e){
      Log.d("HandsOn", "Error setting Camera preview: " + e.getMessage());
    }
  }


  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    releaseCamera();
  }
  
  public void releaseCamera(){
    if(mCamera != null){
      mCamera.release();
      mCamera = null;
    }
  }
  
  public void takePicture(){
    mCamera.takePicture(null, null, mPicture);
  }
  
  public void resumePreview(){
    try{
      mCamera.setPreviewDisplay(getHolder());
      mCamera.startPreview();
    }catch(IOException e){
      Log.d("HandsOn", "Error setting Camera preview: " + e.getMessage());
    }
  }
  
  public void deleteLastPictureTaken(){
    if(lastPictureTaken != null && !lastPictureTaken.isEmpty()){
      File fileToDelete = new File(lastPictureTaken);
      fileToDelete.delete();
    }
  }
  
  /**
   * Creates the file for the taken image
   * @return Image File for storage
   * @throws IOException
   */
  @SuppressLint("SimpleDateFormat")
  private File createImageFile(int type) throws IOException{
    File image = null;
    //Get the time stamp for the file's name
    String imageFileName = null;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    
    if(type == MEDIA_TYPE_IMAGE){
      //Set the storage directory
      storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), ALBUM_NAME);
      initStorageDir();
      //Create and image file name
      imageFileName = "IMG_" + timeStamp;
      image = new File(storageDir.getPath() + File.separator + imageFileName + ".jpg");
    }else if(type == MEDIA_TYPE_VIDEO){
      //Set the storage directory
      storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), ALBUM_NAME);
      initStorageDir();
      //Create and image file name
      imageFileName = "VID_" + timeStamp;
      image = new File(storageDir.getPath() + File.separator + imageFileName + ".mp4");
    }
    
    return image;
  }
  
  
  
  private void initStorageDir() throws IOException{    
    String state = Environment.getExternalStorageState();
    //Checks if the external storage is mounted
    if(Environment.MEDIA_MOUNTED.equals(state)){
      if(!storageDir.exists()){
        if(!storageDir.mkdirs()){
          Log.d("HandsOn", "Failed to create directory");
        }
      }
    }else if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
      //External storage mounted but read only, can't write on it
      throw new IOException("External memory in READ ONLY mode");
    }else{
      //External storage not available/mounted/etc
      throw new IOException("External memory not available");
    }
  }
  
  private PictureCallback mPicture = new PictureCallback(){

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
      File pictureFile;
      try {
        pictureFile = createImageFile(MEDIA_TYPE_IMAGE);
      } catch (IOException e) {
        Log.d("HandsOn", "Error creating media file, check storage permissions");
        e.printStackTrace();
        return;
      }
      
      try{
        FileOutputStream fos = new FileOutputStream(pictureFile);
        fos.write(data);
        fos.close();
        lastPictureTaken = pictureFile.getAbsolutePath();
      } catch(FileNotFoundException e){
        Log.d("HandsOn", "File not found: " + e.getMessage());
      } catch(Exception e){
        Log.d("HandsOn", "Erroe accesing file: " + e.getMessage());
      }
    }
  };
}




Java Source Code List

com.globant.mobile.handson.BaseActivity.java
com.globant.mobile.handson.CameraActivity.java
com.globant.mobile.handson.DisplayMessageActivity.java
com.globant.mobile.handson.GalleryActivity.java
com.globant.mobile.handson.ImageDetailActivity.java
com.globant.mobile.handson.ImageDetailFragment.java
com.globant.mobile.handson.ImageGrid.java
com.globant.mobile.handson.MainActivity.java
com.globant.mobile.handson.RecyclingImageView.java
com.globant.mobile.handson.exception.CameraNotAvailableException.java
com.globant.mobile.handson.media.BitmapCache.java
com.globant.mobile.handson.media.BitmapDecoder.java
com.globant.mobile.handson.media.BitmapFetcher.java
com.globant.mobile.handson.media.CustomCamera.java
com.globant.mobile.handson.media.FaceDetection.java
com.globant.mobile.handson.media.RecyclingBitmapDrawable.java
com.globant.mobile.handson.media.task.AsyncTask.java
com.globant.mobile.handson.media.task.BitmapWorker.java
com.globant.mobile.handson.media.task.MustacheWorker.java
com.globant.mobile.handson.media.task.WorkerListener.java
com.globant.mobile.handson.provider.Bitmaps.java
com.globant.mobile.handson.provider.Images.java
com.globant.mobile.handson.sensor.RotationListener.java
com.globant.mobile.handson.util.DiskLruCache.java
com.globant.mobile.handson.util.SystemUiHiderBase.java
com.globant.mobile.handson.util.SystemUiHiderHoneycomb.java
com.globant.mobile.handson.util.SystemUiHider.java