java学习视频 recursion woocommerce jtable sms Seajs Egret Engine vue网页 管理后台模板 kafka消费不到数据 a标签去除下划线 svn更新本地代码 div字体加粗 centos定时任务 pythonsocket编程 mysql更新 python中for循环的用法 python异常 python服务器开发 javascanner java开发学习 java时间类型 java获取本机ip 骁龙435 小程序源码下载 wscript html5下载 JScodeblocks汉化包 iar下载 2700U sendto函数 服务器之家 截取字符串 jsp源代码 jquery手册 losecontrol 0000008e dnf风神加点 EarthView opengl版本过低
当前位置: 首页 > 学习教程  > 编程语言

ZXing实现二维码

2020/11/24 9:49:57 文章标签: 测试文章如有侵权请发送至邮箱809451989@qq.com投诉后文章立即删除

导入依赖 compile ‘cn.yipianfengye.android:zxing-library:2.2’//二维码添加权限 <uses-permission android:name"android.permission.CAMERA"/><uses-permission android:name"android.permission.WRITE_EXTERNAL_STORAGE"/><uses-perm…

  1. 导入依赖
    compile ‘cn.yipianfengye.android:zxing-library:2.2’//二维码
  2. 添加权限
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
  1. 创建一个类续程Application并在清单里调用
    并在application里添加:android:requestLegacyExternalStorage=“true”
public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //添加
        ZXingLibrary.initDisplayOpinion(this);
    }
}
  1. 简单设置UI
 <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="50dp"
        />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="扫描"
    />
  1. 在对应的Activity实现相对应的功能
    代码如下:
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    
    private TextView _tv;
    private Button _btn;

    private void assignViews() {
        _tv = (TextView) findViewById(R.id.tv);
        _btn = (Button) findViewById(R.id.btn);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //添加动态权限
        requestPermissions();
        assignViews();

        _btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到拍摄页面
                Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
                startActivityForResult(intent,101);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 101){
            if(resultCode == RESULT_OK){
                String string = data.getExtras().getString(CodeUtils.RESULT_STRING);
                _tv.setText(string);
            }
        }else{
            _tv.setText("onActivityResult requestCode else");
        }

    }

    //动态权限
    public void requestPermissions(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{
                    Manifest.permission.CAMERA,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.VIBRATE,
                    Manifest.permission.WAKE_LOCK,
            },101);
        }
    }

}

本文链接: http://www.dtmao.cc/news_show_400154.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?