您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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(); } }
先将获取手机支持预览的尺寸列表通过parmeters.getSupportPreviewSize返回的是一个集合。
进行屏幕方向的判断,因为预览的尺寸都是w>h 如果是竖屏,则需要将宽和高进行调换。
将预览尺寸列表的每个元素的宽和高与SurfaceView的宽和高进行比较,如果存在宽和高尺寸SurfaceView的宽和高,相同的size,则将当前的宽高设置为预览尺寸。
如果没有找到该步骤,则将尺寸列表的比例和SUrfaceView的比例做比较,找一个相同或者相近的 。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。