下面是针对“Android实现扫一扫识别数字功能”的完整攻略。
步骤一:添加ZXing库
-
下载并导入ZXing库。
-
在
build.gradle
文件中添加ZXing依赖
dependencies {
implementation 'com.google.zxing:core:3.3.3'
}
步骤二:添加扫码识别逻辑
- 在
AndroidManifest.xml
中添加相机权限
<uses-permission android:name="android.permission.CAMERA" />
- 创建扫码Activity
public class ScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final String TAG = ScanActivity.class.getSimpleName();
private ZXingScannerView mScannerView;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
}
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera();
}
@Override
public void handleResult(Result result) {
String code = result.getText();
if (code != null && code.matches("[0-9]+")) { // 匹配纯数字
// 扫码识别成功,将扫描结果返回给调用者
Intent intent = new Intent();
intent.putExtra("code", code);
setResult(RESULT_OK, intent);
finish();
} else {
// 提示扫码结果格式不正确
Toast.makeText(this, "扫描结果不是纯数字", Toast.LENGTH_SHORT).show();
mScannerView.resumeCameraPreview(this);
}
}
}
步骤三:调用扫码Activity获取数字结果
- 在需要使用扫码功能的Activity中添加按钮
<Button
android:id="@+id/btn_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="扫一扫" />
- 实现按钮的点击事件
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_SCAN = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnScan = findViewById(R.id.btn_scan);
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ScanActivity.class);
startActivityForResult(intent, REQUEST_CODE_SCAN);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK && data != null) {
String code = data.getStringExtra("code");
if (code != null) {
// 扫码识别成功
Toast.makeText(this, "扫描结果:" + code, Toast.LENGTH_SHORT).show();
} else {
// 扫码失败
Toast.makeText(this, "扫描失败", Toast.LENGTH_SHORT).show();
}
}
}
}
示例1:使用扫码功能获取验证密码
假设有一个应用需要进行短信验证码登录,使用扫码功能可以简化用户的操作。具体实现方式如下:
- 在服务端生成随机的四位数字密码
import java.util.Random;
public class PasswordGenerator {
private static final int PASSWORD_LENGTH = 4;
public static String generate() {
StringBuilder builder = new StringBuilder();
Random random = new Random();
for (int i = 0; i < PASSWORD_LENGTH; i++) {
builder.append(random.nextInt(10));
}
return builder.toString();
}
}
- 在登录Activity中添加获取验证码按钮
<Button
android:id="@+id/btn_get_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取验证码" />
- 在点击事件中生成密码并显示在控件上
public class LoginActivity extends AppCompatActivity {
private TextView mTextPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mTextPassword = findViewById(R.id.text_password);
Button btnGetPassword = findViewById(R.id.btn_get_password);
btnGetPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String password = PasswordGenerator.generate();
mTextPassword.setText(password);
}
});
}
// ...
}
- 在登录Activity中添加扫码按钮
<Button
android:id="@+id/btn_scan_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="扫描二维码登录" />
- 实现扫码按钮的点击事件
public class LoginActivity extends AppCompatActivity {
private static final int REQUEST_CODE_SCAN = 1;
private TextView mTextPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mTextPassword = findViewById(R.id.text_password);
Button btnGetPassword = findViewById(R.id.btn_get_password);
btnGetPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String password = PasswordGenerator.generate();
mTextPassword.setText(password);
}
});
Button btnScanCode = findViewById(R.id.btn_scan_code);
btnScanCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, ScanActivity.class);
startActivityForResult(intent, REQUEST_CODE_SCAN);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK && data != null) {
String code = data.getStringExtra("code");
if (code != null && code.equals(mTextPassword.getText().toString())) {
// 扫码成功,验证密码正确,登录成功
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
} else {
// 验证密码错误,登录失败
Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
}
}
}
}
示例2:使用扫码功能获取支付订单号
假设有一个支付功能,需要用户输入订单号才能进行支付,使用扫码功能可以让用户快速获得订单号。具体实现方式如下:
- 在支付Activity中添加获取支付订单按钮
<Button
android:id="@+id/btn_get_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取支付订单" />
- 在点击事件中生成订单号并显示在控件上
public class PaymentActivity extends AppCompatActivity {
private TextView mTextOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
mTextOrder = findViewById(R.id.text_order);
Button btnGetOrder = findViewById(R.id.btn_get_order);
btnGetOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String order = OrderGenerator.generate();
mTextOrder.setText(order);
}
});
}
// ...
}
- 在支付Activity中添加扫码按钮
<Button
android:id="@+id/btn_scan_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="扫描二维码支付" />
- 实现扫码按钮的点击事件
public class PaymentActivity extends AppCompatActivity {
private static final int REQUEST_CODE_SCAN = 1;
private TextView mTextOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
mTextOrder = findViewById(R.id.text_order);
Button btnGetOrder = findViewById(R.id.btn_get_order);
btnGetOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String order = OrderGenerator.generate();
mTextOrder.setText(order);
}
});
Button btnScanCode = findViewById(R.id.btn_scan_code);
btnScanCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(PaymentActivity.this, ScanActivity.class);
startActivityForResult(intent, REQUEST_CODE_SCAN);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK && data != null) {
String code = data.getStringExtra("code");
if (code != null && code.equals(mTextOrder.getText().toString())) {
// 扫码成功,验证订单正确,进行支付
Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
} else {
// 验证订单错误,支付失败
Toast.makeText(this, "订单错误", Toast.LENGTH_SHORT).show();
}
}
}
}
以上是实现“Android实现扫一扫识别数字功能”的完整攻略,希望能对你有所帮助!
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android实现扫一扫识别数字功能 - Python技术站