检查Android是否具有摄像头且为可用
2017-11-30 14:34
253 查看
最近在做个刷脸支付功能,需求是当打开刷脸支付功能之前检查一下是否具有摄像头且为可用才允许开启。在网上找了实现方法,都不十分靠谱。以下是我的实现代码,供大家参考一下,有更好方案请提出。
public static boolean checkCameraEnable() { boolean result; Camera camera = null; try { camera = Camera.open(); if (camera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { boolean connected = false; for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) { Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(camIdx) + ")"); try { camera = Camera.open(camIdx); connected = true; } catch (RuntimeException e) { Log.e(TAG, "Camera #" + camIdx + "failed to open: " + e.getLocalizedMessage()); } if (connected) { break; } } } List<Camera.Size> supportedPreviewSizes = camera.getParameters().getSupportedPreviewSizes(); result = supportedPreviewSizes != null; /* Finally we are ready to start the preview */ Log.d(TAG, "startPreview"); camera.startPreview(); } catch (Exception e) { Log.e(TAG, "Camera is not available (in use or does not exist): " + e.getLocalizedMessage()); result = false; } finally { if (camera != null) { camera.release(); } } return result; }
相关文章推荐
- 检查Android是否具有摄像头
- android判断摄像头是否可用
- android开发 弹出权限提示框 检查是否具有某个权限
- android 检查SD卡是否可用
- android 检查网络是否可用,如果不可用弹出设置,让用户改变
- Android 检查WIFI是否可用的实现方法
- Android检查当前网络状态是否可用并
- android 中检查设备是否有网络可用
- 检查网络是否可用(Android)
- Android 检查当前设备是否有摄像头
- android检查intent是否可用
- Android wiki:04.ConnectivityManager检查是否有可用网络
- android 判断摄像头是否可用(6.0以下)
- Android检测系统的摄像头是否可用
- [Android] 检查网络是否可用
- Android 检查网络是否可用
- 【Android】编程检查服务是否可用, service available programmally
- 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)
- Android之检查网络是否可用(跳转网络设置页面)
- 检查Android网络连接是否可用