【整理自慕课网】Android动感系列-扫一扫 ZXing

欢迎学习Android 扫一扫功能

主要实现的功能

1.二维码解析
2.二维码生成

二维码的库源码(慕课网链接)

导入方法:1.导入module:file>new>import module>libzxing
2.源码库添加为依赖库:
Project Structure(ctrl+alt+shift+s)>Modules>app>dependencies>点击绿色加号,选择第三个选项

Demo下载 密码:vrf6(百度云链接)

源码部分

主界面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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="20dp"
        android:gravity="center"
        android:text="扫一扫二维码"

        android:textSize="24sp"/>

    <Button
        android:textSize="18sp"
        android:id="@+id/btn_sao"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@color/colorPrimary"
        android:text="扫一扫"
        android:textColor="#FFFFFF"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:text="扫描结果:"
        android:textSize="18sp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:textSize="16sp"
        tools:text="预览"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1.5px"
        android:layout_margin="20dp"
        android:background="@color/colorPrimary"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="生成二维码"
        android:textSize="24sp"/>

    <EditText
        android:id="@+id/ed_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:hint="请输入需要装换的文字"/>
    <CheckBox
        android:id="@+id/cb_logo"
        android:text="logo"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_marginTop="10dp"
        android:textSize="18sp"
        android:background="@color/colorPrimary"
        android:textColor="#FFFFFF"
        android:id="@+id/btn_scan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="生成"/>
    <ImageView
        android:id="@+id/iv_result"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
</LinearLayout>

xml预览

主页activity代码

  • Buttom的点击事件
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_sao:
                startActivityForResult(new Intent(this, CaptureActivity.class), 0);
                break;
            case R.id.btn_scan:
                String input = mEdInput.getText().toString().trim();
                if (input.equals("")) {
                    Toast.makeText(HomeActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show();
                } else {
                //mCheckBox是否在二维码中心添加logo            
                    Bitmap bitmap = EncodingUtils.createQRCode(input, 500, 500, mCheckBox.isChecked() ? BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher) : null);
                    mIvResult.setImageBitmap(bitmap);
                }

                break;
        }
    }
  • 回调事件
 @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");
            mTvResult.setText(result);
        }
    }

遇到问题

弹出提示框 camera error

解决方法:1.手机安全中心->授权管理->应用权限管理->程序名称->多媒体相关相机、拍照、录像和闪光灯选中
2.设置->其他应用程序->程序名称->权限管理->多媒体相关相机、拍照、录像和闪光灯选中

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值