Android call camera, you can write an activity, assign the relevant parameters, open the front camera can be;
To apply for the permission, add in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.CAMERA"/><uses-feature android:name= " Android.hardware.camera "android:required=" false "/><uses-feature android:name=" Android.hardware.camera.front "android:required=" false "/><uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/>
Main function, open front camera
Private Camera Openfrontfacingcameragingerbread () { int cameracount = 0; Camera cam = null; Camera.camerainfo camerainfo = new Camera.camerainfo (); Cameracount = Camera.getnumberofcameras (); for (int camidx = 0; camidx < Cameracount; camidx++) { camera.getcamerainfo (camidx, camerainfo); if (camerainfo.facing = = Camera.CameraInfo.CAMERA_FACING_FRONT) { try { cam = Camera.open (CAMIDX); Mcurrentcamindex = Camidx; } catch (RuntimeException e) { log.e (TAG, "Camera failed to open:" + e.getlocalizedmessage ());}}} return cam;}
Adjust the preview angle according to the orientation of the screen when you open it
According to the horizontal screen automatically adjust the preview direction, starting from the API level, the This method can be called when the preview is active.private static void setc Ameradisplayorientation (Activity activity,int cameraid, Camera camera) {Camera.camerainfo info = new Camera.camerai NFO (); Camera.getcamerainfo (Cameraid, info); int rotation = Activity.getwindowmanager (). Getdefaultdisplay (). Getrotation (); Degrees the angle, the picture would be rotated clockwise. Valid values is 0, and 270. The starting position is 0 (landscape). int degrees = 0; Switch (rotation) {case Surface.ROTATION_0:degrees = 0; Case Surface.ROTATION_90:degrees = 90; Break Case Surface.ROTATION_180:degrees = 180; Break Case Surface.ROTATION_270:degrees = 270; Break } int result; if (info.facing = = Camera.CameraInfo.CAMERA_FACING_FRONT) {result = (Info.orientation + degrees)% 360; result = (360-result)% 360; Compensate The Mirror} else {//back-facing result = (info.orientation -degrees + 360)% 360; } camera.setdisplayorientation (Result); }
Source code associated with this blog: http://download.csdn.net/detail/changliangdoscript/7446287