Android 实现扫描和生成二维码

需求:

  就是需要把数据存放到二维码中,然后通过扫描二维码拿到数据,并展示到页面上。

功能展示:

  在这里插入图片描述  在这里插入图片描述

demo下载:

https://download.csdn.net/download/ljp345775/12453785

代码实现:
1.引用第三方
//AndroidX 版本
implementation 'com.king.zxing:zxing-lite:1.1.7-androidx'
2.在AndroidManifest.xml 声明CaptureActivity
<activity
	android:name="com.king.zxing.CaptureActivity"
	android:screenOrientation="fullSensor"
	android:theme="@style/CaptureTheme" />
3.主要代码
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.king.zxing.CaptureActivity;
import com.king.zxing.util.CodeUtils;

import org.json.JSONObject;

import static com.king.zxing.CaptureFragment.KEY_RESULT;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    protected EditText edtResutlt;
    protected Button btnScan;
    protected Button btnCreate;
    protected EditText edtData;
    protected ImageView ivQr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        PermissionUtils.applyPermission(this);
        initView();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_scan) {
            //跳转到扫描界面
            Intent intent = new Intent(this, CaptureActivity.class);
            startActivityForResult(intent, 1);
        } else if (view.getId() == R.id.btn_create) {
            String data = edtData.getText().toString().trim();
            if (TextUtils.isEmpty(data)) {
                Toast.makeText(this, "请输入文字", Toast.LENGTH_SHORT).show();
            } else {
                //生成二维码
                Bitmap qrCode = CodeUtils.createQRCode(data, 600, null);
                ivQr.setImageBitmap(qrCode);
            }
        }
    }

    /**
     * 在该方法中拿到扫描的数据
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                String result = data.getStringExtra(KEY_RESULT);
                Log.e("aaa", "resu-->" + result);
                //展示到页面上
                edtResutlt.setText(result);
            }
        }
    }

    private void initView() {
        edtResutlt = (EditText) findViewById(R.id.edt_resutlt);
        btnScan = (Button) findViewById(R.id.btn_scan);
        btnScan.setOnClickListener(MainActivity.this);
        btnCreate = (Button) findViewById(R.id.btn_create);
        btnCreate.setOnClickListener(MainActivity.this);
        edtData = (EditText) findViewById(R.id.edt_data);
        ivQr = (ImageView) findViewById(R.id.iv_qr);
    }
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页