二维码扫描

二维码

  • 扫描区域的绘制

    • 布局实现

      <SurfaceView
      android:id="@+id/preview_view"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" />

      <com.google.zxing.client.android.decode.ViewfinderView
      android:id="@+id/viewfinder_view"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true" />

  • 初始化相机管理器

      cameraManager = new CameraManager(getApplication());
    
      viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
      viewfinderView.setCameraManager(cameraManager);
    
      handler = null;
      SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
      SurfaceHolder surfaceHolder = surfaceView.getHolder();
      if (hasSurface) {
          // The activity was paused but not stopped, so the surface still
          // exists. Therefore
          // surfaceCreated() won't be called, so init the camera here.
          initCamera(surfaceHolder);
      } else {
          // Install the callback and wait for surfaceCreated() to init the
          // camera.
          surfaceHolder.addCallback(this);
          surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      }
    
  • 解析扫描结果

      public void handleDecode(Result rawResult, Bitmap barcode) {
      inactivityTimer.onActivity();
      ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(
              this, rawResult);
    
      boolean fromLiveScan = barcode != null;
      if (fromLiveScan) {
          // Then not from history, so beep/vibrate and we have an image to
          // draw on
          beepManager.playBeepSoundAndVibrate();
          // drawResultPoints(barcode, rawResult);
          viewfinderView.drawResultBitmap(barcode);
      }
    
      String text = rawResult.getText();
    
      Toast.makeText(this, "扫描结果:" + text, Toast.LENGTH_LONG).show();
      Log.d(TAG, "result-->" + text);
    

    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 很多朋友没有做过二维码扫描方面的可能会觉得比较复杂,先定义就是不好下手,但是自己仔细写一下其实也好,毕竟网上类似二...
    2c2z0阅读 479评论 0 0
  • 了解二维码这个东西还是从微信中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从一张图片中扫一下竟然能直接加好友,...
    AiPuff阅读 883评论 0 1
  • 二维码扫描最近两年简直是风靡移动互联网时代,尤其在国内发展神速。围绕条码扫码功能,首先说说通过本文你可以知道啥。一...
    55book阅读 4,226评论 0 1
  • Zxing已经是一个很成熟的框架了,但它是用maven构建的项目,在以gradle为基础的AS中集成起来总感觉不太...
    雾中的影子阅读 46,365评论 7 28
  • 周五,晴 今天是白色情人节。 晚上打完球回来,在校园里偶遇告白,当然不是跟我告白ˊ_>ˋ 心形蜡烛,男生很勇敢,捧...
    蘑菇菌阅读 95评论 0 1