【Android】扫描条形码和二维码

【Android】扫描条形码和二维码

 原文链接:https://blog.csdn.net/xu_weijie/article/details/80763848

步骤一:

在gradle集成Zxing。

加入以下代码

compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
compile 'com.google.zxing:core:3.2.0'

步骤二:

生成控件调用

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/button"
    android:layout_alignRight="@+id/button"
    android:layout_below="@+id/button"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:onClick="onScanBarcode"
    android:text="条形码"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.232" />
 
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:onClick="onScanQrcode"
    android:text="二维码"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="@+id/button2"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="@+id/button2"
    app:layout_constraintTop_toBottomOf="@+id/button2"
    app:layout_constraintVertical_bias="0.176" />


步骤三:设置照相机权限:

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


步骤四:加入实例来获取扫描结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null) {
        if(result.getContents() == null) {
            Toast.makeText(this, "扫码取消!", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "扫描成功,条码值: " + result.getContents(), Toast.LENGTH_LONG).show();
        }
    } else {
        // This is important, otherwise the result will not be passed to the fragment
        super.onActivityResult(requestCode, resultCode, data);
    }
}


步骤五:控件调用

public void onScanBarcode(View v){
    IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
    integrator.setPrompt("扫描条形码");
    integrator.setCameraId(0);
    integrator.setBeepEnabled(false);
    integrator.initiateScan();
}
 
public void onScanQrcode(View v){
    IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
    integrator.setPrompt("扫描二维码");
    integrator.setCameraId(0);
    integrator.setBeepEnabled(false);
    integrator.initiateScan();
}


声明:

IntentIntegrator integrator = new IntentIntegrator(this);
// 设置要扫描的条码类型,ONE_D_CODE_TYPES:一维码,QR_CODE_TYPES-二维码
        integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
        integrator.setPrompt("扫描条形码");
        integrator.setCameraId(0);  // 使用默认的相机
        integrator.setBeepEnabled(false); // 扫到码后播放提示音
        integrator.initiateScan();

上面为属性值



原文链接:https://blog.csdn.net/xu_weijie/article/details/80763848

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中实现扫描条形码,可以通过以下步骤进行操作: 1. 添加依赖库:在项目的build.gradle文件中添加zxing库的依赖: ```java dependencies { implementation 'com.google.zxing:core:3.4.0' implementation 'com.journeyapps:zxing-android-embedded:4.0.0' } ``` 2. 创建扫描界面布局:在布局文件中添加一个SurfaceView用于显示摄像头实时预览: ```xml <SurfaceView android:id="@+id/camera_preview" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 3. 创建扫描逻辑:在Activity中初始化并启动扫描: ```java private SurfaceView cameraPreview; private CaptureManager captureManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cameraPreview = findViewById(R.id.camera_preview); captureManager = new CaptureManager(this, cameraPreview); captureManager.initializeFromIntent(getIntent(), savedInstanceState); captureManager.decode(); } @Override protected void onResume() { super.onResume(); captureManager.onResume(); } @Override protected void onPause() { super.onPause(); captureManager.onPause(); } @Override protected void onDestroy() { super.onDestroy(); captureManager.onDestroy(); } ``` 4. 解析扫描结果:在Activity中重写onActivityResult方法,获取扫描结果: ```java @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (result != null) { String barcode = result.getContents(); // 处理扫描结果 } } ``` 这样,当用户打开该Activity时,他们将能够扫描条形码并获取到结果。可以根据实际需求对结果进行处理,例如跳转到其他界面展示扫描结果等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值