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>>
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
|
Latest postings by this author
Top Expert Articles
|