android 打开摄像头的代码
16lz
2022-06-15
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;}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer