二维码扫描+输入框输入内容,点击按钮生成二维码

二维码扫描+输入框输入内容,点击按钮生成二维码_第1张图片


实现步骤:

1. 倒libzxing包

2. 在Settings.gradle中添加  

include ':app','libzxing'

3.添加依赖


//布局文件

xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_scan_result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:text="显示扫描结果字符串" />

        <Button
            android:id="@+id/btn_scan_barcode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10dp"
            android:text="扫描二维码" />

        <EditText
            android:id="@+id/et_qr_string"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:padding="15dp" />

        <ImageView
            android:id="@+id/iv_qr_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:contentDescription="@null"
            android:padding="10dp"
            android:src="@mipmap/ic_launcher" />

        <CheckBox
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:text="是否显示logo(图片)" />

        <Button
            android:id="@+id/btn_add_qrcode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10dp"
            android:text="生成二维码" />

    LinearLayout>
ScrollView>

//核心代码

public class ShowActivity extends AppCompatActivity {

    private TextView resultTextView;
    private EditText qrStrEditText;
    private ImageView qrImgImageView;
    private CheckBox mCheckBox;
    private Button scanBarCodeButton;
    private Button generateQRCodeButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);
        qrStrEditText = (EditText) this.findViewById(R.id.et_qr_string);
        qrImgImageView = (ImageView) this.findViewById(R.id.iv_qr_image);
        mCheckBox = (CheckBox) findViewById(R.id.logo);
        scanBarCodeButton = (Button) this.findViewById(R.id.btn_scan_barcode);
        generateQRCodeButton = (Button) this.findViewById(R.id.btn_add_qrcode);

        //扫描
        //扫描
        scanBarCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(ShowActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    // 申请CAMERA权限
                    ActivityCompat.requestPermissions(ShowActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
                }else{
                    //跳转到zxing的扫描页面
                    // 打开扫描界面扫描条形码或二维码
                    Intent openCameraIntent = new Intent(ShowActivity.this, CaptureActivity.class);
                    startActivityForResult(openCameraIntent, 0);
                }
            }
        });
        //根据输入的字符串生成二维码
        generateQRCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1读et的内容 2生成bitmap 3设置给iv
                String contentString = qrStrEditText.getText().toString();
                if (!TextUtils.isEmpty(contentString)) {
                    // 根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(350*350)
                    Bitmap qrCodeBitmap = EncodingUtils.createQRCode(contentString, 350, 350,mCheckBox.isChecked() ? BitmapFactory.decodeResource(getResources(), R.drawable.ac3
                    ) : null);
                    qrImgImageView.setImageBitmap(qrCodeBitmap);
                } else {
                    Toast.makeText(ShowActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //接受zixing扫描页面的字符串扫描结果
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            resultTextView.setText(scanResult);
        }
    }

    //用户是否同意权限的回调
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        //如果同意直接打开相机
        //跳转到zxing的扫描页面
        // 打开扫描界面扫描条形码或二维码
        Intent openCameraIntent = new Intent(ShowActivity.this, CaptureActivity.class);
        startActivityForResult(openCameraIntent, 0);
    }
}



你可能感兴趣的:(二维码扫描+输入框输入内容,点击按钮生成二维码)