当前位置:   article > 正文

android 扫描二维码_fdroid

fdroid

1.在你的build.gradle文件中添加Mobile Vision库的依赖:

  1. dependencies {
  2. implementation 'com.google.android.gms:play-services-vision:20.1.0'
  3. }

2.创建一个新的Activity来处理扫描过程。

  1. import android.Manifest;
  2. import android.content.pm.PackageManager;
  3. import android.os.Bundle;
  4. import androidx.core.app.ActivityCompat;
  5. import androidx.core.content.ContextCompat;
  6. import androidx.appcompat.app.AppCompatActivity;
  7. import com.google.android.gms.vision.barcode.BarcodeReader;
  8. import com.google.android.gms.vision.CameraSource;
  9. import com.google.android.gms.vision.barcode.Barcode;
  10. import android.util.SparseArray;
  11. import com.google.android.gms.vision.Detector;
  12. import android.view.SurfaceHolder;
  13. import android.view.SurfaceView;
  14. public class QRCodeScannerActivity extends AppCompatActivity {
  15. private SurfaceView cameraPreview;
  16. private CameraSource cameraSource;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_qrcode_scanner);
  21. cameraPreview = findViewById(R.id.camera_preview);
  22. BarcodeReader barcodeReader = new BarcodeReader.Builder(this).build();
  23. cameraSource = new CameraSource.Builder(this, barcodeReader)
  24. .setFacing(CameraSource.CAMERA_FACING_BACK)
  25. .setRequestedPreviewSize(1600, 1024)
  26. .setAutoFocusEnabled(true)
  27. .build();
  28. cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
  29. @Override
  30. public void surfaceCreated(SurfaceHolder holder) {
  31. if (ContextCompat.checkSelfPermission(QRCodeScannerActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
  32. try {
  33. cameraSource.start(cameraPreview.getHolder());
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39. @Override
  40. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  41. }
  42. @Override
  43. public void surfaceDestroyed(SurfaceHolder holder) {
  44. cameraSource.stop();
  45. }
  46. });
  47. barcodeReader.setProcessor(new Detector.Processor<Barcode>() {
  48. @Override
  49. public void release() {
  50. }
  51. @Override
  52. public void receiveDetections(Detector.Detections<Barcode> detections) {
  53. final SparseArray<Barcode> qrCodes = detections.getDetectedItems();
  54. if (qrCodes.size() != 0) {
  55. Barcode barcode = qrCodes.valueAt(0);
  56. String scannedQRCode = barcode.displayValue;
  57. // Handle the scanned code as needed
  58. }
  59. }
  60. });
  61. }
  62. @Override
  63. protected void onDestroy() {
  64. super.onDestroy();
  65. cameraSource.release();
  66. }
  67. }

3.确保你的AndroidManifest.xml文件中添加了相应的权限

<uses-permission android:name="android.permission.CAMERA" />

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/508622
推荐阅读
相关标签
  

闽ICP备14008679号