Mobile City

Join Hands to Build a Smarter India: Sign in | Join now
   Mobile City >> Expert Column
Dont Miss Experts ( 1-4 of 12 )
Camera Service in Android
Nithin M Warrier
Nithin M Warrier
Android Developer, Wipro Technologies
Nithin is an Android Developer at Wipro. He has over 5 years of experience in the Mobile Applicat... more>>
Android has camera APIs to do camera functions in Android devices. Android has lot of callback methods to open camera through application, take a picture, and store it in SD-card of the device. Here we are going to play around the how to open camera and the callback functions of Camera.

Implement camera in our application

For implementing camera in our application, first we need to get the permission, which we have to declare in manifest file as,

<uses-permission android:name=”android.permission.CAMERA” />

Open camera in application


For opening camera in application we need to implement the SurfaceHolder.callback interface, which will have three methods as onSurfaceCreated(), onSurfaceChanged() &onSurfaceDestroyed() which we need to define. In onSurfaceCreated we need to get hold of the surfaceView. For viewing camera we need SurfaceView with surfaceHolder.Callback implementation.

Sample code will look like this,





As our screen contains only camera, we can put the length and width of surfaceView as fill parent.

In surfaceCreated we need to open the camera and set the SurfaceHolder to hold the camera.Here, Camera.open() refers to the back-camera of the device. If there is no back-camera for the device then it will return null.

In surfaceChanged(), we need to set the camera parameters. Here I hard-coded the values for height & width just to show you. Then we can call startPreview(), here the camera starts.

In surfaceDestroyed(), we will release the camera resource and set it to null to inform the garbage collector that you can collect the resource.

Taking picture using camera callback

We can take picture using camera callback.

For this, we will use onTouchListener of SurfaceView. Or we can use frame layout and use a button for camera click. When we use frame layout, button will overlap surfaceView.

But in this, I am going to use onTouchListener. So where ever we pressed on the screen, picture click happens. We can put it as a bitmap and can store anywhere. If we need we can even push it to the gallery of the device.

The API for picture is takePicture() in Camera class. The parameters are, ShutterCallback and PictureCallback.

The sample code is,



takePicture() can take various forms. It is an overloaded function. Here I am showing the simplest way to use the takePicture() API. Here it is taking three parameters as shutterCallback and two PictureCallBacks for raw image and for jpeg image respectively. Here I am interested in JPEG image only. So in Camera.Parameters, I need to set picture format as JPEG as,

parameters.setPictureFormat(PixelFormat.JPEG);.


Here I am just taking the picture in a bitmap. We can do anything with this image, like storing it in sd-card of the device, insert to gallery of the device etc.

Then there are a lot of callback functions in Camera. You can play around it. Here I just showed few callbacks of Camera callbacks.



If you have any queries, you can reach the author @ nithin.warier@gmail.com


Experts on Mobile
Harshvadan  Jani
Sr. Manager
IDEA Cellular Ltd
Sumanth  S
Principal Engineer
Yahoo
Vivek  Juneja
Engineer
Torry Harris
Vishwesh  Jirgale
Chief Tech Architect
AgreeYa Mobility
Mohith  Mathur
Sr Software engineer
Symantec
Bharathi Raja M
Art Director
Trellisys
Bhuvan  Khanna
Business Head
Webonise Lab
Kedar P Bhise
Head Mobility CoE
Zensar Technologies
SiliconIndia About Us   |   Contact Us   |   Help   |   Community rules   |   Advertise with us   |   Sitemap   |   Question & Answer
News:       Technology   |   Enterprise   |   Gadgets   |   Startups   |   Finance   |   Business   |   Career   |   Magazine  |   Newsletter   |   News archive  
Cities:        CEO   |     Startup   |   Mobile   |   CIO   |   Women   |   BI   |   HR   |   SME   |   Cloud   |   Marketing   |   QA   |   Java   |   Web Developer  
Community:      Members   |   Blogs   |   Mentorship   |   Indian Entrepreneurs   |   Gyan   |   Advice   |   Community   |   Find   |   Events   |   CXO Insights  
Job Board:      Jobs   |   Freshers   |   Companies   |   HR Speak   |   Forum  
Online Courses:   Web Developer   |   Java Developer   |   CCNA Training   |   SEO   |   SAS   |   SQL Server 2005   |   J2EE
Education:   MBA   |   MCA   |   Engineering   |   Overseas Education   |   Training Institute
Life:          Real Estate   |   Travel   |   Finance   |   Gadgets   |    Products   |   Movie Reviews   |    Jokes   |   Videos 
Send your and help us continue to improve SiliconIndia
© 2013 SiliconIndia all rights reserved