赞
踩
地址:https://github.com/journeyapps/zxing-android-embedded/releases/tag/v4.3.0
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/sm" android:text="扫描" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/result" android:text="无数据" /> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/dbv_custom"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:zxing_preview_scaling_strategy="fitXY" />
</LinearLayout>
package cn.yang.codescan; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import android.widget.Toast; import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; public class MainActivity extends AppCompatActivity { private TextView results; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.sm).setOnClickListener(view -> initScan()); results=(TextView) findViewById(R.id.result); } public void initScan() { results.setText(""); IntentIntegrator integrator = new IntentIntegrator(this); // 设置要扫描的条码类型,ONE_D_CODE_TYPES:一维码,QR_CODE_TYPES-二维码 //不设置则表示都扫描 //integrator.setDesiredBarcodeFormats(ONE_D_CODE_TYPES); integrator.setDesiredBarcodeFormats(); integrator.setCaptureActivity(CaptureActivity.class); //设置打开摄像头的Activity integrator.setPrompt("请对准二维码"); //底部的提示文字,设为""可以置空 integrator.setCameraId(0); //前置或者后置摄像头 integrator.setBeepEnabled(false); //扫描成功的「哔哔」声,默认开启 integrator.setBarcodeImageEnabled(true); integrator.initiateScan(); } //获取返回结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (requestCode == IntentIntegrator.REQUEST_CODE) { IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); if (scanResult != null && scanResult.getContents() != null) { String result = scanResult.getContents(); //Log.i("扫码返回: ", result); results.setText(result); } else { Toast.makeText(this,"无数据",Toast.LENGTH_LONG).show(); } } } }
package cn.yang.codescan; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.KeyEvent; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import com.journeyapps.barcodescanner.CaptureManager; import com.journeyapps.barcodescanner.DecoratedBarcodeView; public class CaptureActivity extends AppCompatActivity { private CaptureManager capture; private DecoratedBarcodeView barcodeScannerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_capture); barcodeScannerView = (DecoratedBarcodeView) findViewById(R.id.dbv_custom); capture = new CaptureManager(this, barcodeScannerView); capture.initializeFromIntent(getIntent(), savedInstanceState); capture.decode(); } @Override protected void onResume() { super.onResume(); capture.onResume(); } @Override protected void onPause() { super.onPause(); capture.onPause(); } @Override protected void onDestroy() { super.onDestroy(); capture.onDestroy(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); capture.onSaveInstanceState(outState); } @SuppressLint("MissingSuperCall") @Override public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { capture.onRequestPermissionsResult(requestCode, permissions, grantResults); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。