Android 扫描二维码
时间: 2024-02-04 22:09:57 浏览: 175
在Android中,可以使用ZXing库来实现二维码的扫描功能。下面是一个简单的示例代码:
1. 首先,在你的项目中添加ZXing库的依赖。可以在build.gradle文件中添加以下代码:
```groovy
implementation 'com.google.zxing:core:3.4.0'
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
```
2. 在你的布局文件中添加一个用于显示相机预览的SurfaceView:
```xml
<SurfaceView
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
3. 创建一个扫描二维码的Activity,并在其中初始化相机和二维码扫描器:
```java
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.CompoundBarcodeView;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class QRCodeScannerActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private static final int CAMERA_PERMISSION_REQUEST_CODE = 100;
private CompoundBarcodeView barcodeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr_code_scanner);
barcodeView = findViewById(R.id.barcode_scanner);
// 检查相机权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
} else {
// 初始化相机和二维码扫描器
initScanner();
}
}
private void initScanner() {
barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(Arrays.asList(BarcodeFormat.QR_CODE)));
barcodeView.initializeFromIntent(getIntent());
barcodeView.decodeContinuous(new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
// 扫描结果回调
String qrCode = result.getText();
Log.d("QRCodeScanner", "QR Code: " + qrCode);
Toast.makeText(QRCodeScannerActivity.this, "QR Code: " + qrCode, Toast.LENGTH_SHORT).show();
}
@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
});
}
@Override
protected void onResume() {
super.onResume();
barcodeView.resume();
}
@Override
protected void onPause() {
super.onPause();
barcodeView.pause();
}
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
barcodeView.resume();
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
barcodeView.pause();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 初始化相机和二维码扫描器
initScanner();
} else {
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show();
}
}
}
}
```
这样,你就可以在你的Android应用中实现二维码的扫描功能了。
阅读全文