Custom Camera features in Cameraapi

Source: Internet
Author: User

The first few days of the project need to use CAMERAAPI to define their own camera, the previous two-dimensional code to write their own underlying code, so summarize the use of cameraapi a few things. Now due to JDK7.0 and above version of the official document is no longer recommended to use the camera package but CAMERA2 package, but this time, the use of the camera first, as for the Camera2 and so on later.

The first is to add camera permissions, you must add Camera hardware permissions and usage features in the manifest file, where features can be selectively placed according to project requirements.

1     <uses-permissionAndroid:name= "Android.permission.CAMERA"/>2     <!--using the camera hardware feature -3     <uses-featureAndroid:name= "Android.hardware.camera"/>4     <!--Auto Focus function -5     <uses-featureAndroid:name= "Android.hardware.camera.autofocus"/>6     <!--Flash function -7     <uses-featureAndroid:name= "Android.hardware.camera.flash"/>8     <!--front-facing camera -9     <uses-featureAndroid:name= "Android.hardware.camera.front"/>

Regarding the permission and the corresponding function can refer to the article http://www.cnblogs.com/BobGo/articles/5646751.html;

I need two functions in the project, one is to add a layer of ImageView on the surfaceview of the display camera, can draw different mask layer on the ImageView, and one function is to display and save the picture after the camera takes the photo.

The first is the simple use of the camera, where you may encounter a distortion problem with the camera's preview in Surfaceview, the following will refer to the workaround:

1         Surfaceholder Surfaceholder = surfaceview.getholder (); 2         Surfaceholder.addcallback (this); 3         Surfaceholder.settype (surfaceholder.surface_type_push_buffers);

Implement the Surfaceholder.callback interface in the current class, overriding the three methods surfacecreated (), surfacechanged (), Surfacedestroy (), These three methods are the surfaceview corresponding life cycle;

Perform Camera.open ( ) in surfacecreated ( ) to return a camera object, turn on the webcam hardware, and in Surfacedestroy () , The camera object calls release () releasing the camera, and in surfacechanged () , set the camera parameters, where getbestsize () is the most suitable size to determine the phone camera hardware can be used, This algorithm is officially provided. I did not use this algorithm to set the size directly, resulting in a distorted picture that Surfaceview displayed when the test was running.

1 @Override2      Public voidsurfacecreated (Surfaceholder holder) {3Camera =Camera.open ();4     }5 6 @Override7      Public voidSurfacechanged (Surfaceholder holder,intFormatintWidthintheight) {8         //You 've got the width and height of your surface, set the camera parameters9Camera.parameters Parameters =camera.getparameters ();TenCamera.size size=getbestsize (parameters.getsupportedpreviewsizes ()); One         intw=Size.width; A         intH=Size.Height; - parameters.setpreviewsize (W, h); - parameters.setpicturesize (W, h); the //list<camera.size> vsizelist = parameters.getsupportedpicturesizes (); - //for (int num = 0; num < vsizelist.size (); num++) { - //camera.size vsize = vsizelist.get (num); - //        } + //if (t? His.getresources (). GetConfiguration (). Orientation! = Configuration.orientation_landscape) { -             //Force vertical Screen mode +Parameters.set ("Orientation", "Portrait"); A             //more than 2.2 can be used, preview display rotation at //parameters.setrotation (+); -Camera.setdisplayorientation (90); - //} else { - //Parameters.set ("orientation", "landscape"); -             //more than 2.2 can be used - //camera.setdisplayorientation (+); in //        } - camera.setparameters (parameters); to         Try { +             //Set Display - Camera.setpreviewdisplay (holder); the}Catch(IOException exception) { * camera.release (); $Camera =NULL;Panax Notoginseng         } -         //Start Preview the Camera.startpreview (); +         //Set auto Focus ACamera.autofocus (NewCamera.autofocuscallback () { the @Override +              Public voidOnautofocus (Booleansuccess, Camera camera) { -                 if(Success) { $                     //success True for focus success, changing the focus state image $                 } -             } -         }); the     } - Wuyi     PrivateCamera.size Getbestsize (list<camera.size>supportedpreviewsizes) { theCamera.size Largestsize=supportedpreviewsizes.get (0); -         intLargestarea= supportedpreviewsizes.get (0). Height*supportedpreviewsizes.get (0). width; Wu          for(Camera.size s:supportedpreviewsizes) { -             intarea=s.width*S.height; About             if(area>Largestarea) { $Largestarea=Area ; -Largestsize=s; -             } -         } A         returnlargestsize; +  the     } -  $ @Override the      Public voidsurfacedestroyed (Surfaceholder holder) { the         //release the phone camera the camera.release (); the}

You can also set up features such as Flash in surfacechanged () :

1 parameters.setflashmode (isChecked? Camera.Parameters.FLASH_MODE_ON:Camera.Parameters.FLASH_MODE_OFF);

Finally call takepicture (Camera.shuttercallback, Camera.picturecallback, Camera.picturecallback, in the click-to-Listen video Camera.picturecallback) function to complete the picture, this function can be four callback interface,Shuttercallback is the shutter press callback, here we can set the play "click" sound, such as the operation, there are three behind picturecallback interface, corresponding to three copies of the image data: The original image, scaling and compression images and JPG images, image data can be in the picturecallback interface void Onpicturetaken (byte[] data, Camera Camera) , three data corresponding three callbacks are called in the order of the parameters, usually we only care about JPG image data, at this time the first two Picturecallback interface parameters can be directly passed null;

After each call to takepicture () Gets the image, the camera stops previewing, and if you need to continue taking the picture, we need to Picturecallback the onpicturetaken () at the end of the function, the camera object calls the Startpreview () function again, and thecamera object needs to call Stoppreview () when it is not necessary to take a picture Stop the preview function;

The second function is the video image display and save, in this function may appear two questions, one is to shoot the picture horizontal screen display, the other is the upper Surfaceview above the ImageView mask layer saved to the user's phone, achieve similar to the effect of image synthesis;

About the picture horizontal screen display problem, is because the Android official thought the mobile phone screen is the correct way to open the camera, so save the picture is also a horizontal screen save, so if you want to display the picture vertically, it is necessary to rotate the picture clockwise 90 degrees, adjust to the vertical screen display mode, the specific code is as follows:

1 Private voidSetimagebyte (ImageView showImage,byte[] data) {2Bitmap bitmap= bitmapfactory.decodebytearray (data, 0, data.length);3 Showimage.setimagebitmap (bitmap);4Matrix Matrix =Showimage.getimagematrix ();5Matrix.setrotate (90);6Showimage.setimagebitmap (Bitmap.createbitmap (Bitmap, 0, 0, bitmap.getwidth (), Bitmap.getheight (), Matrix,true));7}

Another problem is to solve the problem of saving two control content at the same time, this actually thought of the method is very simple, custom a framelayout sub-layout, in this layout only add a method

1      Public Bitmap Getbitmap () {2         // setting up the cache 3         Setdrawingcacheenabled (true); 4         Builddrawingcache (); 5         // get a picture of the current screen from the cache 6         return Getdrawingcache (); 7     }

The layout is then used as the parent control to save the image at the same time in the XML layout that you use earlier.

Finally, in the Java code where you need to save the composite picture, you only need to instantiate the newly-defined Framelayout object called Getbitmap () to return to the bitmap object after compositing;

So our function can be fully realized.

Custom Camera features in Cameraapi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.