使用ZXing扫描二维码和生成二维码

二维码在APP中的使用越来越多,于是就是找了相关的资料写了一个二维码扫描和生成二维码的Demo。

本Demo使用了第三方的ZXing库。github的地址:

Zxing

AndroidStudio中的引用:

File---New---import  module导入ZXing的第三方库然后选中项目按F4,

点击加号选择File dependency将ZXing库导入。

接下来就开始写布局文件

activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.zwb.zxingdemo.MainActivity">

    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="扫描二维码" />
    <TextView
        android:layout_marginTop="20dp"
        android:textColor="#e90518"
        android:text="扫描的结果:"
        android:textSize="18sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <View
        android:background="@color/colorPrimary"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="5dp"/>

    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/ed_text"
            android:layout_width="0dp"
            android:layout_weight="9"
            android:layout_marginTop="10dp"
            android:layout_height="wrap_content" />
        <CheckBox
            android:id="@+id/cb_logo"
            android:text="logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <Button
        android:id="@+id/make_ma"
        android:text="生成二维码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/img"
        android:layout_marginTop="10dp"
        android:background="@mipmap/ic_launcher"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.xys.libzxing.zxing.encoding.EncodingUtils;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    private ImageView imageView;

    private EditText input;
    private CheckBox logo;

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

        textView = ((TextView) findViewById(R.id.tv_show));
        imageView = ((ImageView) findViewById(R.id.img));
        logo = ((CheckBox) findViewById(R.id.cb_logo));

        findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 0);
            }
        });

        findViewById(R.id.make_ma).setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                input = ((EditText) findViewById(R.id.ed_text));
                String text = input.getText().toString();
                if (text.equals("")) {
                    Toast.makeText(MainActivity.this, "输入内容不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    /**
                     * 四个参数:传入的文字、宽度、高度、logo
                     */
                    Bitmap bitmap = EncodingUtils.createQRCode(text,
                            500, 500,
                            logo.isChecked() ?
                                    BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher) :
                                    null);
                    imageView.setImageBitmap(bitmap);
                }

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String result = bundle.getString("result");
            textView.setText(result);
        }
    }
}
时间: 2024-08-03 22:01:21

使用ZXing扫描二维码和生成二维码的相关文章

Android实例-实现扫描二维码并生成二维码(XE8+小米5)

相关资料: 第三方资料太大没法写在博文上,请下载CSDN的程序包. 程序包下载: 过几天,刚上传的包,都没有办法显示. 注意事项: 如果只加了Lib,然没有改AndroidManifest.xml,App在呼叫BarCode时会ANR没反应.开始可能没有官方的classes.dex,但如果发现编译出错后,请再检查一下.TMessageManager须加System.Messaging单元. 使用DelphiXE7加入JavaLibrary后,呼叫Zxing相机1.新建一个DelphiXE工程,双

Android zxing 解析二维码,生成二维码极简demo

zxing 官方的代码很多,看起来很费劲,此demo只抽取了有用的部分,实现了相机预览解码,解析本地二维码,生成二维码三个功能. 简化后的结构如下: 废话少说直接上代码: BaseDecodeHandler: package com.song.zxing.decode; import android.graphics.Bitmap; import android.os.Bundle; import com.google.zxing.BarcodeFormat; import com.google

Cordova各个插件使用介绍系列(二)—$cordovaBarcodeScanner扫描二维码与生成二维码

详情链接地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-2-cordovabarcodescanner/ 这是一个用来扫描二维码的cordova插件,在做项目的时候想实现类似于微信的扫一扫功能,就想到了cordova的$cordovaBarcodeScanner插件,用很少量的代码就可以实现了,下面来看一下具体的实现步骤: 一.扫描二维码: 1.首先需要有一个简单的项目,然后在命令行输入添加插件的命令: c

【Java 二维码】生成二维码

ZXingCodeEncodeUtils 生成及解析二维码项目 package utils; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.InputStream; import java.util.EnumMap; import java

后端生成二维码 - C#生成二维码(QR)

最近在github上找到一个相对比较好的C#二维码生成类库.在这里和大家分享一下. github地址:https://github.com/codebude/QRCoder 把解决方案下载下来,编译生成QRCoder.dll,就可以使用了.这个类库定制化的参数比较少,只支持两个参数(二维码文本,纠错级别).其中图片尺寸不好控制,可以生成较大一点的图片.当在前台展示的时候限定图片的尺寸就可以了.(二维码图片放大于缩小,一般不会影响识别度.) 1 using QRCoder; 2 3 namespa

iOS端使用二维码扫描(ZBarSDK)和生成(libqrencode)功能

如今二维码随处可见,无论是实物商品还是各种礼券都少不了二维码的身影.手机中二维码使用也很广泛,如微信等.正好最近收集总结了下二维码的使用方法 下面介绍一下如何在iOS设备上使用二维码 首先在github上下载ZBar SDK地址https://github.com/bmorton/ZBarSDK 然后将如下的相关类库添加进去 AVFoundation.framwork, CoreMedia.framework, CoreVideo.framework, libiconv.dylib 和libzb

使用zxing批量生成二维码立牌

使用zxing批量在做好的立牌背景图的指定位置上,把指定的文本内容(链接地址.文本等)生成二维码并放在该位置, 最后加上立牌编号. 步骤: 1).做好背景图,如下图: 2).生成二维码BufferedImage对象.代码如下: /** * * @Title: toBufferedImage * @Description: 把文本转化成二维码图片对象 * @param text * 二维码内容 * @param width * 二维码高度 * @param height * 二位宽度 * @par

android扫描二维码和生产二维码

极客学院扫描二维码和生成二维码 package com.example.testqr; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickList

java 生成 二维码 和jquery 生成二维码

生成二维码 Java 生成二维码: 思路为拿到jar 包知道里面的方法使用 Step one : 在https://github.com/zxing中下载(点击网页中名为 zxing 的a标签,跳转到源码页面,点击release 查看所有发布的源码,下载zip压缩文件 Step two:  解压文件后打开文件夹,将core包和javase包 中的com包拷贝到一java项目src目录下.右键导出 jar file  得到一个二维码开发的jar包 Step three: 进行二维码制作 impor