How to take a photo with the front camera and complete examples of Android calls

Source: Internet
Author: User

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

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.