As described in the title, learn how to open the camera and preview images on the screen. Steps for using camera:
(1) Call the open () method of camera to open the camera;
(2) Call the getparameters () method of camera to obtain the camera parameters. This method returns a camera. Parameters object;
(3) Call the setparameters () method of camera // this step is not required after android2.3.3;
(4) Call the startpreview () method of camera to start previewing the scene. before previewing the scene, call setpreviewdisplay (surfaceholder holder) method of camera to set which surfaceview is used to display the scene capture image;
(5) EndProgramCall stoppreview () of camera to end scene preview and call the release () method to release resources.
For the above process, refer to crazy android handout. This book is really good and can be quickly started. Most of the frameworks of the entire system refer to this book, and some questions are searched online.
1. Add the camera permission to the androidmanifest. xml file.
<! -- Grant the App the permission to use the camera --> <uses-Permission Android: Name = "android. permission. camera "/> <uses-feature Android: Name =" android. hardware. camera "/> <uses-feature Android: Name =" android. hardware. camera. autofocus "/>
2. Add a surfaceview to the interface layout file to display the preview image.
<Surfaceview Android: Id = "@ + ID/sview" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"/>
3. Program subject
Here we define a surfaceview variable sview to get the surfaceview component defined in the layout file. Basic questions about surfaceview, on the ForumHttp://www.apkbus.com/android-43293-1-1.htmlThis post is pretty good.
When surfaceview is created, we call initcamera () to initialize the camera and release the camera when the surfaceview is destroyed. These two times are obtained through surfaceholder. addcallback. Let's take a look at the following:CodeYou will understand.
Public class mainactivity extends activity {surfaceview sview; surfaceholder; int screenwidth, screenheight; // defines the camera used by the system; // Boolean ispreview = false during browsing; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // sets the full screen requestwindowfeature (window. feature_no_title); getwindow (). setflags (windowmanager. layoutparams. flag_fullscre En, windowmanager. layoutparams. flag_fullscreen); setcontentview (R. layout. activity_main); windowmanager WM = (windowmanager) getsystemservice (context. window_service); display = WM. getdefadisplay display (); // obtain the screen width and height screenwidth = display. getwidth (); screenheight = display. getheight (); // obtain the surfaceview component sview = (surfaceview) findviewbyid (R. id. sview); // obtain surfaceview surfaceholder surfaceh Older = sview. getholder (); // Add a callback listener surfaceholder for surfaceholder. addcallback (New callback () {@ override public void surfacechanged (surfaceholder holder, int format, int width, int height) {}@ override public void surfacecreated (surfaceholder holder) {// enable camera initcamera () ;}@ override public void surfacedestroyed (surfaceholder holder) {// If camera is not null, release the camera if (camera! = NULL) {If (ispreview) camera. stoppreview (); camera. release (); camera = NULL ;}}); // set this surfaceview to not maintain the buffer surfaceholder. settype (surfaceholder. surface_type_push_buffers);} private void initcamera () {If (! Ispreview) {Camera = camera. open ();} If (camera! = NULL &&! Ispreview) {try {Camera. parameters = camera. getparameters (); // set the size of the previewed photo parameters. setpreviewsize (screenwidth, screenheight); // four parameters are displayed per second. setpreviewframerate (4); // sets the image format parameters. setpictureformat (pixelformat. JPEG); // set the quality of the JPG photo parameters. set ("Jpeg-quality", 85); // set the Photo size parameters. setpicturesize (screenwidth, screenheight); // no further steps are required after android2.3.3 // camera. setparameters (parameters); // use surfaceview to display the camera. setpreviewdisplay (surfaceholder); // start previewing camera. startpreview (); // Auto Focus camera. autofocus (null);} catch (exception e) {e. printstacktrace () ;}ispreview = true ;}}}
In this way, you can use the camera for preview. Of course, you will find that the preview image has been converted to 90 degrees, and the direction is wrong. In the activity attribute settings of the androidmanifest. xml file, add
Android: screenorientation = "Landscape"
Set it to landscape screen. If the screen is black, check whether camera. setparameters (parameters); this step matches your system. This step is not required in the future of 2.3.3.
In this simple way, we have completed the first step of Wireless Video Monitoring and achieved camera image preview. Here we have set preview, but we have not processed the image data of each frame. Next time we will talk about how to obtain and process each frame of the camera.
The following is the source code under sdk2.3.3, which can be run on the mobile phone of my android 4.04 system. Due to the upgrade of the android development tool, the source package is different from the previous version, so I do not know whether there is a conflict with the old version. If the problem persists, you can create a project and copy the content of the three key files.
Source code download: http://files.cnblogs.com/feifei1010/CameraPreview.rar
Appendix:
Welcome to pay attention to Weibo: http://e.weibo.com/u/2975543812
# Android bus #Blog writing continues. I want to participate in MI 2 & meizu MX ~ There are also cute little dolphins, excellent disks, and book rewards ~~
See http://www.apkbus.com/android-64183-1-1.html