Ii. Multimedia camera
The camera function of the mobile phone is an essential function for everyone to take photos. I think it is easier to understand the explanation of a blog on the Internet. Here I will refer to the blog post, add your own problems. Referenced blog address: http://xblia.iteye.com/blog/1167964
Currently, smartphones have many powerful functions, such as cameras, GPS, and wireless Internet access. Now we are fully using these functions. In this article, we will learn together, in the Google Android programming environment, how does one take photos of Google Android cameras in the simplest way.
In this example, we need to use two files: Layout files and activity files.
[Note]: After the release of Android 1.5 (codenamed cupcake), there have been many security improvements, one of which is related to camera permission control. Before that, you can create an app that allows you to take photos without the user's permission. Now this problem has been fixed. If you want to use a camera in your application, add the following code to androidmanifest. xml:
<uses-permission android:name="android.permission.CAMERA"/>
If this authorization code is not available, the program reports the error "fail to connect to camera service.
1. Set the camera Layout
This is the basis of the development work, that is, how many auxiliary elements we want to add to the application, such as camera function buttons. In this article, we adopt the simplest method. Apart from taking photos, there is no additional camera function. Next, let's take a look at the layout file main. xml used in this example ".
<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>
This layout is very simple. There is only one linearlayout view group, and there is only one surfaceview under it, that is, our camera screen.
Tip: Remember not to use uppercase letters in the resource file name. If you name the file "camerasurface. xml", it will cause unnecessary trouble.
2. Implement the cameraactivity code of the camera
Package COM. jercy. android. camera; import Java. io. ioexception; import android. app. activity; import android. graphics. pixelformat; import android. hardware. camera; import android. OS. bundle; import android. util. log; import android. view. surfaceholder; import android. view. surfaceview; import android. view. window; import android. view. windowmanager; public class cameraactivity extends activity implements surfaceholder. Callback {Private Static final string tag = "cameraactivity"; private camera mcamera; private surfaceview msurfaceview; private surfaceholder msurfaceholder; private Boolean region = false; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); getwindow (). setformat (pixelformat. translucent); // The screen format is "Translucent ". Requestwindowfeature (window. feature_no_title); // The camera preview interface is displayed in full screen without the "title" getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); setcontentview (R. layout. main); // set the activity layout to msurfaceview (surfaceview) findviewbyid (R. id. surface_camera); msurfaceholder = msurfaceview. getholder (); // The holder msurfaceholder is obtained in surfaceview. addcal Lback (this); // and add the callback function to "this ". This means that our operation (activity) can manage this surfaceview. Msurfaceholder. settype (surfaceholder. surface_type_push_buffers);} @ overridepublic void surfacecreated (surfaceholder holder) {// todo auto-generated method stublog. I (TAG, "The surfacecreated method of cameraactivity is called"); // mcamera is an object of the "camera" class. In the surfacecreated method, we "open" the camera. This is how to start it. mcamera = camera. open () ;}@ override/*** this method prepares the camera for taking a photo, sets its parameters, and starts to start preview on the android screen. * When mpreviewrunning is set to true, it means that the camera is active and has not been disabled, so we can use it. */Public void surfacechanged (surfaceholder holder, int format, int width, int height) {// todo auto-generated method stublog. I (TAG, "The surfacechanged method of cameraactivity is called"); If (mpreviewrunning) {mcamera. stoppreview ();} camera. parameters P = mcamera. getparameters (); p. setpreviewsize (width, height); mcamera. setparameters (p); try {mcamera. setpreviewdisplay (holder);} catch (ioexception e) {e. printstac Ktrace ();} mcamera. startpreview (); mpreviewrunning = true;} @ override/*** in this way, we stop the camera and release related resources. As you can see, we set mpreviewrunning to false here, * to prevent conflicts in the surfacechanged method. This means that we have disabled the camera, and we cannot set its parameters or start image preview in the camera */Public void surfacedestroyed (surfaceholder holder) {// todo auto-generated method stublog. I (TAG, "The surfacedestroyed method of cameraactivity is called"); mcamera. stoppreview (); mpreviewrunning = false; mcamera. release ();}/*** when taking a photo, this method is called. For example, you can create an onclicklistener on the interface. When you click the screen, * call the picturecallback method. This method will provide you with an array of bytes of the image. * then you can use the bitmap and bitmapfactory classes provided by Android to convert them from the byte array to the desired image format. */Camera. picturecallback mpicturecallback = new camera. picturecallback () {public void onpicturetaken (byte [] imagedata, camera C ){}};}
Analyze the activity class and implement the surfaceholder. Callback interface:
The surfaceholder. Callback interface is used to receive information about the changes in the camera preview interface. It implements three methods:
Surfacechanged
This method is called when the format and size of the preview interface change.
Surfacecreated
This method is called when the preview interface is created during initial instantiation.
Surfacedestroyed
This method is called when the preview interface is closed.
For example, to run a program and click the cancel button to exit the program log:
11-14 08:24:10. 420: I/cameraactivity (426): The surfacecreated method of cameraactivity is called 11-14 08:24:20. 374: I/cameraactivity (426): The surfacechanged method of cameraactivity is called 11-14 08:24:32. 899: W/keycharactermap (426): no keyboard for ID 011-14 08:24:32. 899: W/keycharactermap (426): Using default keymap:/system/usr/keychars/Qwerty. KCM. bin11-14 08:24:35. 746: I/cameraactivity (426): The surfacedestroyed method of cameraactivity is called.
: