Nowadays smart phones have a great number of hardware enhancements: camera, accelerometer, GPS, Wifi... Its time to start using their whole functionality on our phone. Let`s learn how to use the phones camera. In this example, we will need to work with 2 files, the layout file and the Activity file.
Hint! → Some days ago, there was a software actualization for the 1.5 (cupcake) Android phones. It was a security improvement. One of these security changes was related with camera permission. Before this actualization, you were able to create applications that takes pictures without the users permission. Now, this issue is fixed, and if you want to use the camera in your application, you need to add the corresponding line in the AndroidManifest.xml. The line is as follows:
<uses-permission android:name="android.permission.CAMERA"/>
This is the easiest element to develop, well, it depends of how many improvements we want to add into our application, elements, buttons with camera functionality...we are going to do in the simplest way. No buttons, no extra camera functionality, just take pictures. Lets have a look to the layout we are going to use.
We can call it “camera_surface.xml”
Hint!-> Remember! You can not use Capital letters in the resources elements, if you put “CameraSurface.xml” it will give you problems.
Here it is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<SurfaceView android:id="@+id/surface_camera"
android:layout_width="fill_parent" android:layout_height="10dip"
android:layout_weight="1">
</SurfaceView>
</LinearLayout>
Very simple, eh? Just a LinearLayout (our container) and inside of it, a SurfaceView. SurfaceViews provides us a dedicated place to draw things on it, in this case, our camera screen.