我们公司的相机是自定义的相机。在一些全面屏上有一些显示拉伸的问题。出现问题的手机有三星s8,华为mate10,并且系统是8.0的。我手机是s8,在升级8.0前确认是没有问题的,升级了8.0后拍照和扫描二维码出现了显示的图像变扁的问题。当时还以为是系统的bug。一看果然,刚升级8.0没几天,三星就由发布了一个8.0的补丁,里面的内容有修复相机bug的字样。当时还是比较开心的,以为跟新完就没事了,结果更新完后,没卵用。。。。。在打开微信的相机和扫一扫发现他们的没问题。就觉得他们应该处理过这个问题。

解决这个问题要先明白几个地方:

1. Camera.Params 的setPreviewSize方法和 setPictureSize方法

这俩个方法参数都是Size,并且这俩个size最好一致,不然就会有图像拉伸的问题。而这个size也不能自己想一个数来设置,必须获取系统支持的并且合适的值。查看系统支持的size的方法是:

Params.getSupportedPictureSizes()

Params.getSupportedPreviewSizes()

我的手机是s8他支持的对应的size如下:

05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface:  =============================================================================
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width4032   height:3024
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width4032   height:2268
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width3984   height:2988
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width3264   height:2448
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width3264   height:1836
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width3024   height:3024
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width2976   height:2976
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width2880   height:2160
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width2560   height:1920
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width2560   height:1440
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width2160   height:2160
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width2048   height:1152
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width1920   height:1080
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width1440   height:1080
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width1280   height:960
05-08 15:00:18.836 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width1280   height:720
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: PictureSizes: width640   height:480
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface:  =============================================================================
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: PreviewSizes: width:1920    height:1080
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: PreviewSizes: width:1440    height:1080
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: PreviewSizes: width:1088    height:1088
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: PreviewSizes: width:1280    height:720
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: PreviewSizes: width:960    height:720
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: PreviewSizes: width:720    height:720
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: PreviewSizes: width:720    height:480
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: PreviewSizes: width:640    height:480
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: PreviewSizes: width:352    height:288
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: PreviewSizes: width:320    height:240
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: PreviewSizes: width:176    height:144
05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface:  =============================================================================

既然不能瞎写,哪我们怎么去找一个合适的size呢?这还要个问题要注意,返回的数据有可能是正序的也有可能是倒序的。为了方便处理我们要先对其进行排序。并且,因为默认摄像头方向是水平的,我们在使用的时候要旋转90度,所以这里的width实际上是高度,heigth实际上是宽度,下面给出选择方法:

  /*** 获取pictureSize的合适值* @param list      size集合* @param th        设置的最小宽度* @param rate      传入的宽高比* @return*/public Camera.Size getPictureSize(List<Camera.Size> list, int th, float rate) {Collections.sort(list, sizeComparator);   //统一以升序的方式排列int i = 0;for (Camera.Size s : list) {if ((s.width > th) && equalRate(s, rate)) {        //合适参数的判断条件, 大于我们传入的最小高度且宽高比的差值不能超过0.2Log.i(TAG, "MakeSure Picture :w = " + s.width + " h = " + s.height);break;}i++;                                //拿到最合适的参数}if (i == list.size()) {                 //如果到最后也没有找到合适的,哪么就放宽条件去找return getBestSize(list, rate);} else {return list.get(i);                 //返回找到的合适size}}/*** 遍历所有的size,找到和传入的宽高比的差值最小的一个* @param list* @param rate* @return*/private Camera.Size getBestSize(List<Camera.Size> list, float rate) {float previewDisparity = 100;int index = 0;for (int i = 0; i < list.size(); i++) {Camera.Size cur = list.get(i);float prop = (float) cur.width / (float) cur.height;if (Math.abs(rate - prop) < previewDisparity) {previewDisparity = Math.abs(rate - prop);index = i;}}return list.get(index);}private boolean equalRate(Camera.Size s, float rate) {float r = (float) (s.width) / (float) (s.height);return Math.abs(r - rate) <= 0.2;   //传入的宽高比和size的宽高比的差不能大于0.2,要尽量的和传入的宽高比相同}private class CameraSizeComparator implements Comparator<Camera.Size> {public int compare(Camera.Size lhs, Camera.Size rhs) {if (lhs.width == rhs.width) {return 0;} else if (lhs.width > rhs.width) {return 1;} else {return -1;}}}

获取previewSize的方法一样。

2.那么在显示的时候要保证pictureSize和previewSize尽量保证一样,然后相机的预览界面也要保证尽量和这俩个size一样。我获取的合适的pictureSize和previewSize如下:

05-08 15:00:18.837 8451-8574/com.example.wangxu.testcream I/CameraInterface: suitable:picture_width:1280  picture_height:720   preview_width:1280   preview_height:720

可以发现显示是一样的,那么我设置的预览界面有多大了?

05-08 15:49:23.349 28180-28180/com.example.wangxu.testcream I/JCameraView: screenSize: width:1665.0    height:2960.0

那么他们的比值了?

1280/720 = 1.7777

2960/1665 = 2.0

这。。。将宽高比1.7的的图片显示在宽高比2.0的布局里面,所以被拉伸了。所以我们要做的就是缩放布局,让他的宽高比也到1.7就好。这里就是缩放预览控件的方法:

 private void setSuitableParams() {if (screenProp > 1.8) { //屏幕的宽高比大于1.8的时候处理float previewProp = CameraInterface.getInstance().getPreviewProp();if (previewProp == 0 || mVideoView == null) {   //获取的size宽高比return;}int measuredHeight = mVideoView.getMeasuredHeight();int measuredWidth = mVideoView.getMeasuredWidth();float clacWidth = measuredHeight / previewProp;   //计算出要显示的预览界面的宽度。ViewGroup.LayoutParams layoutParams = mVideoView.getLayoutParams();if (layoutParams == null) {layoutParams = new ViewGroup.LayoutParams((int) clacWidth, measuredHeight);}if (clacWidth > 800 && Math.abs(clacWidth - measuredWidth) > clacWidth * 0.1F) {  //计算的宽度大于 800 并且和显示正常的布局的误差超过10%layoutParams.width = (int) clacWidth;}final ViewGroup.LayoutParams finalLayoutParams = layoutParams;mVideoView.post(new Runnable() {@Overridepublic void run() {if (mVideoView != null && finalLayoutParams != null) {mVideoView.setLayoutParams(finalLayoutParams);}}});}}

需要注意的是,因为我们将预览的布局进行了宽度的方大(全面屏是高度相对于宽度变大了),所以有部分宽度是在屏幕外面了,我们实际拍摄的图片的宽度大于预览界面上图片的宽度。

最后,为啥在8.0以下全面屏没事了。到底8.0对于相机做了啥,还不知道,如果有知道的小伙伴希望告知下。

全面屏 android 自定义相机 显示拉伸处理相关推荐

  1. Android自定义相机拍照、图片裁剪的实现

    原文:Android自定义相机拍照.图片裁剪的实现 最近项目里面又要加一个拍照搜题的功能,也就是用户对着不会做的题目拍一张照片,将照片的文字使用ocr识别出来,再调用题库搜索接口搜索出来展示给用户,类 ...

  2. Android自定义相机,切换前后摄像头,照相机拍照

    Android自定义相机,简单实现切换前后摄像头,照相机拍照 Ctrl +C  Ctrl+V 可以直接 run 起来,注释比较详细;源码下载 <?xml version="1.0&qu ...

  3. android自动对焦第一次对焦,Android自定义相机实现自动对焦和手动对焦

    Android自定义相机实现自动对焦和手动对焦: 不调用系统相机,因为不同的机器打开相机呈现的界面不统一也不能满足需求. 所以为了让程序在不同的机器上呈现出统一的界面,并且可以根据需求进行布局,做了此 ...

  4. Android 自定义相机Demo 入门学习

    Android 自定义相机Demo 本文是参考网上一些自定义相机示例,再结合自己对相机的功能需求写的,基本上包含了很多基本功能,比如相机对焦.闪光灯,以及在手机预览界面上绘制自己想要绘制的图案. 话不 ...

  5. android 实现自动拍照,Android自定义相机实现定时拍照功能

    这篇博客为大家介绍Android自定义相机,并且实现倒计时拍照功能. 首先自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件: activity_main.xml andro ...

  6. android 自定义相机源码,Android 自定义相机及分析源码

    Android 自定义相机及分析源码 使用Android 系统相机的方法: 要想让应用有相机的action,咱们就必须在清单文件中做一些声明,好让系统知道,如下 action的作用就是声明action ...

  7. vivox20 html阅读器,【vivoX20评测】屏幕:全面屏也得看显示素质_vivo X20_手机评测-中关村在线...

    屏幕:全面屏也得看显示素质 vivo X20的全面屏尺寸为6.01英寸,分辨率2160*1080,屏幕尺寸比例18:9,材质为Super AMOLED.在外观部分,我们提到,vivo X20的这块屏幕 ...

  8. Android自定义相机实现定时拍照

    这篇博客为大家介绍Android自定义相机,并且实现倒计时拍照功能. 首先自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件: activity_main.xml <F ...

  9. android 自定义相机,Android自定义相机实现定时拍照功能

    这篇博客为大家介绍Android自定义相机,并且实现倒计时拍照功能. 首先自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件: activity_main.xml andro ...

最新文章

  1. Centos 7 冗余备份磁盘配置介绍
  2. 由谈退格键的实现来学习字符编码
  3. linux 档案类型s,Linux学习(四)档案与目录管理
  4. 对多态变形引擎背后方法的概述
  5. php引用代码_PHP引用是什么?php中引用的介绍(代码实例)
  6. curd什么意思中文_每日一句英译英:She's a ten什么意思?
  7. angular jwt_Angular5 JWT身份验证(Spring Boot安全性)
  8. 单片机设计时钟程序c语言,单片机电子时钟程序设计
  9. 2021年广东新高考学业水平考试成绩查询,2021年1月广东高中学业水平考试成绩查询时间及入口...
  10. ASP.NET基础教程-C#与JAVA的比较
  11. 加速Webpack-缩小文件搜索范围
  12. bootstrap jasny fileinput插件冲突问题解决
  13. C语言常用库函数(含详细用法)
  14. 树莓派做无线打印服务器,用树莓派和 CUPS 打印服务器将你的打印机变成网络打印机...
  15. 外贸常用术语_外贸常用术语总结.doc
  16. 哪些数学特征可以用来描述一组数据
  17. 关于Fortify 代码安全扫描常见问题
  18. pythonsqlite操作_在Python中操作SQLite3
  19. Python 点滴 V
  20. python迭代器和生成器

热门文章

  1. 信创办公--基于WPS的Word最佳实践系列(图文环绕方式)
  2. win10 桌面新建文件夹、重命名文件、删除文件及复制文件不能自动刷新问题的解决
  3. 王者荣耀s15服务器维护,王者荣耀s15赛季更新全部内容
  4. 命令行参数解析picocli快速使用
  5. 计算机财务管理系统是,计算机财务管理作业
  6. 女生穿不穿高跟鞋有什么区别?
  7. 机器学习十大算法之Matlab-5决策树
  8. python 矩阵类型转换_python矩阵中float转int
  9. 前端合并数组\对象的方法
  10. 大学计算机上机实验指导与测试pdf,大学计算机基础上机指导与测试-王瑞祥主编.pdf...