在Android中扫描不同的二维码并列出它们,可以通过以下步骤实现:
<uses-permission android:name="android.permission.CAMERA" />
implementation 'com.google.zxing:core:3.4.0'
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/scan_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="20sp" />
</RelativeLayout>
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.TextView;
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 MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private static final int CAMERA_PERMISSION_REQUEST_CODE = 100;
private CompoundBarcodeView barcodeView;
private TextView scanResultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
barcodeView = findViewById(R.id.barcode_view);
scanResultTextView = findViewById(R.id.scan_result);
SurfaceView surfaceView = findViewById(R.id.camera_preview);
surfaceView.getHolder().addCallback(this);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
} else {
startCamera();
}
}
@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) {
startCamera();
} else {
// 权限被拒绝
}
}
}
private void startCamera() {
barcodeView.decodeContinuous(new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
if (result.getText() != null) {
scanResultTextView.setText(result.getText());
}
}
@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.getBarcodeView().getCameraSettings().setAutoFocusEnabled(true);
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
}
}
以上代码使用了ZXing库来实现二维码扫描功能。在onCreate方法中,首先检查相机权限是否已授权,如果未授权则请求相机权限。然后通过SurfaceHolder.Callback监听SurfaceView的创建事件,在surfaceCreated方法中启动相机。在startCamera方法中,调用barcodeView的decodeContinuous方法开始连续扫描,并通过BarcodeCallback获取扫描结果。
注意:以上代码仅包含了基本的二维码扫描功能,如需处理不同类型的二维码,可以在BarcodeCallback的barcodeResult方法中根据BarcodeResult的BarcodeFormat属性进行判断和处理。
推荐的腾讯云相关产品:腾讯云移动扫码支付服务(https://cloud.tencent.com/product/qrcodepay)
领取专属 10元无门槛券
手把手带您无忧上云