Building a Custom Camera Application in Android

21
HUYEN TUE DAO PROJECT DATE BY MAY 30, 2014 SAY CHEESE BUILDING A CUSTOM CAMERA APPLICATION 360dp wrap_content ANDEVCON 2014

description

Presentation slides from AnDevCon Boston 2014: "Building a Custom Camera Application in Android"

Transcript of Building a Custom Camera Application in Android

  • HUYEN TUE DAO PROJECT DATE BY MAY 30, 2014 SAY CHEESE BUILDING A CUSTOM CAMERA APPLICATION 360dp wrap_content ANDEVCON 2014
  • BUILDING A CUSTOM CAMERA APPLICATION Getting Started Setting up the Camera Taking pictures Camera settings New features in API 14 2
  • ABOUT ME Mobile developer: native Android and native iOS (personally I use Android but no I do not have a favorite) Computer Engineering, University of MD, College Park Marylander living in Colorado Gamer (video, board, card, anything): currently Dota 2, Dont Starve, Kingdom Rush, 7 Wonders 3
  • TITLE CUSTOM CAMERA CAPABILITY AND AVAILABILITY What you want to do What features the API supports Typical for Android development: Build.VERSION etc. What features the device camera has Typical for Android development: use-feature and PackageManager Specic to camera: querying support level from the Camera class What you can do
  • THE CUSTOM CAMERA: THE MANIFEST Camera application hardware-driven so vital to separate critical features from optional ones. Highly recommended to use specic to specify required features. Why? ! - This will automatically enforce the manifest element for all camera features. 5
  • THE CUSTOM CAMERA: THE MANIFEST Just specifying the camera permission (without any specications) means that a device would require the following: - a back-facing camera - a front-facing camera - auto-focus - flash If you do not need any of the above, specify with android:required=false 6
  • TITLE CAMERA CLASS AND INNER CLASSES android.hardware.Camera* Device camera client: setup + access point Preview callback Shutter callback Picture taking callback ! ! ! Auto Focus callback Zoom listener Face detection listener Camera.CameraInfo front or back, orientation, shutter disable Camera.Parameters preview, picture output, photography stuff: features/settings dependent on the device camera: flash modes, color effects, scene modes, white balance, etc. Camera.Area photography stuff: focus and metering, rectangular bounds + weight Camera.Face face stuff, bounds and feature (left eye, right eye, mouth) coordinates for a face identied with face detection, condence score APK 14+: Data objects Device camera information + settings Camera.Size width and height: picture size, video size, preview size
  • TITLE THE CUSTOM CAMERA: CAMERA SETUP Add a SurfaceView to your layout for the camera preview. Implement a SurfaceHolder.Callback to listen for #surfaceCreated, #surfaceChanged, and #surfaceDestroyed Pass the callback to the SurfaceHolder instance of the SurfaceView. Open a Camera instance: Camera#open Get the Camera.Parameters and perform any initial setup. After the preview surface has been created, call Camera#setPreviewDisplay with the SurfaceHolder. Start the preview: Camera#startPreview. wait for surface to be created
  • THE CUSTOM CAMERA: BASIC SETUP Things to note about setting up the camera preview: - The surface is destroyed when the visibility of the SurfaceView is set to View.INVISIBLE. - Camera#release will stop the preview - Camera#stopPreview nulls out callbacks, stops face detection - Camera#setPreviewDisplay should be called after the surface is created. No error, just no preview. - Any changes to the preview size must be between calls to Camera#startPreview and Camera#stopPreview 9
  • THE CUSTOM CAMERA: PICTURE TAKING When the camera is set up, call takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)! PictureCallback parameters = 3 picture formats: raw (uncompressed), postview, and JPEG. - Raw and postview availability depends on device - takePicture stops the camera preview so Camera#startPreview should be called in/after callbacks 10
  • THE CUSTOM CAMERA: CAMERA INFO CameraInfo: information about a particular device camera - orientation: angle of rotation when facing the camera for the camera image to match the natural orientation of the device - facing: camera direction - whether the shutter sound can be disabled Camera.getCameraInfo: camera IDs are indices 0 to n-1 Use CameraInfo to swap between front and back - Use PackageManager to check if a front camera exists unless you have front camera as a requirement for device - Close the current camera before swapping 11
  • TITLE DEVICE ORIENTATION VS CAMERA ORIENTATION Natural device orientation Natural camera orientation 90 difference
  • THE CUSTOM CAMERA: ROTATION Some thoughts on rotation: - Empirically, trying to work with camera/display rotation and conguration changes sucks: - Complicated. - Orientation changes do not coordinate well with camera orientation changes. - Can change the activity orientation change animation in API 18+). 13
  • THE CUSTOM CAMERA: CAMERA INFO Recommendation: - Keep a xed activity orientation. - Call Camera#setDisplayOrientation to adjust for CameraInfo.orientation. - Use the OrientationEventListener to rotate the UI. - Does mean that your application thumbnail may look sideways in the Recent Apps list. 14
  • THE CUSTOM CAMERA: CAMERA PARAMETERS Most of the fun stuff (settings and modes) is set in Camera.Parameters. - A couple of features (auto-focus and flash) have and PackageManager values. - Most other features will provide support information via methods in Camera.Parameters - Example: getMinExposureCompensation returns 0 if exposure compensation is unsupported - Several getters provide lists of valid values for features or modes that have different value ranges on different devices. - Note that API level also factors: face detection and metering areas are API 14+. 15
  • THE CUSTOM CAMERA: CAMERA PARAMETERS For the most part, Camera.Parameters can be changed while preview started and will take effect immediately. For saving/restoring settings state, handy methods: Camera.Parameters#flatten and Camera.Parameters#unflatten Important: - Always call Camera#getParameters, do not hold onto Camera.Parameters instances - To actually change parameters, set values on a Camera.Parameters instance and call Camera#setParameters 16
  • THE CUSTOM CAMERA: CAMERA PARAMETERS Random tips and observations on Camera.Parameters: - Auto-focus may cause the flash to activate depending on the camera and its drivers. - Setting a scene mode overrides other parameters so if camera parameters have UI feedback may want to call Camera#getParameters and update. Other common camera/photo features done through Camera.Parameters: - GPS coordinates and timestamps for geotagging photos. - Image size and quality - Note that image size and preview size are independent 17
  • THE CUSTOM CAMERA: WORKING WITH AREAS API 14: Camera.Area and metering areas and focus areas Camera.Area: denes bounds within the viewnder for the camera to use in metering and focus Camera viewnder/sensor has its own coordinate system different from a Views coordinate system. Otherwise, just like setting other camera parameters 18
  • TITLE VIEWFINDER COORDINATES VS VIEW COORDINATES 1000-1000 1000 -1000 Camera (0,0) H W View
  • THE CUSTOM CAMERA: FACE DETECTION API 14+ Camera.Face, Camera.FaceDetectionListener, Camera#startFaceDetection, Camera#stopFaceDetection! Camera.Face camera coordinates of bounds of face in viewnder; maybe left eye, right eye, mouth position; also condence camera/sensor coordinates -> view coordinates 20
  • THANKS SO MUCH FOR COMING! ! QUESTIONS? RANDOMLYTYPING.COM [email protected] PRESENTER CONTACT HUYEN TUE DAO THINGS TO CHECK OUT Standford Digital Image Processing Class http://www.stanford.edu/class/ee368/Android/index.html ! Android Design in Action https://www.youtube.com/watch?v=OLSa7fErTAM