首页 > 移动平台 > 详细

android 调用前摄像头进行拍照的方法及完整例子

时间:2014-06-20 12:52:09      阅读:453      评论:0      收藏:0      [点我收藏+]

android调用camera时,可以自己写一个activity,赋上相关参数,打开前camera就可以了;


需要申请的permission,在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" />

主要功能,打开前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;
	}

根据打开时的横竖屏方向来调整preview角度

	
	//根据横竖屏自动调节preview方向,Starting from API level 14, this method can be called when preview is active.
	private static void setCameraDisplayOrientation(Activity activity,int cameraId, Camera camera) 
	{    
		   Camera.CameraInfo info = new Camera.CameraInfo(); 
	       Camera.getCameraInfo(cameraId, info);      
	       int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
	       
	       //degrees  the angle that the picture will be rotated clockwise. Valid values are 0, 90, 180, and 270. 
	       //The starting position is 0 (landscape). 
	       int degrees = 0;
	       switch (rotation) 
	       {   
	           case Surface.ROTATION_0: degrees = 0; break;         
	           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);  
	} 


本博文关联的源码下载: http://download.csdn.net/detail/changliangdoscript/7446287 

android 调用前摄像头进行拍照的方法及完整例子,布布扣,bubuko.com

android 调用前摄像头进行拍照的方法及完整例子

原文:http://blog.csdn.net/do_script/article/details/28426977

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!