Android 拍照
2015-10-16 16:44
429 查看
android调用camera时,可以自己写一个activity,赋上相关参数,打开前camera就可以了;
需要申请的permission,在AndroidManifest.xml中添加:
主要功能,打开前camera
根据打开时的横竖屏方向来调整preview角度
需要申请的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); }
相关文章推荐
- Android中颜色选择器及styles样式(六)
- android安全学习之4—结合java中keytool工具理解数字证书
- Android时光轴:ExpandableListview结合SwipeLayout
- Android调用Webservice发送文件
- Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x19
- Android中扫描wifi信号,收集其所有相关信息,当前连接上的wifi信息程序代码
- Android 解决屏幕适配(尺寸)
- android-Ultra-Pull-To-Refresh 源码解析
- Android中Bitmap和Drawable
- Volley使用指南第一回(来自developer.android)
- Android Studio在Ubuntu14.04下的安装与测试
- Android adjustresize全屏无效问题
- android针对json数据解析方法实例分析
- Android开发之监听或获取手机短信内容
- Android性能优化之一:ViewStub
- 推荐几个Android自定义的进度条(补充)
- android安全学习之3—java中的Key Management常用的一些概念
- 如何在Android应用中加入广告
- Android长按复制取词功能开源库MarkView,阅读器功能
- onConfigurationChanged在Android4.0中不执行解决办法