android Zxing扫码二维码拉伸解决

2024-05-11 20:38

本文主要是介绍android Zxing扫码二维码拉伸解决,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Zxing扫描二维码:

1、拉伸问题,大家都有解决办法,参考:https://blog.csdn.net/baidu_15079577/article/details/55048077 

2、可能是源码库不同,我这边在 CameraConfigurationUtils 里面的  findBestPreviewSizeValue 修改尺寸

3、修改的位置和代码:

public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) {List<Camera.Size> rawSupportedSizes = parameters.getSupportedPreviewSizes();if (rawSupportedSizes == null) {Log.w(TAG, "Device returned no supported preview sizes; using default");Camera.Size defaultSize = parameters.getPreviewSize();if (defaultSize == null) {throw new IllegalStateException("Parameters contained no preview size!");}return new Point(defaultSize.width, defaultSize.height);}// Sort by size, descendingList<Camera.Size> supportedPreviewSizes = new ArrayList<>(rawSupportedSizes);Collections.sort(supportedPreviewSizes, new Comparator<Camera.Size>() {@Overridepublic int compare(Camera.Size a, Camera.Size b) {int aPixels = a.height * a.width;int bPixels = b.height * b.width;if (bPixels < aPixels) {return -1;}if (bPixels > aPixels) {return 1;}return 0;}});if (Log.isLoggable(TAG, Log.INFO)) {StringBuilder previewSizesString = new StringBuilder();for (Camera.Size supportedPreviewSize : supportedPreviewSizes) {previewSizesString.append(supportedPreviewSize.width).append('x').append(supportedPreviewSize.height).append(' ');}Log.i(TAG, "Supported preview sizes: " + previewSizesString);}double screenAspectRatio = screenResolution.x / (double) screenResolution.y;// Remove sizes that are unsuitableIterator<Camera.Size> it = supportedPreviewSizes.iterator();while (it.hasNext()) {Camera.Size supportedPreviewSize = it.next();int realWidth = supportedPreviewSize.width;int realHeight = supportedPreviewSize.height;if (realWidth * realHeight < MIN_PREVIEW_PIXELS) {it.remove();continue;}boolean isCandidatePortrait = realWidth < realHeight;int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth;int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight;double aspectRatio = maybeFlippedWidth / (double) maybeFlippedHeight;double distortion = Math.abs(aspectRatio - screenAspectRatio);if (distortion > MAX_ASPECT_DISTORTION) {it.remove();continue;}if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {Point exactPoint = new Point(realWidth, realHeight);Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);return exactPoint;}}// If no exact match, use largest preview size. This was not a great idea on older devices because// of the additional computation needed. We're likely to get here on newer Android 4+ devices, where// the CPU is much more powerful.if (!supportedPreviewSizes.isEmpty()) {Camera.Size largestPreview = supportedPreviewSizes.get(0);Point largestSize = new Point(largestPreview.width, largestPreview.height);Log.i(TAG, "Using largest suitable preview size: " + largestSize);return largestSize;}// If there is nothing at all suitable, return current preview sizeCamera.Size defaultPreview = parameters.getPreviewSize();if (defaultPreview == null) {throw new IllegalStateException("Parameters contained no preview size!");}//修改的位置在这里,添加如下代码,替换defaultPreview returnPoint screenResolutionForCamera = new Point();screenResolutionForCamera.x = screenResolution.x;screenResolutionForCamera.y = screenResolution.y;// preview size is always something like 480*320, other 320*480if (screenResolution.x < screenResolution.y) {screenResolutionForCamera.x = screenResolution.y;screenResolutionForCamera.y = screenResolution.x;}Point cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);Log.i(TAG, "No suitable preview sizes, using default: " + cameraResolution );return cameraResolution ;//Point defaultSize = new Point(defaultPreview.width, defaultPreview.height);//Log.i(TAG, "No suitable preview sizes, using default: " + defaultSize);//return defaultSize;
}
private static Point getCameraResolution(Camera.Parameters parameters, Point screenResolution) {String previewSizeValueString = parameters.get("preview-size-values");// saw this on Xperiaif (previewSizeValueString == null) {previewSizeValueString = parameters.get("preview-size-value");}Point cameraResolution = null;if (previewSizeValueString != null) {Log.e(TAG, "preview-size-values parameter: " + previewSizeValueString);cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution);}if (cameraResolution == null) {// Ensure that the camera resolution is a multiple of 8, as the screen may not be.cameraResolution = new Point((screenResolution.x >> 3) << 3, (screenResolution.y >> 3) << 3);}return cameraResolution;
}

这篇关于android Zxing扫码二维码拉伸解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/980597

相关文章

C#使用HttpClient进行Post请求出现超时问题的解决及优化

《C#使用HttpClient进行Post请求出现超时问题的解决及优化》最近我的控制台程序发现有时候总是出现请求超时等问题,通常好几分钟最多只有3-4个请求,在使用apipost发现并发10个5分钟也... 目录优化结论单例HttpClient连接池耗尽和并发并发异步最终优化后优化结论我直接上优化结论吧,

Python使用qrcode库实现生成二维码的操作指南

《Python使用qrcode库实现生成二维码的操作指南》二维码是一种广泛使用的二维条码,因其高效的数据存储能力和易于扫描的特点,广泛应用于支付、身份验证、营销推广等领域,Pythonqrcode库是... 目录一、安装 python qrcode 库二、基本使用方法1. 生成简单二维码2. 生成带 Log

解决systemctl reload nginx重启Nginx服务报错:Job for nginx.service invalid问题

《解决systemctlreloadnginx重启Nginx服务报错:Jobfornginx.serviceinvalid问题》文章描述了通过`systemctlstatusnginx.se... 目录systemctl reload nginx重启Nginx服务报错:Job for nginx.javas

Mysql DATETIME 毫秒坑的解决

《MysqlDATETIME毫秒坑的解决》本文主要介绍了MysqlDATETIME毫秒坑的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 今天写代码突发一个诡异的 bug,代码逻辑大概如下。1. 新增退款单记录boolean save = s

vue解决子组件样式覆盖问题scoped deep

《vue解决子组件样式覆盖问题scopeddeep》文章主要介绍了在Vue项目中处理全局样式和局部样式的方法,包括使用scoped属性和深度选择器(/deep/)来覆盖子组件的样式,作者建议所有组件... 目录前言scoped分析deep分析使用总结所有组件必须加scoped父组件覆盖子组件使用deep前言

解决Cron定时任务中Pytest脚本无法发送邮件的问题

《解决Cron定时任务中Pytest脚本无法发送邮件的问题》文章探讨解决在Cron定时任务中运行Pytest脚本时邮件发送失败的问题,先优化环境变量,再检查Pytest邮件配置,接着配置文件确保SMT... 目录引言1. 环境变量优化:确保Cron任务可以正确执行解决方案:1.1. 创建一个脚本1.2. 修

Mysql8.0修改配置文件my.ini的坑及解决

《Mysql8.0修改配置文件my.ini的坑及解决》使用记事本直接编辑my.ini文件保存后,可能会导致MySQL无法启动,因为MySQL会以ANSI编码读取该文件,解决方法是使用Notepad++... 目录Myhttp://www.chinasem.cnsql8.0修改配置文件my.ini的坑出现的问题

SpringBoot项目删除Bean或者不加载Bean的问题解决

《SpringBoot项目删除Bean或者不加载Bean的问题解决》文章介绍了在SpringBoot项目中如何使用@ComponentScan注解和自定义过滤器实现不加载某些Bean的方法,本文通过实... 使用@ComponentScan注解中的@ComponentScan.Filter标记不加载。@C

MySQL8.0找不到my.ini如何解决

《MySQL8.0找不到my.ini如何解决》在配置MySQL主从复制时,发现找不到my.ini配置文件,通过检查路径和打开隐藏文件夹,最终在C:ProgramDataMySQLMySQLSer... 目录问题描述解决方法总结问题描述今天在配置mysql主从复制的时候发现,找不到my.ini这个配置文件。

Mybatis提示Tag name expected的问题及解决

《Mybatis提示Tagnameexpected的问题及解决》MyBatis是一个开源的Java持久层框架,用于将Java对象与数据库表进行映射,它提供了一种简单、灵活的方式来访问数据库,同时也... 目录概念说明MyBATis特点发现问题解决问题第一种方式第二种方式问题总结概念说明MyBatis(原名