Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系

简介: <div class="markdown_views"><h1 id="android高级控件三-使用google-zxing实现二维码的扫描和生成相关功能体系">Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系</h1><hr><h2 id="摘要">摘要</h2><blockquote> <p>现在的二维码可谓

Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系


摘要

现在的二维码可谓是烂大街了,到处都是二维码,什么都是二维码,扫一扫似乎已经流行到习以为常了,今天我们也来实现以下二维码的相关功能,我们使用到的是Google开源的Zxing项目
Zxing GitHub:https://github.com/zxing/zxing
这个项目很大,乱七八糟的,我们还是直接使用jar包吧,这里感谢一下医生,他为我们封装了一个3.1的jar,我们可以拿来用:https://github.com/xuyisheng/ZXingLib,但是我还是觉得依赖好用,本文也是用依赖做的,这里也提供一个依赖,下载地址:http://download.csdn.net/detail/qq_26787115/9434734,话不多说,我们把这个依赖导入到我们的Eclipse中去,然后再新建一个工程——ZXing

这里写图片描述

好的,我们现在右键项目,选择Properties——Android——add——ZXingLibrary

这里写图片描述

准备工作做好了之后,我们就可以来实现了

二维码扫描

1.权限

这可是非常重要的哟,我们需要两个权限

    <!-- 相机 -->
    <uses-permission android:name="android.permission.CAMERA" />
    <!-- 振动 -->
    <uses-permission android:name="android.permission.VIBRATE" />

2.相关类

这里看了医生的介绍,我也就按部就班的写过来了,是依赖里面的类,你也可以自己去翻看一下依赖,ZXing毕竟太庞大了,所以这个是提取出来的Android二维码的相关核心类

CaptureActivity

ZXing暴露的调用Activity。在handleDecode方法中对扫码成功后的动作作处理。

ViewfinderView

ZXing扫码窗口的绘制,原始的ZXing使用这种方式去绘制,在上面提供的开源库中,作者将扫描框的绘制直接抽取到了XML文件中,这样修改起来更加方便了。

CameraConfigurationManager

修改横竖屏、处理变形效果的核心类。


我们扫描就是要用到这个CaptureActivity类,我们既然依赖了,那么我们也是可以拿来直接用,这里我们可以直接把依赖里面的关于CaptureActivity类的AndroidManifest.xml的注册信息拷贝过来放在我们这个项目中

<activity
            android:configChanges="orientation|keyboardHidden"
            android:name="com.zxing.activity.CaptureActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:windowSoftInputMode="stateAlwaysHidden" >
        </activity>

好的,这些我们都写完了之后,我们就开始我们的逻辑处理了,非常的简单

3.实现扫描

我们在activity_main.xml中声明一个Button

     <Button
        android:id="@+id/btnSan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="扫描二维码" />

我们把他初始化之后在他的点击事件里面加入这样一行代码

startActivity(new Intent(this, CaptureActivity.class));

就可以调用依赖里面的扫描功能了,我们运行一下试试效果,这里就需要使用真机了

这里写图片描述

是不是感觉很6?但是你很快就会发现,你扫描之后振动了一下之后什么都没有,这里我们既要在做一些其他操作了,这里我们在activity_main.xml中新建一个TextView让他显示扫描的东西

 <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

好的,既然我们要接受返回值,那startIntent也是需要更换了,换成我们的startActivityForResult,这里我们也就要重写我们的onActivityResult方法了


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            String result = data.getExtras().getString("result");
            tv_content.setText(result);
        }
    }

现在我们运行一下看看效果,我们随便扫描一张二维码,这里我扫描了一下我自己的微信二维码,看看

这里写图片描述

就是这么吊

生成二维码

二维码生成起来,我们需要三个元素,要生成的内容,生成的按钮,生成内容的存放,所以我们layou_main.xml里面要添加这样的

 <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入要生成的二维码文字" />

    <Button
        android:id="@+id/btn_generate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="生成二维码" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center" >

        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

我们把这几个控件都初始化一下,然后在Button的点击事件中写

case R.id.btnGenerate:
            String str = et_input.getText().toString();
            if (str.equals("")) {
                Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
            } else {
                // 位图
                try {
                    /**
                     * 参数:1.文本 2.二维码的宽高(正方形)
                     */
                    Bitmap bitmap = EncodingHandler.createQRCode(str, 500);
                    // 设置图片
                    img.setImageBitmap(bitmap);
                } catch (WriterException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            break;

我们来运行一下,很神奇的一幕发生了

这里写图片描述

OK,是不是觉得非常的简单?没错,就是这么的简单,你也可以试试


完整代码

layout_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnSan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="扫描二维码" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入要生成的二维码文字" />

    <Button
        android:id="@+id/btnGenerate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="生成二维码" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center" >

        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

</LinearLayout>

MainActivity

package com.lgl.zxing;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.zxing.activity.CaptureActivity;
import com.zxing.encoding.EncodingHandler;

public class MainActivity extends Activity implements OnClickListener {

    private Button btnSan, btnGenerate;
    private TextView tv_content;
    private EditText et_input;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnSan = (Button) findViewById(R.id.btnSan);
        btnSan.setOnClickListener(this);
        tv_content = (TextView) findViewById(R.id.tv_content);
        btnGenerate = (Button) findViewById(R.id.btnGenerate);
        btnGenerate.setOnClickListener(this);
        et_input = (EditText) findViewById(R.id.et_input);
        img = (ImageView) findViewById(R.id.img);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnSan:
            Intent intent = new Intent(this, CaptureActivity.class);
            startActivityForResult(intent, 0);
            break;
        case R.id.btnGenerate:
            String str = et_input.getText().toString();
            if (str.equals("")) {
                Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
            } else {
                // 位图
                try {
                    /**
                     * 参数:1.文本 2.二维码的宽高(正方形)
                     */
                    Bitmap bitmap = EncodingHandler.createQRCode(str, 500);
                    // 设置图片
                    img.setImageBitmap(bitmap);
                } catch (WriterException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            String result = data.getExtras().getString("result");
            tv_content.setText(result);
        }
    }
}

Demo下载地址:http://download.csdn.net/detail/qq_26787115/9434790

目录
相关文章
|
Web App开发
在 HTML 中禁用 Chrome 浏览器的 Google 翻译功能
在 html 标签中添加 translate=“no” 属性,浏览器将不会翻译整个页面。
723 0
|
IDE API 开发工具
Google I/O :Android Jetpack 最新变化(四)Compose
Google I/O :Android Jetpack 最新变化(四)Compose
437 0
|
传感器 编解码 数据处理
Open Google Earth Engine(OEEL)——哨兵1号数据的黑边去除功能附链接和代码
Open Google Earth Engine(OEEL)——哨兵1号数据的黑边去除功能附链接和代码
379 0
|
5月前
|
Java Android开发 开发者
Android使用zxing生成二维码
这是一篇关于如何在Android应用中生成二维码的教程。首先,需要导入zxing库的jar包。布局文件中包含一个按钮、一个图片控件和一个输入框。用户可以在输入框中输入想要转换为二维码的内容,点击按钮后,程序会通过实例化QRCodeWriter类并使用for循环绘制二维码图像,最后将生成的二维码显示在ImageView上。源码展示了具体的实现细节,包括布局定义与Java逻辑代码,便于开发者理解和实践。
119 2
|
Android开发
如何在Android真机上检测是否有Google Map add-on
如何在Android真机上检测是否有Google Map add-on
152 3
|
12月前
|
缓存 Java Shell
Android 系统缓存扫描与清理方法分析
Android 系统缓存从原理探索到实现。
445 15
Android 系统缓存扫描与清理方法分析
|
API Android开发
Google I/O :Android Jetpack 最新变化(三)UI
Google I/O :Android Jetpack 最新变化(三)UI
228 0
|
开发工具 Android开发
上架Google Play报错:For new apps, Android App Bundles must be signed with an RSA key.
上架Google Play报错:For new apps, Android App Bundles must be signed with an RSA key.
313 1
|
存储 数据库 Android开发
🔥Android Jetpack全解析!拥抱Google官方库,让你的开发之旅更加顺畅无阻!🚀
【7月更文挑战第28天】在Android开发中追求高效稳定的路径?Android Jetpack作为Google官方库集合,是你的理想选择。它包含多个独立又协同工作的库,覆盖UI到安全性等多个领域,旨在减少样板代码,提高开发效率与应用质量。Jetpack核心组件如LiveData、ViewModel、Room等简化了数据绑定、状态保存及数据库操作。引入Jetpack只需在`build.gradle`中添加依赖。例如,使用Room进行数据库操作变得异常简单,从定义实体到实现CRUD操作,一切尽在掌握之中。拥抱Jetpack,提升开发效率,构建高质量应用!
438 4
|
监控 数据挖掘 UED
Google Analytics的实时监控功能有哪些优势?
【6月更文挑战第8天】Google Analytics的实时监控功能有哪些优势?
217 4

热门文章

最新文章

推荐镜像

更多