邹生 發表於 2019-11-28 15:36:00

Android开发 QRCode二维码开发第三方框架

<h1><span style="color: rgba(0, 128, 128, 1)">前言</span></h1>
<p><span style="color: rgba(0, 0, 0, 1)">  Android开发里二维码开发经常用到,这里简单的介绍下Android开发里的二维码.</span></p>
<h1><span style="color: rgba(0, 128, 128, 1)">最广泛使用的二维码库zxing</span></h1>
<p><span style="color: rgba(0, 0, 0, 1)">  zxing是最广泛的二维码库各个平台都可以适用它,但是Android平台使用它好像需要进行JNI处理.但是,github上大神已经帮我们做好了,下面我会介绍一个好用的二维码框架.这里提zxing是让你知道很多二维码框架的封装源头都是它.</span></p>
<p><span style="color: rgba(0, 128, 128, 1)"><span style="color: rgba(0, 0, 0, 1)">github地址:</span> https://github.com/zxing/zxing</span></p>
<div class="cnblogs_code">
<pre>implementation 'com.google.zxing:core:3.4.0'</pre>
</div>
<h1 class="public "><span class="path-divider" style="color: rgba(0, 128, 128, 1)"><strong>barcodescanner </strong></span><span class="path-divider" style="color: rgba(0, 128, 128, 1)"><strong>第三方二维码框架</strong> </span></h1>
<p><span class="path-divider" style="color: rgba(0, 0, 0, 1)">github地址:https://github.com/dm77/barcodescanner</span></p>
<p><span class="path-divider" style="color: rgba(0, 0, 0, 1)">这个框架已经将zxing打包封装好了,你直接使用即可,甚至还能使用依赖它后独立使用zxing的功能.(比如生成二维码图片)</span></p>
<p><span class="path-divider" style="color: rgba(0, 0, 0, 1)">ps: 这个框架也支持条形码</span></p>
<h2 class="public "><span class="path-divider"><strong>依赖</strong><br></span></h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">repositories {
   jcenter()
}
implementation </span>'me.dm7.barcodescanner:zxing:1.9.13'</pre>
</div>
<h2>扫码识别内容</h2>
<p>这里偷懒引用作者的代码...</p>
<p>activity里</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> SimpleScannerActivity <span style="color: rgba(0, 0, 255, 1)">extends</span> Activity <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> ZXingScannerView.ResultHandler {
      </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> ZXingScannerView mScannerView;

      @Override
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(Bundle state) {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(state);
            mScannerView </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ZXingScannerView(<span style="color: rgba(0, 0, 255, 1)">this</span>);   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 初始化扫描仪视图</span>
<span style="color: rgba(0, 0, 0, 1)">            setContentView(mScannerView);               
      }

      @Override
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onResume() {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onResume();
            mScannerView.setResultHandler(</span><span style="color: rgba(0, 0, 255, 1)">this</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将自己注册为扫描结果的处理程序。</span>
            mScannerView.startCamera();          <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在启动相机</span>
<span style="color: rgba(0, 0, 0, 1)">      }

      @Override
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onPause() {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onPause();
            mScannerView.stopCamera();         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 暂停相机</span>
<span style="color: rgba(0, 0, 0, 1)">      }

      @Override
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> handleResult(Result rawResult) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 扫描结果</span>
            Log.v(TAG, rawResult.getText()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Prints scan results</span>
            Log.v(TAG, rawResult.getBarcodeFormat().toString()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Prints the scan format (qrcode, pdf417 etc.)

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> If you would like to resume scanning, call this method below:</span>
            mScannerView.resumeCameraPreview(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
      }</span></pre>
</div>
<h2>其他API</h2>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置闪光灯</span>
      <span style="color: rgba(0, 0, 255, 1)">void</span> setFlash(<span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)">);

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 切换自动对焦</span>
      <span style="color: rgba(0, 0, 255, 1)">void</span> setAutoFocus(<span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)">);

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 指定感兴趣的条形码格式</span>
      <span style="color: rgba(0, 0, 255, 1)">void</span> setFormats(List&lt;BarcodeFormat&gt;<span style="color: rgba(0, 0, 0, 1)"> formats);

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 指定cameraId</span>
      <span style="color: rgba(0, 0, 255, 1)">void</span> startCamera(<span style="color: rgba(0, 0, 255, 1)">int</span> cameraId);</pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">QRCodeReaderView&nbsp;<span class="path-divider"><strong>第三方二维码框架</strong></span></span></h1>
<p><span style="color: rgba(0, 0, 0, 1)"><span class="path-divider">缺点,无法扫描条形码</span></span></p>
<p><span style="color: rgba(0, 0, 0, 1)"><span class="path-divider">优点,可以自定义页面布局</span></span></p>
<p>github地址:https://github.com/dlazaro66/QRCodeReaderView</p>
<h1><span style="color: rgba(0, 128, 128, 1)">基于zxing的库生成二维码图片</span></h1>
<p>利用第三方库里的zxing库生成二维码,<strong>请注意!图片生成是耗时工作需要在子线程中运行</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 二维码工具类
* </span><span style="color: rgba(128, 128, 128, 1)">@version</span><span style="color: rgba(0, 128, 0, 1)"> 创建时间:2014年12月5日 下午5:15:47
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> QrCodeUtils {
   
    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 传入字符串生成二维码
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> str
   * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)">
   * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> WriterException
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> Bitmap Create2DCode(String str) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> WriterException {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败</span>
      BitMatrix matrix = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MultiFormatWriter().encode(str,
                BarcodeFormat.QR_CODE, </span>300, 300<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">int</span> width =<span style="color: rgba(0, 0, 0, 1)"> matrix.getWidth();
      </span><span style="color: rgba(0, 0, 255, 1)">int</span> height =<span style="color: rgba(0, 0, 0, 1)"> matrix.getHeight();
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 二维矩阵转为一维像素数组,也就是一直横着排了</span>
      <span style="color: rgba(0, 0, 255, 1)">int</span>[] pixels = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>;
      </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> y = 0; y &lt; height; y++<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> x = 0; x &lt; width; x++<span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (matrix.get(x, y)) {
                  pixels = 0xff000000<span style="color: rgba(0, 0, 0, 1)">;
                }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
                  pixels = 0xffffffff<span style="color: rgba(0, 0, 0, 1)">;
                }

            }
      }

      Bitmap bitmap </span>=<span style="color: rgba(0, 0, 0, 1)"> Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通过像素数组生成bitmap,具体参考api</span>
      bitmap.setPixels(pixels, 0, width, 0, 0<span style="color: rgba(0, 0, 0, 1)">, width, height);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> bitmap;
    }
}</span></pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">基于zxing库解析二维码图片文件</span></h1>
<p><strong>请注意!二维码图片解析是耗时工作需要在子线程中运行</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * zxing解码二维码图片
   *
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> bitmap
   * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)">
   * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> NotFoundException
   * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> IOException
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> String zxingScanQrcodePicture(Bitmap bitmap) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> NotFoundException, IOException {
      Map</span>&lt;DecodeHintType, Object&gt; hints = <span style="color: rgba(0, 0, 255, 1)">new</span> EnumMap&lt;DecodeHintType, Object&gt;(DecodeHintType.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
      Collection</span>&lt;BarcodeFormat&gt; decodeFormats = EnumSet.noneOf(BarcodeFormat.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
      decodeFormats.addAll(EnumSet.of(BarcodeFormat.QR_CODE));
      hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
      hints.put(DecodeHintType.CHARACTER_SET, </span>"UTF-8"<span style="color: rgba(0, 0, 0, 1)">);

      </span><span style="color: rgba(0, 0, 255, 1)">int</span> lWidth =<span style="color: rgba(0, 0, 0, 1)"> bitmap.getWidth();
      </span><span style="color: rgba(0, 0, 255, 1)">int</span> lHeight =<span style="color: rgba(0, 0, 0, 1)"> bitmap.getHeight();
      </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] lPixels = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>;
      bitmap.getPixels(lPixels, </span>0, lWidth, 0, 0<span style="color: rgba(0, 0, 0, 1)">, lWidth, lHeight);
      BinaryBitmap binaryBitmap </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> BinaryBitmap(<span style="color: rgba(0, 0, 255, 1)">new</span> HybridBinarizer(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RGBLuminanceSource(lWidth, lHeight, lPixels)));

      Result lResult </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MultiFormatReader().decode(binaryBitmap, hints);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> lResult.getText();
    }</span></pre>
</div>
<p>&nbsp;</p>
<h1><span style="color: rgba(0, 128, 128, 1)">BGAQRCode-Android</span></h1>
<p><span style="color: rgba(0, 0, 0, 1)">收藏率比较多的二维码与条形码扫描 第三方库</span></p>
<p>github地址&nbsp;&nbsp;https://github.com/bingoogolapple/BGAQRCode-Android/</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>End</p>

</div>
<div id="MySignature" role="contentinfo">
    <div style="text-align: center">
    <p style="color:orange;font-size:16px;" >本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/11950397.html </p>
    <div style="color:orange;font-size:16px;">本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。 </div>
</div><br><br>
来源:https://www.cnblogs.com/guanxinjing/p/11950397.html
頁: [1]
查看完整版本: Android开发 QRCode二维码开发第三方框架