文章目录
前言
二、实现生成二维码的功能
三、实现扫面二维码的功能
总结
前言
提示:这里可以添加本文要记录的大概内容:
我是通过一个第三方库来实现二维码的生成,以及扫描二维码的功能,开源库如下:
复制代码
1implementation 'com.journeyapps:zxing-android-embedded:3.4.0'
一、布局文件如下
布局比较简单,就只有简单的一些按钮,输入框,图片。
效果如下:
activity_main.xml
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" android:layout_margin="20dp" tools:context=".MainActivity"> <EditText android:id="@+id/et_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入文字" android:layout_margin="12dp"/> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center" android:text="点击生成"/> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center" android:text="扫码"/> <ImageView android:layout_marginTop="30dp" android:id="@+id/image" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitCenter" android:adjustViewBounds="true"/> </LinearLayout>
二、实现生成二维码的功能
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String s = editText.getText().toString().trim(); MultiFormatWriter writer = new MultiFormatWriter(); try { BitMatrix matrix = writer.encode(s, BarcodeFormat.QR_CODE,350,350); BarcodeEncoder encoder = new BarcodeEncoder(); Bitmap bitmap = encoder.createBitmap(matrix); imageView.setImageBitmap(bitmap); InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); // manager.hideSoftInputFromWindow(editText.getApplicationWindowToken(),0); } catch (WriterException e) { e.printStackTrace(); } } });
三、实现扫面二维码的功能
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new IntentIntegrator(MainActivity.this) .setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES)// 扫码的类型,可选:一维码,二维码,一/二维码 .setPrompt("请对准二维码")// 设置提示语 .setCameraId(0)// 选择摄像头,可使用前置或者后置 .setBeepEnabled(true)// 是否开启声音,扫完码之后会"哔"的一声 .setBarcodeImageEnabled(true)// 扫完码之后生成二维码的图片 .initiateScan();// 初始化扫码 } }); //返回的结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if(result != null) { if(result.getContents() == null) { Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show(); } } else { super.onActivityResult(requestCode, resultCode, data); } }
生成二维码的效果显示:
二维码的效果显示:
总结
本次实现创建二维码和扫描二维码的功能比较简单,使用的是第三方库的依赖。
最后
以上就是自然时光最近收集整理的关于Android实现生成二维码以及扫描二维码的功能(超级简单!)前言二、实现生成二维码的功能三、实现扫面二维码的功能总结的全部内容,更多相关Android实现生成二维码以及扫描二维码内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复