利用 ZXing Android Embedded 实现二维码处理

来源:互联网 发布:鼠标指针软件下载 编辑:程序博客网 时间:2024/06/10 01:34

ZXing Android Embedded 是一个非常好用的开源库,对于 ZXing 在 Android 上的应用方法进行了精简。这里简单地用一个小 demo 来练习对此库的使用。

引用开源库

Android Studio 引用方法相当简单,直接在 build.gradle 文件里加入如下代码:

repositories {    jcenter()}dependencies {    compile 'com.journeyapps:zxing-android-embedded:3.4.0'    compile 'com.android.support:appcompat-v7:23.1.0'   // Version 23+ is required}android {    buildToolsVersion '23.0.2' // Older versions may give compile errors}

扫描二维码

新建一个项目,在布局文件里设置简单的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.shmj.mouzhai.zxingdemo.MainActivity">    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="startScan"        android:text="@string/start_scan" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/result"        android:textSize="16sp" />    <TextView        android:id="@+id/tv_result"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="24sp" /></LinearLayout>

接着在 MainActivity 里,在 startScan() 方法里调用:

public void startScan(View view) {        IntentIntegrator integrator = new IntentIntegrator(this);        integrator.setOrientationLocked(false)//设置扫码的方向                .setPrompt("将条码放置于框内")//设置下方提示文字                .setCameraId(0)//前置或后置摄像头                .setBeepEnabled(false)//扫码提示音,默认开启                .initiateScan();         }

但是默认的样式是横屏的,如果想要切换为竖屏的话,需要进一步自定义样式。这里只是简单地以切换竖屏为例,当然可以根据源码自由定义想要的效果。
新建 ScanActivity 继承自 CaptureActivity:

import android.os.Bundle;import com.journeyapps.barcodescanner.CaptureActivity;public class ScanActivity extends CaptureActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);    }}

在 AndroidManifest.xml 中添加:

<activity     android:name=".ScanActivity"     android:screenOrientation="fullSensor"     tools:replace="screenOrientation"/>

接着利用之前用到的 IntentIntegrator,在刚才的点击事件里添加:

 .setOrientationLocked(false)//解锁屏幕方向锁定 .setCaptureActivity(ScanActivity.class)//设置扫码界面为自定义样式

接着,我们需要对扫码结果进行处理。在 onActivityResult() 中:

    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);        if (result != null) {            //扫描成功            if (result.getContents() == null) {                //结束扫码                Toast.makeText(this, "取消", Toast.LENGTH_SHORT).show();            } else {                //扫码出结果                tvResult.setText(result.getContents());                Toast.makeText(this, "扫码成功", Toast.LENGTH_SHORT).show();            }        } else {            super.onActivityResult(requestCode, resultCode, data);        }    }

这样,扫码功能就完成啦。
这里写图片描述

生成二维码

在布局文件中继续添加:

    <EditText        android:id="@+id/et_words"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="createCode"        android:text="@string/create_code" />    <ImageView        android:id="@+id/iv_code"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center" />

我们要做的很简单,在文本框输入文字,点击按钮,将输入的内容转化为二维码。
所需要调用的代码也很少,因为都是已经封装好了的。回到 MainActivity 中:

    /**     * 点击生成按钮,根据字符串生成对应的二维码     */    public void createCode(View view) {        Bitmap bitmap;        BitMatrix matrix;        MultiFormatWriter writer = new MultiFormatWriter();        String words = etWords.getText().toString();//输入的内容        try {            matrix = writer.encode(words, BarcodeFormat.QR_CODE, 500, 500);            BarcodeEncoder encoder = new BarcodeEncoder();            bitmap = encoder.createBitmap(matrix);            ivCode.setImageBitmap(bitmap);        } catch (WriterException e) {            e.printStackTrace();        }    }

大功告成!

这里写图片描述

源码地址:https://github.com/Zhai-Wang/Practices/tree/master/zxingdemo/src/main

0 2
原创粉丝点击