Android项目中如何实现自定义相机预览界面?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
官方文档:
public static void setCameraDisplayOrientation(Activity activity,int cameraIo, Camera camera){Camera.CameraInfo info=new Camera.CameraInfo();Camera.getCameraInfo(cameraIo,info);int rotation=activity.getWindowManager().getDefaultDisplay().getRotation();int degress=0;switch(rotation){case Surface.ROTATION_0: degress=0; break;case Surface.ROTATION_90: degress=90; break; case Surface.ROTATION_180: degress=180; break;case Surface.ROTATION_270: degress=270; break;}int result;if(info.facing=Camera.CameraInfo.CAMERA_FACING_FRONT){ result = (info.orientation + degrees) % 360; > 就是摄像头需要顺时针转过多少度才能恢复自然方向 result = (360 - result) % 360; } else { // back-facing result = (info.orientation - degrees + 360) % 360; } camera.setDisplayOrientation(result);switch (result) { case 0: case 180: setCameraSize(camera.getParameters(), getScreenWidth(), getScreenHeight()); break; case 90: case 270: setCameraSize(camera.getParameters(), getScreenHeight(), getScreenWidth()); break;}}public static void setCameraSize(Camera.Parameters parameters, int width, int height) { Map<String, List<Size>> allSizes = new HashMap<>(); String typePreview = "typePreview"; String typePicture = "typePicture"; allSizes.put(typePreview, parameters.getSupportedPreviewSizes()); allSizes.put(typePicture, parameters.getSupportedPictureSizes()); Iterator iterator = allSizes.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, List<Size>> entry = (Map.Entry<String, List<Size>>) iterator.next(); List<Size> sizes = entry.getValue(); if (sizes == null || sizes.isEmpty()) continue; ArrayList<WrapCameraSize> wrapCameraSizes = new ArrayList<>(sizes.size()); for (Size size : sizes) { WrapCameraSize wrapCameraSize = new WrapCameraSize(); wrapCameraSize.setWidth(size.width); wrapCameraSize.setHeight(size.height); wrapCameraSize.setD(Math.abs((size.width - width)) + Math.abs((size.height - height))); if (size.width == width && size.height == height) { if (typePreview.equals(entry.getKey())) { parameters.setPreviewSize(size.width, size.height); } else if (typePicture.equals(entry.getKey())) { parameters.setPictureSize(size.width, size.height); } Log.d(TAG, "best size: width=" + size.width + ";height=" + size.height); break; } wrapCameraSizes.add(wrapCameraSize); } Log.d(TAG, "wrapCameraSizes.size()=" + wrapCameraSizes.size()); Size resultSize = null; if (typePreview.equals(entry.getKey())) { resultSize = parameters.getPreviewSize(); } else if (typePicture.equals(entry.getKey())) { resultSize = parameters.getPictureSize(); } if (resultSize == null || (resultSize.width != width && resultSize.height != height)) { //找到相机Preview Size 和 Picture Size中最适合的大小 if(wrapCameraSizes.isEmpty()) continue; WrapCameraSize minCameraSize = Collections.min(wrapCameraSizes); while (!(minCameraSize.getWidth() >= width && minCameraSize.getHeight() >= height)) { wrapCameraSizes.remove(minCameraSize); if(wrapCameraSizes.isEmpty()) break; minCameraSize = null; minCameraSize = Collections.min(wrapCameraSizes); } Log.d(TAG, "best min size: width=" + minCameraSize.getWidth() + ";height=" + minCameraSize.getHeight()); if (typePreview.equals(entry.getKey())) { parameters.setPreviewSize(minCameraSize.getWidth(), minCameraSize.getHeight()); } else if (typePicture.equals(entry.getKey())) { parameters.setPictureSize(minCameraSize.getWidth(), minCameraSize.getHeight()); } } iterator.remove(); } }
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容猜你喜欢
AI推送时光机Android项目中如何实现自定义相机预览界面
后端开发2023-05-31
Android中关于自定义相机预览界面拉伸问题
后端开发2022-06-06
Android自定义相机界面的实现代码
后端开发2022-06-06
Java 项目中如何实现自定义ArrayList
后端开发2023-05-31
在spring-boot项目中如何实现自定义filter
后端开发2023-05-31
Springboot项目中如何实现异常处理自定义
后端开发2023-05-31
咦!没有更多了?去看看其它编程学习网 内容吧