经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android实现扫码功能
来源:jb51  时间:2021/5/31 18:23:39  对本文有异议

本文实例为大家分享了Android实现扫码功能的具体代码,供大家参考,具体内容如下

1、引入

  1. implementation 'com.journeyapps:zxing-android-embedded:3.5.0'

2、使用:

  1. public void initScan() {
  2. IntentIntegrator integrator = new IntentIntegrator(this);
  3. // 设置要扫描的条码类型,ONE_D_CODE_TYPES:一维码,QR_CODE_TYPES-二维码
  4. integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
  5. integrator.setCaptureActivity(ScanActivity.class); //设置打开摄像头的Activity
  6. integrator.setPrompt(""); //底部的提示文字,设为""可以置空
  7. integrator.setCameraId(0); //前置或者后置摄像头
  8. integrator.setBeepEnabled(true); //扫描成功的「哔哔」声,默认开启
  9. integrator.setBarcodeImageEnabled(true);
  10. integrator.initiateScan();
  11. }
  12. @Override
  13. protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  14. super.onActivityResult(requestCode, resultCode, intent);
  15. if (requestCode == IntentIntegrator.REQUEST_CODE) {
  16. IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
  17. if (scanResult != null && scanResult.getContents() != null) {
  18. String result = scanResult.getContents();
  19. LogUtil.d("扫码返回: " + result);
  20. try {
  21. JSONObject jsonObject = new JSONObject(result);
  22. if (jsonObject.has(Constant.USERPOLICEMENID)) {
  23. //TODO 逻辑
  24. } else {
  25. ToastUtil.showShortToast("未扫描到有效的信息");
  26. }
  27. } catch (Exception e) {
  28. ToastUtil.showShortToast("未扫描到有效的信息");
  29. e.printStackTrace();
  30. }
  31. } else {
  32. ToastUtil.showShortToast("未扫描到有效的信息");
  33. }
  34. }
  35. }

3、ScanActivity

  1. public class ScanActivity extends BackActivity {
  2. @BindView(R.id.dbv)
  3. DecoratedBarcodeView mDbv;
  4. private CaptureManager captureManager;
  5. @Override
  6. protected int getLayoutId() {
  7. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  8. return R.layout.activity_scan;
  9. }
  10. @Override
  11. protected void init() {
  12. super.init();
  13. captureManager = new CaptureManager(this, mDbv);
  14. captureManager.initializeFromIntent(getIntent(), getSavedInstanceState());
  15. captureManager.decode();
  16. }
  17. @Override
  18. public void onSaveInstanceState(@NotNull Bundle outState, @NotNull PersistableBundle outPersistentState) {
  19. super.onSaveInstanceState(outState, outPersistentState);
  20. captureManager.onSaveInstanceState(outState);
  21. }
  22. @Override
  23. public boolean onKeyDown(int keyCode, KeyEvent event) {
  24. return mDbv.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
  25. }
  26. @Override
  27. protected void onPause() {
  28. super.onPause();
  29. captureManager.onPause();
  30. }
  31. @Override
  32. public void onResume() {
  33. super.onResume();
  34. captureManager.onResume();
  35. }
  36. @Override
  37. protected void onDestroy() {
  38. super.onDestroy();
  39. captureManager.onDestroy();
  40. }
  41. }

4、布局文件

activity_scan

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".ui.main.alarmassistant.ScanActivity">
  9. <com.journeyapps.barcodescanner.DecoratedBarcodeView
  10. android:id="@+id/dbv"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:fitsSystemWindows="true"
  14. app:zxing_framing_rect_height="200dp"
  15. app:zxing_framing_rect_width="200dp"
  16. app:zxing_preview_scaling_strategy="fitXY"
  17. app:zxing_scanner_layout="@layout/view_qr"
  18. app:zxing_use_texture_view="true" />
  19. </LinearLayout>

view_qr

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <merge xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto">
  4. <com.journeyapps.barcodescanner.BarcodeView
  5. android:id="@+id/zxing_barcode_surface"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. app:zxing_framing_rect_height="50dp"
  9. app:zxing_framing_rect_width="250dp" />
  10. <com.x.x.widget.QrView
  11. android:id="@+id/zxing_viewfinder_view"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. app:zxing_possible_result_points="@color/color_white"
  15. app:zxing_result_view="@color/zxing_custom_result_view"
  16. app:zxing_viewfinder_laser="@color/color_white"
  17. app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask" />
  18. <TextView
  19. android:id="@+id/zxing_status_view"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_gravity="bottom|center_horizontal"
  23. android:layout_marginBottom="30dp"
  24. android:background="@color/zxing_transparent"
  25. android:text="@string/zxing_msg_default_status"
  26. android:textColor="@color/zxing_status_text" />
  27. <com.x.common.widget.MyActionBar
  28. android:id="@+id/action_bar"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. app:dark_mode="true"
  32. app:title="扫一扫" />
  33. </merge>

5、

  1. /**
  2. * 自定义扫描界面
  3. */
  4. public class QrView extends ViewfinderView {
  5. public int laserLinePosition = 0;
  6. public float[] position = new float[]{0f, 0.5f, 1f};
  7. public int[] colors = new int[]{0x0027B14D, 0xff27B14D, 0x0027B14D};
  8. public LinearGradient linearGradient;
  9. private int ScreenRate;
  10. public QrView(Context context, AttributeSet attrs) {
  11. super(context, attrs);
  12. float density = context.getResources().getDisplayMetrics().density;
  13. ScreenRate = (int) (15 * density);
  14. }
  15. @Override
  16. public void onDraw(Canvas canvas) {
  17. int CORNER_WIDTH = 15;
  18. refreshSizes();
  19. if (framingRect == null || previewFramingRect == null) {
  20. return;
  21. }
  22. Rect frame = framingRect;
  23. Rect previewFrame = previewFramingRect;
  24. int width = canvas.getWidth();
  25. int height = canvas.getHeight();
  26. //绘制4个角
  27. paint.setColor(getResources().getColor(R.color.color_white));//定义画笔的颜色
  28. canvas.drawRect(frame.left, frame.top, frame.left + ScreenRate, frame.top + CORNER_WIDTH, paint);
  29. canvas.drawRect(frame.left, frame.top, frame.left + CORNER_WIDTH, frame.top + ScreenRate, paint);
  30. canvas.drawRect(frame.right - ScreenRate, frame.top, frame.right, frame.top + CORNER_WIDTH, paint);
  31. canvas.drawRect(frame.right - CORNER_WIDTH, frame.top, frame.right, frame.top + ScreenRate, paint);
  32. canvas.drawRect(frame.left, frame.bottom - CORNER_WIDTH, frame.left + ScreenRate, frame.bottom, paint);
  33. canvas.drawRect(frame.left, frame.bottom - ScreenRate, frame.left + CORNER_WIDTH, frame.bottom, paint);
  34. canvas.drawRect(frame.right - ScreenRate, frame.bottom - CORNER_WIDTH, frame.right, frame.bottom, paint);
  35. canvas.drawRect(frame.right - CORNER_WIDTH, frame.bottom - ScreenRate, frame.right, frame.bottom, paint);
  36. // 画出外部(即构图矩形之外)变暗
  37. paint.setColor(resultBitmap != null ? resultColor : maskColor);
  38. canvas.drawRect(0, 0, width, frame.top, paint);
  39. canvas.drawRect(0, frame.top, frame.left, frame.bottom, paint);
  40. canvas.drawRect(frame.right, frame.top, width, frame.bottom, paint);
  41. canvas.drawRect(0, frame.bottom, width, height, paint);
  42. if (resultBitmap != null) {
  43. // Draw the opaque result bitmap over the scanning rectangle
  44. paint.setAlpha(CURRENT_POINT_OPACITY);
  45. canvas.drawBitmap(resultBitmap, null, frame, paint);
  46. } else {
  47. laserLinePosition = laserLinePosition + 8;
  48. if (laserLinePosition >= frame.height()) {
  49. laserLinePosition = 0;
  50. }
  51. linearGradient = new LinearGradient(frame.left + 1, frame.top + laserLinePosition, frame.right - 1, frame.top + 10 + laserLinePosition, colors, position, Shader.TileMode.CLAMP);
  52. // Draw a red "laser scanner" line through the middle to show decoding is active
  53. paint.setShader(linearGradient);
  54. //绘制扫描线
  55. canvas.drawRect(frame.left + 1, frame.top + laserLinePosition, frame.right - 1, frame.top + 10 + laserLinePosition, paint);
  56. paint.setShader(null);
  57. float scaleX = frame.width() / (float) previewFrame.width();
  58. float scaleY = frame.height() / (float) previewFrame.height();
  59. List<ResultPoint> currentPossible = possibleResultPoints;
  60. List<ResultPoint> currentLast = lastPossibleResultPoints;
  61. int frameLeft = frame.left;
  62. int frameTop = frame.top;
  63. if (currentPossible.isEmpty()) {
  64. lastPossibleResultPoints = null;
  65. } else {
  66. possibleResultPoints = new ArrayList<>(5);
  67. lastPossibleResultPoints = currentPossible;
  68. paint.setAlpha(CURRENT_POINT_OPACITY);
  69. paint.setColor(resultPointColor);
  70. for (ResultPoint point : currentPossible) {
  71. canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX), frameTop + (int) (point.getY() * scaleY), POINT_SIZE, paint);
  72. }
  73. }
  74. if (currentLast != null) {
  75. paint.setAlpha(CURRENT_POINT_OPACITY / 2);
  76. paint.setColor(resultPointColor);
  77. float radius = POINT_SIZE / 2.0f;
  78. for (ResultPoint point : currentLast) {
  79. canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX), frameTop + (int) (point.getY() * scaleY), radius, paint);
  80. }
  81. }
  82. postInvalidateDelayed(16, frame.left, frame.top, frame.right, frame.bottom);
  83. }
  84. }
  85. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号