欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android扫描和生成二维码

程序员文章站 2022-05-27 10:05:20
本文实例为大家分享了android扫描和生成二维码的具体代码,供大家参考,具体内容如下 mainactivity.java public class maina...

本文实例为大家分享了android扫描和生成二维码的具体代码,供大家参考,具体内容如下

mainactivity.java

public class mainactivity extends appcompatactivity {

 private imageview mimageview;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  button button =findviewbyid(r.id.btn);
  mimageview =findviewbyid(r.id.img);
  button.setonclicklistener(new view.onclicklistener() { //点击按钮扫描二维码
   @override
   public void onclick(view view) {
    intent intent = new intent(mainactivity.this,captureactivity.class);


    startactivityforresult(intent,2);
   }
  });

 }

 @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);
    if (textutils.isempty(content)){

     toast.maketext(mainactivity.this, "您的输入为空!", toast.length_short).show();
     return;

    }

    bitmap logo = bitmapfactory.decoderesource(getresources(), r.mipmap.ic_launcher);
    try {
     bitmap bitmap = codecreator.createqrcode(content, 400, 400, logo);
     mimageview.setimagebitmap(bitmap);
    } catch (writerexception e) {
     e.printstacktrace();
    }

    toast.maketext(mainactivity.this,"扫描"+content,toast.length_short).show();


   }
  }
 }
}

activity.main.xml

<android.support.constraint.constraintlayout 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=".mainactivity"
 android:orientation="vertical">

 <button
  android:id="@+id/btn"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="扫一扫"/>

 <imageview
  android:id="@+id/img"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />

</android.support.constraint.constraintlayout>

需要配置的权限

<uses-permission android:name="android.permission.internet"/>
 <uses-permission android:name="android.permission.camera"></uses-permission>
 <uses-permission android:name="android.permission.vibrate" />
 <uses-permission android:name="android.permission.write_external_storage"></uses-permission>
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />
 <uses-permission android:name="android.permission.flashlight" />

build.gradle

minsdkversion 16  //配置16
implementation'com.github.yuzhiqiang1993:zxing:2.2.1' //依赖

外部build.gradle

allprojects {
 repositories {
  google()
  jcenter()
  maven { url 'https://jitpack.io' } //加这行代码
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。