时间:2020-10-13 13:21:05 | 栏目:Android代码 | 点击:次
这个二维码框架不仅使用的代码少,而且可以生成带图片的二维码,而且二维码扫描界面也是先做好的,也比较好看。我们所需要做的就是在扫描二维码后得到的数据对其进行操作。
使用方法
添加依赖
compile 'com.singleshu:ZxingPlus:1.1.4'
代码
public class MainActivity extends AppCompatActivity { TextView test; ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); test = (TextView) findViewById(R.id.test); imageView = (ImageView) findViewById(R.id.image1); //6.0加上动态权限申请,需要在外面Acitvity做申请 PermissionUtils.getInstance().requestPermission(this); } public void click(View view){ switch (view.getId()){ //生成二维码 case R.id.score: imageView.setImageBitmap(QRCodeUtil.CreateTwoDCode(test.getText().toString())); break; //扫描二维码 case R.id.scan: startActivity(new Intent(this,QRCodeScanAtivity.class)); break; //生成二维码带图标,这和图片大小相关,默认取3/4显示 case R.id.scorewithlogo: imageView.setImageBitmap(QRCodeUtil.createWithLogo(test.getText().toString(), BitmapFactory.decodeResource(getResources(), R.drawable.timg))); break; //生成带图标二维码,自设置大小 取 multipart/divisor显示 case R.id.custom_scorewithlogo: imageView.setImageBitmap(QRCodeUtil.createWithLogo(test.getText().toString(), BitmapFactory.decodeResource(getResources(), R.drawable.timg),1,4)); break; } } }
public class QRCodeScanAtivity extends DefaultQRScanActivity { //对控件的自定义,可以修改隐藏默认控件 @Override protected void initCustomViewAndEvents() { } //扫描图片结果解析回调 recode二维码字符串 @Override protected void onAlbumResult(int requestCode, int resultCode, String recode) { Bundle bundle = new Bundle(); bundle.putString("result",recode); startActivity(new Intent(QRCodeScanAtivity.this,ResultActivity.class).putExtras(bundle)); finish(); } //二维码图片解析回调 rawResult 二维码字符串 @Override protected void handleDecodeResult(String rawResult, Bundle bundle) { bundle.putString("result",rawResult); startActivity(new Intent(QRCodeScanAtivity.this,ResultActivity.class).putExtras(bundle)); finish(); } }
public class ResultActivity extends AppCompatActivity { TextView test2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_result); test2 = (TextView) findViewById(R.id.test2); if (getIntent() != null){ Bundle bundle = getIntent().getExtras(); String result = bundle.getString("result"); test2.setText(result); } } }
代码真的不多,基本我们就只用去掉用和接受数据。