18 Jun

Camera Picture Taken on Android

public class MakePhotoActivity extends Activity {

	private final static String DEBUG_TAG = "MakePhotoActivity"; 
	private Camera camera; 
	private int cameraId = 0;
        public void onClick(View view) { 
	    camera.takePicture(null, null, 
	    new PhotoHandler(getApplicationContext())); 
        }

        protected void onPause() { 
	    if (camera != null) { 
		camera.release(); 
		camera = null; 
	    } 
	    super.onPause(); 
        }
        private int findFrontFacingCamera() { 
	    int cameraId = -1; 

	    int numberOfCameras = Camera.getNumberOfCameras(); 
            for (int i = 0; i < numberOfCameras; i++) { 	
                CameraInfo info = new CameraInfo(); 	
                Camera.getCameraInfo(i, info); 
		if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 
			Log.d(DEBUG_TAG, "Camera found"); 
			cameraId = i; break; 
		} 
	} 
	return cameraId; 
}
public class PhotoHandler implements PictureCallback
{ 
	
	private final Context context; 
	public PhotoHandler(Context context) {
		this.context = context; 
	}
        private File getDir() { 
	File sdDir = Environment
		.getExternalStoragePublicDirectory(
		Environment.DIRECTORY_PICTURES); 
	return new File(sdDir, &quot;CameraAPIDemo&quot;); 
}
public void onPictureTaken(
  byte[] data, Camera camera) { 

	File pictureFileDir = getDir(); 
	if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) { 	
              Log.d(Constants.DEBUG_TAG, 
		  "Can't create directory to save image."); 
		Toast.makeText(context, 
		  "Can't create directory to save image.", 
		  Toast.LENGTH_LONG).show(); 
	return; 
} 

Leave a Reply

Your email address will not be published. Required fields are marked *