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的BUG - 广为人知的诟病:频繁重启
  2. android游戏开发(三)触屏事件处理_手势识
  3. 三大布局的基本摆放属性总结,以及imageVIe
  4. 为什么 Android(安卓)截屏需要 root 权限
  5. 游戏开发中SurfaceView的重要作用
  6. Android内核和驱动篇-Android内核介绍
  7. Android(安卓)Socket 发送广播包的那些坑
  8. Android重量级开发之--提高android启动速
  9. Android实现pppoe拨号上网(一)概述
  10. Android的init过程详解(一)