扫描二维码,输入内容生成二维码,二维码扫描框的配置

package liyuanqi.bwie.com.liyuanqi20181106;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.zxing.WriterException;
import com.yzq.zxinglibrary.android.CaptureActivity;

import com.yzq.zxinglibrary.common.Constant;
import com.yzq.zxinglibrary.encode.CodeCreator;

public class MainActivity extends AppCompatActivity {
Button sao;
Button nosao;
ImageView img;
EditText kk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sao = findViewById(R.id.cao);
nosao = findViewById(R.id.nocao);
img = findViewById(R.id.img);
kk = findViewById(R.id.kk);
sao.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
           /* ZxingConfig config=new ZxingConfig();
            config.setDecodeBarCode(true);//是否扫描条形码 默认为true
            config.setReactColor(R.color.colorAccent);//设置扫描框四个角的颜色 默认为白色
            config.setFrameLineColor(R.color.colorAccent);//设置扫描框边框颜色 默认无色
            config.setScanLineColor(R.color.colorAccent);//设置扫描线的颜色 默认白色
            config.setFullScreenScan(false);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
            intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);*/
            startActivityForResult(intent, 200);
        }
    });
    nosao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Bitmap bitmap = null;
            try {
                String s = kk.getText().toString();
                Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.kk);
                bitmap = CodeCreator.createQRCode(s, 400, 400, logo);
            } catch (WriterException e) {
                e.printStackTrace();
            }
            img.setImageBitmap(bitmap);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // 扫描二维码/条码回传
    if (requestCode == 200 && resultCode == RESULT_OK) {
        if (data != null) {
            String content = data.getStringExtra(Constant.CODED_CONTENT);

            String textContent = content;
            if (TextUtils.isEmpty(textContent)) {
                Toast.makeText(MainActivity.this, "您的输入为空!", Toast.LENGTH_SHORT).show();
                return;
            }
            try {
                /*
                 * contentEtString:字符串内容
                 * w:图片的宽
                 * h:图片的高
                 * logo:不需要logo的话直接传null
                 * */

                Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                Bitmap bitmap = CodeCreator.createQRCode(textContent, 400, 400, logo);
                img.setImageBitmap(bitmap);
            } catch (WriterException e) {
                e.printStackTrace();
            }

            Toast.makeText(MainActivity.this,"扫描"+content,Toast.LENGTH_SHORT).show();
        }
    }
}

}

清单文件的权限:

**





**

依赖
app中:implementation ‘com.github.yuzhiqiang1993:zxing:2.2.1’

gradle中:
allprojects {
repositories {
google()
jcenter()
maven { url ‘https://jitpack.io’ }
}
}

导入依赖后如果报SDK错误 ,把SDK的最低版本改高一些

你可能感兴趣的:(扫描二维码,输入内容生成二维码,二维码扫描框的配置)