android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_alignParentEnd=“true”
android:text="@string/firstTest"
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<Button
android:id="@+id/btn"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text="@string/generate_btn"
app:layout_constraintTop_toBottomOf="@+id/editText"
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”/>
<ImageView
android:id="@+id/imageView"
android:layout_width=“256dp”
android:layout_height=“256dp”
android:layout_centerInParent=“true”
app:layout_constraintBottom_toBottomOf=“parent”
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
</androidx.constraintlayout.widget.ConstraintLayout>
https://www.baidu.com
生成二维码
- 在AndroidManifest.xml中注册(Android Studio会自动生成)
<application
…
…
- 要实现二维码的生成,主要是靠zxing提供的接口和方法,在GenerateQRcode文件中新建一个
generateSimpleBitmap
方法用于生成二维码,参数为二维码内容、宽和高,并将参数传入到QRCodeWriter的encode
方法生成BitMatrix
对象,创建一个大小为二维码宽*高的数组,根据BitMatrix
对象,往其中放置黑白色块,最后使用createBitmap
返回一个Bitmap
对象
private Bitmap generateSimpleBitmap(String content, int width, int height) {
// 字符串内容判空
if (TextUtils.isEmpty(content)) {
Toast.makeText(getApplicationContext(),“输入为空!”,Toast.LENGTH_LONG).show();
return null;
}
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, “utf-8”);
try {
BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (encode.get(x, y)) {
pixels[y * width + x] = 0x00000000;
} else {
pixels[y * width + x] = 0xFFFFFFFF;
}
}
}
return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
- 在GenerateQRcode文件中的
onCreate
引用,将结果注入布局的ImageView组件中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.generate_qrcode);
btn = (Button)findViewById(R.id.btn);
editText = (EditText)findViewById(R.id.editText);
imageView = (ImageView)findViewById(R.id.imageView);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String content = editText.getText().toString();
Bitmap qrBitmap = generateSimpleBitmap(content,256, 256);
imageView.setImageBitmap(qrBitmap);
}
});
}
- 实现结果
- 在以上基础上,在GenerateQRcode文件中新建一个
generateQRCodeBitmap
方法,相比generateSimpleBitmap
,参数多了两个颜色,一个为原二维码黑点的颜色,另一个为原来白色的背景颜色
public Bitmap generateQRCodeBitmap(String content, int width, int height,
int color_point, int color_back) {
//字符串内容判空
if (TextUtils.isEmpty(content)) {
Toast.makeText(getApplicationContext(),“输入为空!”,Toast.LENGTH_LONG).show();
return null;
}
Map<EncodeHintType, String> hints = new HashMap<>();
//格式utf-8
hints.put(EncodeHintType.CHARACTER_SET, “utf-8”);
//空白边距设置
hints.put(EncodeHintType.MARGIN, “1”);
try {
//生成BitMatrix对象encode
BitMatrix encode = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
//生成宽*高的像素数组
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (encode.get(x, y)) {
pixels[y * width + x] = color_point;
} else {
pixels[y * width + x] = color_back;
}
}
}
return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
- 将上面简单二维码的引用改为
Bitmap qrBitmap = generateQRCodeBitmap(content, 256, 256, Color.BLUE, Color.GREEN);
- 实现结果
- 在GenerateQRcode文件中新增一个
addLogo
方法,三个参数分别是原二维码、logo图片以及logo在二维码中占据的比例
private static Bitmap addLogo(Bitmap srcBitmap, Bitmap logoBitmap, float logoPercent){
if(srcBitmap == null){
return null;
}
if(logoBitmap == null){
return srcBitmap;
}
if(logoPercent < 0F || logoPercent > 1F){
logoPercent = 0.2F;
}
int srcWidth = srcBitmap.getWidth();
int srcHeight = srcBitmap.getHeight();
int logoWidth = logoBitmap.getWidth();
int logoHeight = logoBitmap.getHeight();
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
float scaleWidth = srcWidth * logoPercent / logoWidth;
float scaleHeight = srcHeight * logoPercent / logoHeight;
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(srcBitmap, 0, 0, null);
canvas.scale(scaleWidth, scaleHeight, srcWidth/2, srcHeight/2);
canvas.drawBitmap(logoBitmap, srcWidth/2 - logoWidth/2, srcHeight/2 - logoHeight/2, null);
return bitmap;
}
- 修改刚才的
generateQRCodeBitmap
方法,添加两个参数,一个是logo图片,一个是所占比例,取值范围[0,1],并在其中引用addLogo
方法
public Bitmap generateQRCodeBitmap(String content, int width, int height,
int color_point, int color_back,
Bitmap logoBitmap, float logoPercent) {
// 字符串内容判空
if (TextUtils.isEmpty(content)) {
Toast.makeText(getApplicationContext(),“输入为空!”,Toast.LENGTH_LONG).show();
return null;
}
Map<EncodeHintType, String> hints = new HashMap<>();
//格式utf-8
hints.put(EncodeHintType.CHARACTER_SET, “utf-8”);
//空白边距设置
hints.put(EncodeHintType.MARGIN, “1”);
try {
//生成BitMatrix对象encode
BitMatrix encode = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
//生成宽*高的像素数组
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (encode.get(x, y)) {
pixels[y * width + x] = color_point;
} else {
pixels[y * width + x] = color_back;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
if(logoBitmap != null){
return addLogo(bitmap, logoBitmap, logoPercent);
}
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
- 从网上下载一张图片,存放在drawable文件中,然后在GenerateQRcode文件中的
onCreate
引用,将结果注入布局的ImageView组件中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.generate_qrcode);
btn = (Button)findViewById(R.id.btn);
editText = (EditText)findViewById(R.id.editText);
imageView = (ImageView)findViewById(R.id.imageView);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String content = editText.getText().toString();
Resources res = getResources();
Bitmap logoBitmap= BitmapFactory.decodeResource(res,R.drawable.cat);
//Bitmap qrBitmap = generateSimpleBitmap(content,256, 256);
Bitmap qrBitmap = generateQRCodeBitmap(content, 256, 256, Color.BLUE, Color.GREEN, logoBitmap, 0.2F);
imageView.setImageBitmap(qrBitmap);
}
});
}
- 实现结果
package com.example.qrcodetest;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.graphics.Bitmap;
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 androidx.appcompat.app.AppCompatActivity;
import java.util.HashMap;
import java.util.Map;
public class GenerateQRcode extends AppCompatActivity {
private Button btn;
private ImageView imageView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.generate_qrcode);
btn = (Button)findViewById(R.id.btn);
editText = (EditText)findViewById(R.id.editText);
imageView = (ImageView)findViewById(R.id.imageView);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String content = editText.getText().toString();
Resources res = getResources();
Bitmap logoBitmap= BitmapFactory.decodeResource(res,R.drawable.cat);
//Bitmap qrBitmap = generateSimpleBitmap(content,256, 256);
Bitmap qrBitmap = generateQRCodeBitmap(content, 256, 256, Color.BLUE, Color.GREEN, logoBitmap, 0.2F);
imageView.setImageBitmap(qrBitmap);
}
});
}
private Bitmap generateSimpleBitmap(String content, int width, int height) {
// 字符串内容判空
if (TextUtils.isEmpty(content)) {
Toast.makeText(getApplicationContext(),“输入为空!”,Toast.LENGTH_LONG).show();
return null;
}
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, “utf-8”);
try {
BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (encode.get(x, y)) {
pixels[y * width + x] = 0x00000000;//黑色
} else {
pixels[y * width + x] = 0xFFFFFFFF;//白色
}
}
}
return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
public Bitmap generateQRCodeBitmap(String content, int width, int height,
int color_point, int color_back,
Bitmap logoBitmap, float logoPercent) {
// 字符串内容判空
if (TextUtils.isEmpty(content)) {
Toast.makeText(getApplicationContext(),“输入为空!”,Toast.LENGTH_LONG).show();
return null;
}
Map<EncodeHintType, String> hints = new HashMap<>();
//格式utf-8
hints.put(EncodeHintType.CHARACTER_SET, “utf-8”);
//空白边距设置
hints.put(EncodeHintType.MARGIN, “1”);
try {
//生成BitMatrix对象encode
BitMatrix encode = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
//生成宽*高的像素数组
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (encode.get(x, y)) {
pixels[y * width + x] = color_point;
} else {
pixels[y * width + x] = color_back;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
if(logoBitmap != null){
return addLogo(bitmap, logoBitmap, logoPercent);
}
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
private static Bitmap addLogo(Bitmap srcBitmap, Bitmap logoBitmap, float logoPercent){
if(srcBitmap == null){
return null;
}
if(logoBitmap == null){
return srcBitmap;
}
if(logoPercent < 0F || logoPercent > 1F){
logoPercent = 0.2F;
}
int srcWidth = srcBitmap.getWidth();
int srcHeight = srcBitmap.getHeight();
int logoWidth = logoBitmap.getWidth();
int logoHeight = logoBitmap.getHeight();
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
float scaleWidth = srcWidth * logoPercent / logoWidth;
float scaleHeight = srcHeight * logoPercent / logoHeight;
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(srcBitmap, 0, 0, null);
canvas.scale(scaleWidth, scaleHeight, srcWidth/2, srcHeight/2);
canvas.drawBitmap(logoBitmap, srcWidth/2 - logoWidth/2, srcHeight/2 - logoHeight/2, null);
return bitmap;
}
}
================================================================
打开真机调试
-
因为在虚拟机中的摄像头显示是一片黑暗,所以这里使用真机调试
-
首先在将手机和电脑用USB线连接,USB连接方式选择传输文件
- 然后在手机中打开开发者选项,启动USB调试。开发者选项一般是隐藏的,可在关于手机处多次点击版本号,即可出现。
- 即可在Android Studio调用
集成工作
- 在build.gradle中添加依赖com.google.zxing:core:3.4.0,此步在前面已添加,因此可不用再次操作
implementation ‘com.google.zxing:core:3.4.0’
- 打开ZXing的开源地址项目https://github.com/zxing/zxing,在该项目下方的readme中看到,ZXing已将扫描仪的一些功能封装整合到一个包中
-
我们可以下载其中的Android调用,但是,ZXing在封装了多个功能,而我们只需要做到一个简单扫码的功能,因此此处选择网上大神们从其中挑出并修改整合好的扫码的一些基本功能
-
来到下载文件处,获取zxing内容,复制进自己的项目
- 获得音频文件,直接将raw放入自己项目中的res文件下(也可自己去别处下载)
- 拷贝attrs.xml、colors.xml以及ids.xml三个文件
- 获得图片
- 拷贝导航栏布局toolbar_scanner.xml以及扫码界面的布局activity_scanner.xml
- 拷贝util文件中的BitmapUtil.java、Constant.java、UriUtil.java
- 此时运行程序,此处因为引用的是原来作者的包,必定出错,等下再进行修改
实现扫码
- 在AndroidManifest.xml文件添加权限
…
- 新建一个名为ScanQRcode的activity,同时生成布局文件scan_qrcode.xml,修改布局文件,主要有一个按钮用于监听、一个文本框显示扫码返回结果
<androidx.constraintlayout.widget.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=".ScanQRcode">
<Button
android:id="@+id/button"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text="@string/scan_button"
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toBottomOf="@+id/include" />
<TextView
android:id="@+id/textView"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text="@string/scan_result"
android:gravity=“center”
android:layout_gravity=“center_horizontal”
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
对应string
扫描二维码
扫码结果
- 回到AndroidManifest.xml对新建内容注册(有的会自动注册,这里主要是手动注册CaptureActivity),因为在该项目中自定义了导航栏,因此此处还有修改主题为
NoActionBar
<application
…
<activity
android:name=“com.google.zxing.activity.CaptureActivity”
android:theme="@style/Theme.AppCompat.NoActionBar">
</ac
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
tivity>
…
- 再回到主界面MainActivity文件,给第二个按钮添加监听操作
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),“扫描二维码”,Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, ScanQRcode.class);
startActivity(intent);
}
});
- 启动扫码程序,在ScanQRcode文件中新建一个
startScanQRcode
来获得权限并开始扫码,在onCreate
中监听调用
private void startScanQRcode() {
// 申请相机权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// 申请权限
ActivityCompat.requestPermissions(ScanQRcode.this, new String[]{Manifest.permission.CAMERA}, Constant.REQ_PERM_CAMERA);
return;
}
// 申请文件读写权限(部分朋友遇到相册选图需要读写权限的情况,这里一并写一下)
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// 申请权限
ActivityCompat.requestPermissions(ScanQRcode.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Constant.REQ_PERM_EXTERNAL_STORAGE);
return;
}
// 二维码扫码
Intent intent = new Intent(ScanQRcode.this, CaptureActivity.class);
startActivityForResult(intent, Constant.REQ_QR_CODE);
}
- 重写
onRequestPermissionsResult
方法,修改获得权限
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case Constant.REQ_PERM_CAMERA:
// 摄像头权限申请
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 获得授权
startScanQRcode();
} else {
// 被禁止授权
Toast.makeText(ScanQRcode.this, “请至权限中心打开本应用的相机访问权限”, Toast.LENGTH_LONG).show();
}
break;
case Constant.REQ_PERM_EXTERNAL_STORAGE:
// 文件读写权限申请
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 获得授权
startScanQRcode();
} else {
// 被禁止授权
Toast.makeText(ScanQRcode.this, “请至权限中心打开本应用的文件读写权限”, Toast.LENGTH_LONG).show();
}
break;
}
}
- 修改错误导入包,下图为原先错误的导入,此时将错误的导入删除即可(Android Studio会自动添加正确的包,若是未添加,也可手动修改),对出现类似的错误导入进行修改
修改前
修改后
- 进入扫码界面(虚拟机)
返回二维码内容
在ScanQRcode文件新建一个onActivityResult
,重写该方法,回调扫描结果