效果图
build.gradle(:app)添加依赖
dependencies {implementation 'com.google.zxing:core:3.3.3'implementation 'com.journeyapps:zxing-android-embedded:3.6.0'implementation 'com.google.zxing:javase:3.0.0'
}
Manifests.xml
<uses-permission android:name="android.permission.CAMERA" /><uses-feature android:name="android.hardware.camera" /><uses-feature android:name="android.hardware.camera.autofocus" />
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"><ImageViewandroid:id="@+id/qr_code_image"android:layout_width="200dp"android:layout_height="200dp" /><EditTextandroid:id="@+id/creat_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="请输入想要生成二维码的文字"android:layout_marginTop="20dp"android:textSize="20dp"/><Buttonandroid:id="@+id/creat_button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="生成二维码"android:layout_marginTop="20dp"/><Buttonandroid:id="@+id/scan_button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="扫描二维码"android:layout_marginTop="20dp"/>
</LinearLayout>
MainActivity
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.journeyapps.barcodescanner.BarcodeEncoder;/*** 实现生成二维码和扫描二维码的功能*/
public class MainActivity extends AppCompatActivity {private ImageView qrCodeImageView;private Button scanButton;private Button creatButton;private EditText creatText;@SuppressLint("MissingInflatedId")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);qrCodeImageView = findViewById(R.id.qr_code_image);scanButton = findViewById(R.id.scan_button);creatButton = findViewById(R.id.creat_button);creatText = findViewById(R.id.creat_text);// 生成二维码creatButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {generateQRCode(creatText.getText().toString());}});// 扫描二维码scanButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, ScanActivity.class);startActivity(intent); }});}private void generateQRCode(String text) {MultiFormatWriter multiFormatWriter = new MultiFormatWriter();try {//使用 multiFormatWriter.encode() 函数将文本 text 编码为一个二维码的 BitMatrix 对象BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.QR_CODE, 200, 200);BarcodeEncoder barcodeEncoder = new BarcodeEncoder();//使用 BarcodeEncoder 类的 createBitmap() 函数将 BitMatrix 对象转换为一个 Bitmap 对象Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);//使用 qrCodeImageView.setImageBitmap() 方法将生成的二维码图像显示在 qrCodeImageView 这个 ImageView 上qrCodeImageView.setImageBitmap(bitmap);} catch (WriterException e) {e.printStackTrace();}}
}
ScanActivity
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.zxing.Result;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;/*** 创建一个新的Activity用于扫描二维码,在该Activity中,使用ZXing提供的库来实现二维码的扫描功能*/
public class ScanActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 初始化二维码扫描//创建了一个新的IntentIntegrator对象,并传入当前上下文(this)作为参数。IntentIntegrator是一个用于集成扫描仪应用程序的类IntentIntegrator intentIntegrator = new IntentIntegrator(this);//设置了IntentIntegrator对象期望的条形码格式为QR码。//通过调用setDesiredBarcodeFormats()方法并传入IntentIntegrator.QR_CODE,表示只扫描QR码格式的条形码intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE);//将提示信息设置为"Scan a QR Code"intentIntegrator.setPrompt("Scan a QR Code");//通过调用setCameraId()方法并传入0,表示使用默认的摄像头设备进行扫描intentIntegrator.setCameraId(0);//通过调用setBeepEnabled()方法并传入false,表示在扫描过程中不发出蜂鸣声音效intentIntegrator.setBeepEnabled(false);//通过调用setOrientationLocked()方法并传入true,表示在扫描过程中保持设备竖直方向不变intentIntegrator.setOrientationLocked(true);//通过调用initiateScan()方法,IntentIntegrator对象将启动扫描仪应用程序并等待扫描结果intentIntegrator.initiateScan();}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);if (result != null && result.getContents() != null) {String scannedText = result.getContents();Toast.makeText(this, "扫描结果: " + scannedText, Toast.LENGTH_SHORT).show();// 在这里处理扫描到的二维码内容}finish();}
}