private Camera openCamera(){Camera camera = null;try{Class<?> cameraClass = Class.forName("android.hardware.Camera");Object cameraInfo = null;Field field = null;int cameraCount = 0;Method getNumberOfCamerasMethod = cameraClass.getMethod("getNumberOfCameras");if(getNumberOfCamerasMethod != null){cameraCount = (Integer) getNumberOfCamerasMethod.invoke(null, (Object[]) null);Log.i(TAG, "cameraCount: " + cameraCount);}Class<?> cameraInfoClass = Class.forName("android.hardware.Camera$CameraInfo");if(cameraInfoClass != null){cameraInfo = cameraInfoClass.newInstance();}if(cameraInfo != null){field = cameraInfo.getClass().getField("facing");}Method getCameraInfoMethod = cameraClass.getMethod("getCameraInfo",Integer.TYPE,cameraInfoClass);if(getCameraInfoMethod != null && cameraInfoClass != null && field != null){for(int camIndex = 0; camIndex < cameraCount; camIndex ++){getCameraInfoMethod.invoke(null, camIndex,cameraInfo);int facing = field.getInt(cameraInfo);Log.i(TAG, "facing: " + facing);Log.i(TAG, "camIndex: " + camIndex);if(facing == 0){try{Method cameraOpenMethod = cameraClass.getMethod("open", Integer.TYPE);if(cameraOpenMethod != null){//Field frontCameraId = cameraInfo.getClass().getField("CAMERA_FACING_FRONT");//if(frontCameraId != null){//int cameraId = frontCameraId.getInt(cameraInfo);//camera = (Camera) cameraOpenMethod.invoke(null, cameraId);//}//else{//camera = (Camera) cameraOpenMethod.invoke(null, camIndex);//}camera = (Camera) cameraOpenMethod.invoke(null, camIndex);    Method unlock = camera.getClass().getDeclaredMethod("unlock");    unlock.invoke(camera);}}catch (RuntimeException e) {// TODO: handle exceptionLog.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());}break;}if(facing == 1){// Camera.CameraInfo.CAMERA_FACING_FRONTtry{Method cameraOpenMethod = cameraClass.getMethod("open", Integer.TYPE);if(cameraOpenMethod != null){camera = (Camera) cameraOpenMethod.invoke(null, camIndex);    Method unlock = camera.getClass().getDeclaredMethod("unlock");    unlock.invoke(camera);}}catch (RuntimeException e) {// TODO: handle exceptionLog.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());}break;}}}}catch (ClassNotFoundException e){Log.e(TAG, "ClassNotFoundException" + e.getLocalizedMessage());}    catch (NoSuchMethodException e){    Log.e(TAG, "NoSuchMethodException" + e.getLocalizedMessage());    }    catch (NoSuchFieldException e){    Log.e(TAG, "NoSuchFieldException" + e.getLocalizedMessage());    }    catch (IllegalAccessException e){    Log.e(TAG, "IllegalAccessException" + e.getLocalizedMessage());    }    catch (InvocationTargetException e){    Log.e(TAG, "InvocationTargetException" + e.getLocalizedMessage());    }    catch (InstantiationException e){    Log.e(TAG, "InstantiationException" + e.getLocalizedMessage());    }    catch (SecurityException e){    Log.e(TAG, "SecurityException" + e.getLocalizedMessage());    }    if(camera == null){    try{    camera = Camera.open();    }catch (RuntimeException e) {// TODO: handle exception    Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());}    }return camera;}


更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)自绘输入框
  2. LinearLayout按下(pressed)或获取焦点(fo
  3. Android(安卓)TextView在XML中的属性
  4. Android之常用文件路径
  5. Android(安卓)Touch事件
  6. Android软件框架的搭建笔记
  7. 2010.11.16———android Camera 拍照的
  8. android下访问sd卡和网络的权限
  9. Android在开机时自动启动一个应用程序
  10. Android(安卓)mms 发送流程