【问题标题】:Adding a back/cancel button in the scan page in ZXing在 ZXing 的扫描页面中添加返回/取消按钮
【发布时间】:2019-04-22 20:40:34
【问题描述】:

我有一个 Android 应用程序,我正在通过 ZXings 库扫描条形码。我已将它集成到我的 Android 应用程序中构建,因此用户不再需要单独安装 ZXing Barcode 应用程序。

我可以完美地扫描和处理结果和取消。但是,为了使应用程序对用户更直观,我想在扫描页面本身添加一个“取消”或“返回”按钮。

我已经通过互联网搜索了found this blog。该博客说 ZXing 更新了他们的库以使自定义更容易,但是,customization seems a bit limited from what the GitHub page shows 并且没有太多关于如何自定义 UI 的示例。

该博客还显示了后退/取消按钮的选项,但是 the GitHub link seems broken 并没有指向任何地方。

有没有人尝试并成功地使用自定义 UI 布局通过 ZXing 进行扫描?

【问题讨论】:

    标签: android zxing


    【解决方案1】:

    主要问题是大多数博客中链接的自定义布局已失效。 I found a working link here. 基本上,我们要做的是提供一个自定义布局文件并通过以下方式使用它:

    IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.setCaptureLayout(R.layout.custom_capture_layout);
    
    //do more things here if you want
    
    integrator.initiateScan();
    

    它将使用您提供的自定义布局。以防上述链接失效,这是我使用的代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
     Copyright (C) 2008 ZXing authors
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
     -->
    <merge xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    
        <SurfaceView android:id="@+id/zxing_preview_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    
        <com.google.zxing.client.android.ViewfinderView
            android:id="@+id/zxing_viewfinder_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    
        <LinearLayout android:id="@+id/zxing_result_view"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/zxing_result_view"
            android:visibility="gone"
            android:baselineAligned="false">
    
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:padding="@dimen/zxing_standard_padding">
    
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="right|center_vertical">
    
                    <ImageView android:id="@+id/zxing_barcode_image_view"
                        android:layout_width="160dip"
                        android:layout_height="wrap_content"
                        android:maxWidth="160dip"
                        android:maxHeight="160dip"
                        android:layout_marginBottom="@dimen/zxing_half_padding"
                        android:adjustViewBounds="true"
                        android:scaleType="centerInside"
                        tools:ignore="ContentDescription"/>
    
                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
    
                        <TextView android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/zxing_msg_default_format"
                            android:textColor="@color/zxing_result_minor_text"
                            android:textStyle="bold"
                            android:paddingRight="@dimen/zxing_half_padding"/>
    
                        <TextView android:id="@+id/zxing_format_text_view"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/zxing_result_minor_text"/>
    
                    </LinearLayout>
    
                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
    
                        <TextView android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/zxing_msg_default_type"
                            android:textColor="@color/zxing_result_minor_text"
                            android:textStyle="bold"
                            android:paddingRight="@dimen/zxing_half_padding"/>
    
                        <TextView android:id="@+id/zxing_type_text_view"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/zxing_result_minor_text"/>
    
                    </LinearLayout>
    
                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
    
                        <TextView android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/zxing_msg_default_time"
                            android:textColor="@color/zxing_result_minor_text"
                            android:textStyle="bold"
                            android:paddingRight="@dimen/zxing_half_padding"/>
    
                        <TextView android:id="@+id/zxing_time_text_view"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/zxing_result_minor_text"/>
    
                    </LinearLayout>
    
                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
    
                        <TextView android:id="@+id/zxing_meta_text_view_label"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/zxing_msg_default_meta"
                            android:textColor="@color/zxing_result_minor_text"
                            android:textStyle="bold"
                            android:paddingRight="@dimen/zxing_half_padding"/>
    
                        <TextView android:id="@+id/zxing_meta_text_view"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/zxing_result_minor_text"/>
    
                    </LinearLayout>
    
                </LinearLayout>
    
                <ScrollView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
    
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
    
                        <TextView android:id="@+id/zxing_contents_text_view"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/zxing_result_text"
                            android:textColorLink="@color/zxing_result_text"
                            android:textSize="22sp"
                            android:paddingLeft="12dip"
                            android:autoLink="web"
                            android:textIsSelectable="true"/>
    
                        <TextView android:id="@+id/zxing_contents_supplement_text_view"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/zxing_result_text"
                            android:textColorLink="@color/zxing_result_text"
                            android:paddingLeft="12dip"
                            android:autoLink="web"
                            android:textIsSelectable="true"/>
    
                    </LinearLayout>
    
                </ScrollView>
    
            </LinearLayout>
    
            <LinearLayout android:id="@+id/zxing_result_button_view"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center">
    
                <Button style="@style/zxing_ResultButton"
                    android:visibility="gone"/>
    
                <Button style="@style/zxing_ResultButton"
                    android:visibility="gone"/>
    
                <Button style="@style/zxing_ResultButton"
                    android:visibility="gone"/>
    
                <Button style="@style/zxing_ResultButton"
                    android:visibility="gone"/>
    
            </LinearLayout>
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_gravity="bottom|center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <TextView android:id="@+id/zxing_status_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|center_horizontal"
                android:background="@color/zxing_transparent"
                android:text="@string/zxing_msg_default_status"
                android:textColor="@color/zxing_status_text"/>
    
            <Button android:id="@id/zxing_back_button"
                android:layout_marginTop="10dp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:height="60dp"
                android:textAlignment="center"
                android:layout_gravity="bottom|center_horizontal"
                android:text="@string/zxing_button_cancel"/>
    
        </LinearLayout>
    
    </merge>
    

    我只是复制粘贴它,根本没有定制。当您开始扫描时,它有一个“取消”按钮。我希望这对遇到死链接的人有所帮助。

    【讨论】:

      【解决方案2】:

      作为class comment said

      要自定义 UI,请直接使用 BarcodeView 和 ViewfinderView。

      BarcodeView 是一个ViewGroup,所以你可以添加孩子回来/取消buttons

      【讨论】:

      • 这是否意味着我可以提供自己的布局并将ViewGroup 用作BarcodeView?我现在正在阅读链接课程,我很难剖析它。如果您能提供解决此问题的“方法”,我将不胜感激。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多