二维码的生成与识别(小白)

1,需要用到的编程工具 Android studio
2,需要下载第三方辅助文件zxing(链接: https://pan.baidu.com/s/1dF8Omgp 密码: 9a54)
首先我们先布局好我们的layout
这里写图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.shinelon.erweima.MainActivity"
    android:orientation="vertical"
    android:weightSum="1">
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/saomiao"
    android:onClick="saomian"
    android:text="点击扫描二维码"/>
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="二维码的内容是:"
        android:layout_weight="0.26" />
    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入你想生成的二维码"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/shengchen"
        android:text="生成二维码"/>
    <CheckBox
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="是否需要logo"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv"
        android:background="@mipmap/zhuzhu"*
        android:layout_gravity="center"/>
/——————这里的图片可以是自己修改————————————/

</LinearLayout>


我们先把我们的zxing解压然后导入我们的demo中
我们右键APP文件然后moudle一个library一个文件然后可以吧zxing文件导进去 你可以复制粘贴
效果就这样(后面两个mylibray是我自己弄得不用管它….)这里写图片描述
还要将这个library导入到APP文档中
在app.gradle里要求添加这个
dependencies {
compile project(‘:libzxing’)
}
然后我们来写MainActicity


public class MainActivity extends AppCompatActivity {
//这块地方我就不说了
private Button bt1,bt2;
    private EditText et;
    private TextView tv;
    private ImageView iv;
    private CheckBox cb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //注册id方法
        initView();
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //如果点击了扫描二维码的话他会跳转到另一个界面  CaptureActivity()这个界面是辅助文件zxing里的一个类
               startActivityForResult(new Intent(MainActivity.this,CaptureActivity.class),0);
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //获取edittext内的文本
               String input=et.getText().toString();
               //如果文本为空则toast输入不能为空,否则实行EncodingUtils上的方法
                if(input.equals("")){
                    Toast.makeText(MainActivity.this,"输入不能为空",Toast.LENGTH_LONG).show();
                }else {
                    Bitmap bitmap= EncodingUtils.createQRCode(input,500,500,cb.isChecked()? BitmapFactory.decodeResource(getResources(),R.mipmap.zhuzhu):null);
                    iv.setImageBitmap(bitmap);
                }
            }
        });
    }

    private void initView() {
    bt1= (Button) findViewById(R.id.saomiao);
        bt2= (Button) findViewById(R.id.shengchen);
        et= (EditText) findViewById(R.id.et);
        tv= (TextView) findViewById(R.id.tv);
        iv= (ImageView) findViewById(R.id.iv);
        cb= (CheckBox) findViewById(R.id.cb);
    }

    @Override
    //这个是重写CaptureActivity()里面的方法
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //如果扫描的二维码是有值的则返回扫描到的内容 然后text出来
        if (resultCode==RESULT_OK){
            Bundle bundle=data.getExtras();
            String result=bundle.getString("result");
            tv.setText(result);
        }
    }
}

最后看一下运行结果
这里写图片描述

本文学习来自慕课网 希望我自己能理解的深透 也希望能帮助大家
这是第一次写 写的不好的请谅解 我是个小小的大二专科生 有志同道合的人来一起进步 qq2218430288

猜你喜欢

转载自blog.csdn.net/qq_39388035/article/details/78700357