ag旗舰厅官网_ag旗舰厅下载客户端

热门关键词: ag旗舰厅官网,ag旗舰厅下载客户端

网络编程

当前位置:ag旗舰厅官网 > 网络编程 > ZXing二维码拉伸,ZXing个性化与近距离识别优化

ZXing二维码拉伸,ZXing个性化与近距离识别优化

来源:http://www.pedaLyourcycLe.com 作者:ag旗舰厅官网 时间:2019-09-30 16:18

ZXing改为竖屏后会有二维码拉伸难题,网络海高校部分格局修改后会导致中距离识别慢的主题素材,还某个中距离识别的修改章程会造成拉伸。

简介

上一篇Android完成二维码扫描功效(一)-ZXing插件接入介绍了ZXing框架连接格局,已经足以开首集成扫码功用到项目中。

本篇大家对扫码分界面实行优化,并对ZXing中远距离不可能分辨的标题做出优化。

踩了多个坑后,找到上面包车型大巴更换章程,对自家有效:原博地址

脾气化定制

各样APP都有谈得来的表现形式,达成本性化扫码界面定制,首要有多少个地点:

  1. activity_scanner.xml分界面文件
  2. com.google.zxing.view.ViewfinderView扫码控件

下边分别来表明。

ZXing版本:3.2.1(非当前版本未必有效)首要修改CameraConfigurationManager中的getCameraResolution方法:

分界面文件

activity_scanner.xml源码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar_scanner" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <SurfaceView
            android:id="@+id/scanner_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center" />

        <com.google.zxing.view.ViewfinderView
            android:id="@+id/viewfinder_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:corner_color="@color/corner_color"
            app:frame_color="@color/viewfinder_frame"
            app:label_text="二维码/条形码扫描"
            app:label_text_color="@color/colorAccent"
            app:laser_color="@color/laser_color"
            app:mask_color="@color/viewfinder_mask"
            app:result_color="@color/result_view"
            app:result_point_color="@color/result_point_color" />

    </FrameLayout>

</LinearLayout>

分界面剖判:

  • 顶层选取LinearLayout达成自上至下的方向,基本不用修改;
  • <include layout="@layout/toolbar_scanner" />援用toolbar布局文件,稍后给出,这里能够投入菜单种类、重临键等;
  • FrameLayout布局将相机可围观窗口叠合在一同;
  • SurfaceView装载相机内容,即相机拍片到的画面;
  • com.google.zxing.view.ViewfinderView扫码自定义View,重要由遮罩层、八个角的边框、扫描线等整合。

toolbar_scanner.xml,比较轻松

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:contentInsetStart="0dip">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">

        <ImageButton
            android:id="@+id/btn_back"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:background="?attr/selectableItemBackground"
            android:padding="10dip"
            android:scaleType="centerCrop"
            android:src="@drawable/btn_back" />

        <TextView
            android:id="@+id/txt_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="扫描二维码"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

    </RelativeLayout>
</android.support.v7.widget.Toolbar>

可依靠要求调解toolbar内容,这里大家就非常的少说。

 private static Point getCameraResolution(Camera.Parameters parameters, Point screenResolution) { List<Camera.Size>list= parameters.getSupportedPreviewSizes(); Collections.sort(list, new Comparator<Camera.Size>() { @Override public int compare(Camera.Size lhs, Camera.Size rhs) { // TODO Auto-generated method stub if (lhs.width-rhs.width==0) { return lhs.height-rhs.height; } return lhs.width-rhs.width; } }); for (Camera.Size size : list) { System.out.println(size.width+" "+size.height); if (Math.abs( size.width*1.0/size.height - 1.0*screenResolution.y/screenResolution.x )<0.1) { return new Point(size.width, size.height); } } return new Point(list.get.width, list.get.height); }

扫码控件定制

今昔使用的ViewfinderView已是因在此以前辈修改过的本子,他们参与了边框和扫描线,基于那几个本子大家再做一些调治。

1、提醒文字的职位

现存的唤起文字是在扫描框下边的
图片 1
我们须要把它放置到扫描框上边,供给将源码中drawTextInfo方法调度一下:

//绘制文本
  private void drawTextInfo(Canvas canvas, Rect frame) {
    ...
    canvas.drawText(labelText, frame.left + frame.width() / 2, frame.top - CORNER_RECT_HEIGHT, paint);
  }

调整为:

//绘制文本
  private void drawTextInfo(Canvas canvas, Rect frame) {
    ...
    canvas.drawText(labelText, frame.left + frame.width() / 2, frame.bottom + CORNER_RECT_HEIGHT * 1.5f, paint);
  }

此间通过更动drawText的y坐标参数调节了文字地方,可以根据必要活动调节。

2、扫码框的地方

如今的扫码框全部地点偏下,缺乏雅观,筹算将地方往上调节一些。
修改com.google.zxing.camera.CameraManager类中的getFramingRect()方法,由

public Rect getFramingRect() {
        ...
        int leftOffset = (screenResolution.x - width) / 2;
        int topOffset = (screenResolution.y - height) / 2;
        ...

        return framingRect;
}

修改为

public Rect getFramingRect() {
        ...
        int leftOffset = (screenResolution.x - width) / 2;
        int topOffset = (screenResolution.y - height) / 3;
        ...

        return framingRect;
}

即topOffset有了缩小,预览后如:
图片 2

3、扫码框大小
由2我们得以看出getFramingRect()方法组织的矩形中有宽、高级参照他事他说加以考察数,通过改换那四个参数,能够做到扫码框大小改动。比如:

public Rect getFramingRect() {
        ...
        int width = screenResolution.x * 8 / 10;
        int height = screenResolution.y * 8 / 10;
        ...

        return framingRect;
}

则比原先的代码中推广了1/10,预览图(可对照第22中学图):
图片 3

扫码识别优化

那边根本说一下中距离扫码识别不了的主题素材,关于扫码算法的校对涉猎非常的少,文尾给咱们一篇小说参谋。

ZXing在遇见二维码撑满扫码框的情况下识别不出结果,等多长期、再聚集都不得以,有心上人说扩张扫码框的深浅能够缓慢解决这么些难题,扩充后远距离扫码结果恐怕长期以来的,很难识别出来。

原因是原算法对录像头搜罗的图像像素进行了裁剪,真正再次来到给识别算法的图像只怕未有扫码框那么完整。

参谋互连网大神的帖子,大家将com.google.zxing.camera.CameraManager类中的buildLuminanceSource方法做了调节,不再回来剪裁后的图像。源码:

public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
        Rect rect = getFramingRectInPreview();
        int previewFormat = configManager.getPreviewFormat();
        String previewFormatString = configManager.getPreviewFormatString();
        switch (previewFormat) {
            // This is the standard Android format which all devices are REQUIRED to support.
            // In theory, it's the only one we should ever care about.
            case PixelFormat.YCbCr_420_SP:
                // This format has never been seen in the wild, but is compatible as we only care
                // about the Y channel, so allow it.
            case PixelFormat.YCbCr_422_SP:
                return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top,
                        rect.width(), rect.height());
            default:
                // The Samsung Moment incorrectly uses this variant instead of the 'sp' version.
                // Fortunately, it too has all the Y data up front, so we can read it.
                if ("yuv420p".equals(previewFormatString)) {
                    return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top,
                            rect.width(), rect.height());
                }
        }
        throw new IllegalArgumentException("Unsupported picture format: " +
                previewFormat + '/' + previewFormatString);
    }

修改为:

public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
        Rect rect = getFramingRectInPreview();
        int previewFormat = configManager.getPreviewFormat();
        String previewFormatString = configManager.getPreviewFormatString();
        switch (previewFormat) {
            // This is the standard Android format which all devices are REQUIRED to support.
            // In theory, it's the only one we should ever care about.
            case PixelFormat.YCbCr_420_SP:
                // This format has never been seen in the wild, but is compatible as we only care
                // about the Y channel, so allow it.
            case PixelFormat.YCbCr_422_SP:
                return new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height);
            default:
                // The Samsung Moment incorrectly uses this variant instead of the 'sp' version.
                // Fortunately, it too has all the Y data up front, so we can read it.
                if ("yuv420p".equals(previewFormatString)) {
                    return new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height);
                }
        }
        throw new IllegalArgumentException("Unsupported picture format: " +
                previewFormat + '/' + previewFormatString);
    }

重大改换的是new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height); 实际上正是将一体化的相机图像内容重回,不做裁剪。

结语

本篇对ZXing扫码工具分界面定制和扫码识别优化做了助教,消除了ZXing中距离无法甄别二维码的主题材料。
世家也可以本着那个思路,做更八日性化定制和优化,完善扫码功用。

参照他事他说加以考察资料

源码下载

csdn桐月上传,下载地址:

本文由ag旗舰厅官网发布于网络编程,转载请注明出处:ZXing二维码拉伸,ZXing个性化与近距离识别优化

关键词: