美文网首页
Zxing竖屏界面

Zxing竖屏界面

作者: 取了个很好听的名字 | 来源:发表于2019-06-03 14:13 被阅读0次

简单说明

Zxing中的二维码扫描界面是横屏的,一般都会对其界面进行自定义来满足项目设计风格的需求,本文章实现了自定义界面中的竖屏界面的修改。

修改过程

首先现将manifest.xml中的CaptureActivity的screenOrientation改为portrait,实现该activity的竖屏显示


修改竖屏显示.png

在CameraManager.java文件中,修改预览缩放。主要根据长宽比对预览效果进行了分类。如果不需要横屏了,可以直接取竖屏的设置。

rect.left = framingRect.left * cameraResolution.x / screenResolution.x;
rect.right = framingRect.right * cameraResolution.x / screenResolution.x;
rect.top = framingRect.top * cameraResolution.y / screenResolution.y;
rect.bottom = framingRect.bottom * cameraResolution.y / screenResolution.y;

修改为

// 下面为竖屏模式
if (screenResolution.x < screenResolution.y) {
    rect.left = framingRect.left * cameraResolution.y / screenResolution.x;
                    rect.right = framingRect.right * cameraResolution.y / screenResolution.x;
                    rect.top = framingRect.top * cameraResolution.x / screenResolution.y;
                    rect.bottom = framingRect.bottom * cameraResolution.x / screenResolution.y;
            } else {
                    // 下面为横屏模式 
                    rect.left = framingRect.left * cameraResolution.x / screenResolution.x;
                    rect.right = framingRect.right * cameraResolution.x / screenResolution.x;
                    rect.top = framingRect.top * cameraResolution.y / screenResolution.y;
                    rect.bottom = framingRect.bottom * cameraResolution.y / screenResolution.y;
            }

在CameraManager.java文件中,修改解析函数 buildLuminanceSource(),这是本次修改的核心,因为默认的条形码解析是横屏的,在本次修改中,我们根据竖屏条件做了一些针对性的处理,主要功能是将获得数据的宽和高置换,使其适应竖屏环境。注意:如果你没有条形码的扫描该不该这里其实无所谓

public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
        Rect rect = getFramingRectInPreview();
        if (rect == null) {
            return null;
        }
       PlanarYUVLuminanceSource source;
        Point point = configManager.getScreenResolution();
        if (point.x < point.y) {
            byte[] rotatedData = new byte[data.length];
            int newWidth = height;
            int newHeight = width;
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++)
                    rotatedData[x * newWidth + newWidth - 1 - y] = data[x + y * width];
            }
            source = new PlanarYUVLuminanceSource(rotatedData, newWidth, newHeight,
                    rect.left, rect.top, rect.width(), rect.height(), false);
        } else {
            source = new PlanarYUVLuminanceSource(data, width, height,
                    rect.left, rect.top, rect.width(), rect.height(), false);
        }
            return source;
    }

在CameraConfigurationUtils.java文件中,需要在findBestPreviewSizeValue方法中将screenAspectRatio分情况设置,因为screenAspectRatio的要求总是大值比小值。

double screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;

修改为

double screenAspectRatio;
if (screenResolution.x < screenResolution.y) { // 竖屏           
    screenAspectRatio = (double) screenResolution.y / (double) screenResolution.x;
} else {
    screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;
}

在CameraConfigurationUtils.java文件中,删除如下代码,原因是对于镜头分辨率高,而屏幕分辨率低的手机,这段代码直接导致扫码插件采用较低的分辨率去生成用于解析的位图,所以直接去掉。然后你就会发现低分辨率的手机,对二维码、条码的识别率显著提高。

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;}

全部修改后重启项目的效果如下所示:


修改结果.jpg

相关文章

网友评论

      本文标题:Zxing竖屏界面

      本文链接:https://www.haomeiwen.com/subject/eudcxctx.html