Android 二维码ZXing使用详解

二维码 (QRCode):最早是日本的一家公司,所定义的一个编码标准,全称 Quick Response Code。它通过在一个矩形区域内使用黑白两种像素来进行编码,它具有高纠错性、高可用性、高识别性。

现在二维码在世界上的使用非常常见,很多app都把它作为信息传递的方式。而且使用起来也非常的方便。也正是因为手机的普及,二维码才可以去广泛传播



二维码的实现和解析是非常复杂的,但是现在有很多的第三方的库已经将这样一个解析的过程封装起来了,而开发者只需要调用其封装好的API即可完成二维码的生成和解析。

这样的二维码的第三方有很多,做的比较出色的那就是ZXing。

ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。ZXing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。

ZXing的项目地址:https://github.com/zxing/zxing

首先,打开GitHub项目地址,下载项目



这个项目非常大,有120多兆,然而这并没有什么卵用,因为ZXing的功能是非常强大的,不仅仅是对扫描二维码,还可以扫描很多其他的编码,同时也不光在Android上可以使用,在其他比如JavaSE,JavaScript等很多平台上都可以使用,所以这样一个很庞大的源代码,我们很难去对他进行使用,所以必须对这个源代码进行精简。也就是把没有的给干掉。

当然,网上也有很多人,把仅仅在Android 端的编码,解码,扫码部分的工能代码抽取了出来,来作为Android 端的一个库来使用。不过需要注意的是,大部分都基于ZXing旧的版本精简的,很多功能已经过时了。


这里我已经为大家精简好了:http://download.csdn.net/detail/a704225995/9693642


打开AndroidStudio,创建新的项目,并导入库文件,开始开发。

1.创建布局文件


<?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:orientation="vertical"
    tools:context="com.example.smart.newapp.MainActivity">

    <Button
        android:id="@+id/main_btn_scan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="扫一扫!"
        android:onClick="scan"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="扫描结果:"
        />
    <TextView
        android:id="@+id/main_tv_scan_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        />

    <EditText
        android:id="@+id/main_et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:hint="输入生成二维码的文字"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击生产二维码"
        android:onClick="makeQRCode"
        />
    <CheckBox
        android:id="@+id/main_cb_is_add_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="是否添加Logo"
        />

    <ImageView
        android:id="@+id/main_iv_qrcode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@mipmap/ic_launcher"
        />
</LinearLayout>
2 .逻辑代码。

public class MainActivity extends AppCompatActivity {

    /**显示扫描结果的TextView*/
    private TextView mTvScanResult;
    /**输入文字用来生成二维码*/
    private EditText mEtInput;
    /**显示生成的二维码*/
    private ImageView mIvShowQRCode;
    /**是否添加logo的checkBox*/
    private CheckBox mCbIsAddLogo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTvScanResult = (TextView) findViewById(R.id.main_tv_scan_result);
        mEtInput = (EditText) findViewById(R.id.main_et_input);
        mIvShowQRCode = (ImageView) findViewById(R.id.main_iv_qrcode);
        mCbIsAddLogo = (CheckBox) findViewById(R.id.main_cb_is_add_logo);
    }
    /**
     * 扫描按钮的点击方法
     * @param view
     */
    public void scan(View view){
        /**
         * CaptureActivity是类库中的一个Activity可以在这个界面完成扫描并解析的操作
         * */
        startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class),0);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            //创建Bundel对象获取从二维码中扫描解析出的数据
            Bundle bundle = data.getExtras();
            String result = bundle.getString("result");
            //将数据显示在TextView上
            mTvScanResult.setText(result);
        }
    }
    /**
     * 生成二维码按钮点击事件
     * @param view
     */
    public void makeQRCode(View view){
        String input = mEtInput.getText().toString().trim();
        if(input.equals("")){
            Toast.makeText(this,"输入为空,无法生成二维码",Toast.LENGTH_SHORT).show();
            return;
        }
        //创建Logo的Bitmap图片
        Bitmap logo = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
        //调用类库中的createQRCode生成二维码
        Bitmap bitmap = EncodingUtils.createQRCode(input,500,500,mCbIsAddLogo.isChecked()?logo:null);
        //将二维码显示的ImageView上
        mIvShowQRCode.setImageBitmap(bitmap);
    }
}
3.不需要添加权限,因为类库中已经帮我们做了。点击运行项目






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值