AnDevCon 2014: Building a Custom Camera Application

5,403 views
5,097 views

Published on

Slides from AnDevCon Boston 2014 presentation on building custom camera applications in Android.

Published in: Software

AnDevCon 2014: Building a Custom Camera Application

  1. 1. HUYEN TUE DAO PROJECT DATE BY MAY 30, 2014 SAY CHEESE BUILDING A CUSTOM CAMERA APPLICATION 360dp wrap_content ANDEVCON 2014
  2. 2. BUILDING A CUSTOM CAMERA APPLICATION • Getting Started • Setting up the Camera • Taking pictures • Camera settings • “New” features in API 14 2
  3. 3. 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, Don’t Starve, Kingdom Rush, 7 Wonders 3
  4. 4. 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 Specific to camera: querying support level from the Camera class What you can do
  5. 5. THE CUSTOM CAMERA: THE MANIFEST • Camera application hardware-driven so vital to separate critical features from optional ones. • Highly recommended to use specific <uses-feature> to specify required features. Why? • <uses-permission android:name="android.permission.CAMERA" />! - “This will automatically enforce the <uses- feature> manifest element for all camera features.” 5
  6. 6. THE CUSTOM CAMERA: THE MANIFEST • Just specifying the camera permission (without any <uses-feature> specifications) 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 <uses- feature> with android:required=false 6
  7. 7. 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 identified with face detection, confidence score APK 14+: Data objects Device camera information + settings Camera.Size width and height: picture size, video size, preview size
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. TITLE DEVICE ORIENTATION VS CAMERA ORIENTATION Natural device orientation Natural camera orientation 90° difference
  13. 13. THE CUSTOM CAMERA: ROTATION • Some thoughts on rotation: - Empirically, trying to work with camera/display rotation and configuration changes sucks: - Complicated. - Orientation changes do not coordinate well with camera orientation changes. - Can change the activity orientation change animation in API 18+). 13
  14. 14. THE CUSTOM CAMERA: CAMERA INFO • Recommendation: - Keep a fixed 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
  15. 15. 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 <uses-feature> 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
  16. 16. 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
  17. 17. 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
  18. 18. THE CUSTOM CAMERA: WORKING WITH AREAS • API 14: Camera.Area and metering areas and focus areas • Camera.Area: defines bounds within the viewfinder for the camera to use in metering and focus • Camera viewfinder/sensor has its own coordinate system different from a View’s coordinate system. • Otherwise, just like setting other camera parameters 18
  19. 19. TITLE VIEWFINDER COORDINATES VS VIEW COORDINATES 1000-1000 1000 -1000 Camera (0,0) H W View
  20. 20. THE CUSTOM CAMERA: FACE DETECTION • API 14+ • Camera.Face, Camera.FaceDetectionListener, Camera#startFaceDetection, Camera#stopFaceDetection! • Camera.Face camera coordinates of bounds of face in viewfinder; maybe left eye, right eye, mouth position; also confidence • camera/sensor coordinates -> view coordinates 20
  21. 21. THANKS SO MUCH FOR COMING! ! QUESTIONS? RANDOMLYTYPING.COM HUYEN@RANDOMLYTYPING.COM 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

×