二维码

1.学习内容
   a.今天讲一下目前移动领域很常用的技术——二维码;现在大街小巷、各大网站都有二维码的踪迹,不管是IOS、Android、WP都有相关支持的软件。
    b.在Android平台上主流还是用zxing库,ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,可以实现使用手机的内置的摄像头完成条形码的扫描及解码。
    c.因此这里主要讲述如何利用zxing进行二维码开发。 生成和识别二维码,以实用性为主。

2.准备工作
   a.首先需要在github下载开源库,然后进行删减保留我们所需要的功能。
    下面现对用到的类进行一个简单的介绍:
    b. ZXing库里面主要的类以及这些类的作用:
  • CaptureActivity             这个是启动Activity 也就是扫描器。其中有个重要的方法是handleDecode()是对扫描结果处理。
  • decoding                      解码相关的类。
  • encoding                      编码相关的类。
  • camera                        包,摄像头控制包。
  • View                            扫描取景类的包。

3. 将减少过后的工程导入eclipse中或者android studio中
   a.实现一个扫描生成图片的过程,
   这个主要依赖CaptureActivity这个类,这个类中比较重要的方法是handleDecode()。用Intent调用返回一个扫描成功的结果。
   
   b.一个实现输入字符串生成二维码图片的功能。
   这个过程主要依赖EncodingHandler这个类中的createQRCode()方法。根据二维码的算法生成一个bitmap图片。


4. 关于实际开发中可能遇到的问题(定制你所需要的二维码扫描界面)
  • 上面的代码还是比较简单,但是要想做出像微信那样只的扫描框,紧紧上面的代码是没有那种效果的,我们必须重写com.mining.app.zxing.view包下面的ViewfinderView类,微信里面的都是用的图片,我是自己画出来的,代码注释的比较清楚,大家直接看代码吧,相信你能理解的,如果你要修改扫描框的大小,去CameraManager类里面修改
  • public Rect getFramingRect() { 
  •   Point screenResolution = configManager.getScreenResolution();
  •   if (framingRect == null) {
  •    if (camera == null) {
  •     return null;
  •    }
  •    // 宽度
  •    // int width = screenResolution.x * 3 / 4;
  •    // if (width < MIN_FRAME_WIDTH) {
  •    // width = MIN_FRAME_WIDTH;
  •    // } else if (width > MAX_FRAME_WIDTH) {
  •    // width = MAX_FRAME_WIDTH;
  •    // }
  •    
  •    //高度
  •             // int height = screenResolution.y * 3 / 4;
  •             //if (height < MIN_FRAME_HEIGHT) {
  •             //height = MIN_FRAME_HEIGHT;
  •             //} else if (height > MAX_FRAME_HEIGHT) {
  •             //height = MAX_FRAME_HEIGHT;
  •             //}
  •    
  •    //宽高修改如下:
  •    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
  •    int width = (int) (metrics.widthPixels * 0.6);
  •    int height = (int) (width * 0.9);
  •    
  •    
  •    //这里是左边和上边的偏移量
  •    int leftOffset = (screenResolution.x - width) / 2;
  •    //int topOffset = (screenResolution.y - height) / 2;
  •    
  •    //屏幕距离上边的偏移量修改:
  •    int topOffset=(screenResolution.y - height)/4;
  •    framingRect = new Rect(leftOffset, topOffset, leftOffset + width,
  •      topOffset + height);
  •    Log.d(TAG, "Calculated framing rect: " + framingRect);
  •   }
  •   return framingRect;
  • 修改扫描框的界面如下:

  1. /* 
  2.  * Copyright (C) 2008 ZXing authors 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package com.mining.app.zxing.view;  
  18.   
  19. import java.util.Collection;  
  20. import java.util.HashSet;  
  21.   
  22. import android.content.Context;  
  23. import android.content.res.Resources;  
  24. import android.graphics.Bitmap;  
  25. import android.graphics.Canvas;  
  26. import android.graphics.Color;  
  27. import android.graphics.Paint;  
  28. import android.graphics.Rect;  
  29. import android.graphics.Typeface;  
  30. import android.util.AttributeSet;  
  31. import android.view.View;  
  32.   
  33. import com.example.qr_codescan.R;  
  34. import com.google.zxing.ResultPoint;  
  35. import com.mining.app.zxing.camera.CameraManager;  
  36.   
  37. /** 
  38.  * This view is overlaid on top of the camera preview. It adds the viewfinder 
  39.  * rectangle and partial transparency outside it, as well as the laser scanner 
  40.  * animation and result points. 
  41.  *  
  42.  */  
  43. public final class ViewfinderView extends View {  
  44.     private static final String TAG = "log";  
  45.     /** 
  46.      * 刷新界面的时间 
  47.      */  
  48.     private static final long ANIMATION_DELAY = 10L;  
  49.     private static final int OPAQUE = 0xFF;  
  50.   
  51.     /** 
  52.      * 四个绿色边角对应的长度 
  53.      */  
  54.     private int ScreenRate;  
  55.       
  56.     /** 
  57.      * 四个绿色边角对应的宽度 
  58.      */  
  59.     private static final int CORNER_WIDTH = 10;  
  60.     /** 
  61.      * 扫描框中的中间线的宽度 
  62.      */  
  63.     private static final int MIDDLE_LINE_WIDTH = 6;  
  64.       
  65.     /** 
  66.      * 扫描框中的中间线的与扫描框左右的间隙 
  67.      */  
  68.     private static final int MIDDLE_LINE_PADDING = 5;  
  69.       
  70.     /** 
  71.      * 中间那条线每次刷新移动的距离 
  72.      */  
  73.     private static final int SPEEN_DISTANCE = 5;  
  74.       
  75.     /** 
  76.      * 手机的屏幕密度 
  77.      */  
  78.     private static float density;  
  79.     /** 
  80.      * 字体大小 
  81.      */  
  82.     private static final int TEXT_SIZE = 16;  
  83.     /** 
  84.      * 字体距离扫描框下面的距离 
  85.      */  
  86.     private static final int TEXT_PADDING_TOP = 30;  
  87.       
  88.     /** 
  89.      * 画笔对象的引用 
  90.      */  
  91.     private Paint paint;  
  92.       
  93.     /** 
  94.      * 中间滑动线的最顶端位置 
  95.      */  
  96.     private int slideTop;  
  97.       
  98.     /** 
  99.      * 中间滑动线的最底端位置 
  100.      */  
  101.     private int slideBottom;  
  102.       
  103.     private Bitmap resultBitmap;  
  104.     private final int maskColor;  
  105.     private final int resultColor;  
  106.       
  107.     private final int resultPointColor;  
  108.     private Collection<ResultPoint> possibleResultPoints;  
  109.     private Collection<ResultPoint> lastPossibleResultPoints;  
  110.   
  111.     boolean isFirst;  
  112.       
  113.     public ViewfinderView(Context context, AttributeSet attrs) {  
  114.         super(context, attrs);  
  115.           
  116.         density = context.getResources().getDisplayMetrics().density;  
  117.         //将像素转换成dp  
  118.         ScreenRate = (int)(20 * density);  
  119.   
  120.         paint = new Paint();  
  121.         Resources resources = getResources();  
  122.         maskColor = resources.getColor(R.color.viewfinder_mask);  
  123.         resultColor = resources.getColor(R.color.result_view);  
  124.   
  125.         resultPointColor = resources.getColor(R.color.possible_result_points);  
  126.         possibleResultPoints = new HashSet<ResultPoint>(5);  
  127.     }  
  128.   
  129.     @Override  
  130.     public void onDraw(Canvas canvas) {  
  131.         //中间的扫描框,你要修改扫描框的大小,去CameraManager里面修改  
  132.         Rect frame = CameraManager.get().getFramingRect();  
  133.         if (frame == null) {  
  134.             return;  
  135.         }  
  136.           
  137.         //初始化中间线滑动的最上边和最下边  
  138.         if(!isFirst){  
  139.             isFirst = true;  
  140.             slideTop = frame.top;  
  141.             slideBottom = frame.bottom;  
  142.         }  
  143.           
  144.         //获取屏幕的宽和高  
  145.         int width = canvas.getWidth();  
  146.         int height = canvas.getHeight();  
  147.   
  148.         paint.setColor(resultBitmap != null ? resultColor : maskColor);  
  149.           
  150.         //画出扫描框外面的阴影部分,共四个部分,扫描框的上面到屏幕上面,扫描框的下面到屏幕下面  
  151.         //扫描框的左边面到屏幕左边,扫描框的右边到屏幕右边  
  152.         canvas.drawRect(00, width, frame.top, paint);  
  153.         canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);  
  154.         canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1,  
  155.                 paint);  
  156.         canvas.drawRect(0, frame.bottom + 1, width, height, paint);  
  157.           
  158.           
  159.   
  160.         if (resultBitmap != null) {  
  161.             // Draw the opaque result bitmap over the scanning rectangle  
  162.             paint.setAlpha(OPAQUE);  
  163.             canvas.drawBitmap(resultBitmap, frame.left, frame.top, paint);  
  164.         } else {  
  165.   
  166.             //画扫描框边上的角,总共8个部分  
  167.             paint.setColor(Color.GREEN);  
  168.             canvas.drawRect(frame.left, frame.top, frame.left + ScreenRate,  
  169.                     frame.top + CORNER_WIDTH, paint);  
  170.             canvas.drawRect(frame.left, frame.top, frame.left + CORNER_WIDTH, frame.top  
  171.                     + ScreenRate, paint);  
  172.             canvas.drawRect(frame.right - ScreenRate, frame.top, frame.right,  
  173.                     frame.top + CORNER_WIDTH, paint);  
  174.             canvas.drawRect(frame.right - CORNER_WIDTH, frame.top, frame.right, frame.top  
  175.                     + ScreenRate, paint);  
  176.             canvas.drawRect(frame.left, frame.bottom - CORNER_WIDTH, frame.left  
  177.                     + ScreenRate, frame.bottom, paint);  
  178.             canvas.drawRect(frame.left, frame.bottom - ScreenRate,  
  179.                     frame.left + CORNER_WIDTH, frame.bottom, paint);  
  180.             canvas.drawRect(frame.right - ScreenRate, frame.bottom - CORNER_WIDTH,  
  181.                     frame.right, frame.bottom, paint);  
  182.             canvas.drawRect(frame.right - CORNER_WIDTH, frame.bottom - ScreenRate,  
  183.                     frame.right, frame.bottom, paint);  
  184.   
  185.               
  186.             //绘制中间的线,每次刷新界面,中间的线往下移动SPEEN_DISTANCE  
  187.             slideTop += SPEEN_DISTANCE;  
  188.             if(slideTop >= frame.bottom){  
  189.                 slideTop = frame.top;  
  190.             }  
  191.             canvas.drawRect(frame.left + MIDDLE_LINE_PADDING, slideTop - MIDDLE_LINE_WIDTH/2, frame.right - MIDDLE_LINE_PADDING,slideTop + MIDDLE_LINE_WIDTH/2, paint);  
  192.               
  193.               
  194.             //画扫描框下面的字  
  195.             paint.setColor(Color.WHITE);  
  196.             paint.setTextSize(TEXT_SIZE * density);  
  197.             paint.setAlpha(0x40);  
  198.             paint.setTypeface(Typeface.create("System", Typeface.BOLD));  
  199.             canvas.drawText(getResources().getString(R.string.scan_text), frame.left, (float) (frame.bottom + (float)TEXT_PADDING_TOP *density), paint);  
  200.               
  201.               
  202.   
  203.             Collection<ResultPoint> currentPossible = possibleResultPoints;  
  204.             Collection<ResultPoint> currentLast = lastPossibleResultPoints;  
  205.             if (currentPossible.isEmpty()) {  
  206.                 lastPossibleResultPoints = null;  
  207.             } else {  
  208.                 possibleResultPoints = new HashSet<ResultPoint>(5);  
  209.                 lastPossibleResultPoints = currentPossible;  
  210.                 paint.setAlpha(OPAQUE);  
  211.                 paint.setColor(resultPointColor);  
  212.                 for (ResultPoint point : currentPossible) {  
  213.                     canvas.drawCircle(frame.left + point.getX(), frame.top  
  214.                             + point.getY(), 6.0f, paint);  
  215.                 }  
  216.             }  
  217.             if (currentLast != null) {  
  218.                 paint.setAlpha(OPAQUE / 2);  
  219.                 paint.setColor(resultPointColor);  
  220.                 for (ResultPoint point : currentLast) {  
  221.                     canvas.drawCircle(frame.left + point.getX(), frame.top  
  222.                             + point.getY(), 3.0f, paint);  
  223.                 }  
  224.             }  
  225.   
  226.               
  227.             //只刷新扫描框的内容,其他地方不刷新  
  228.             postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top,  
  229.                     frame.right, frame.bottom);  
  230.               
  231.         }  
  232.     }  
  233.   
  234.     public void drawViewfinder() {  
  235.         resultBitmap = null;  
  236.         invalidate();  
  237.     }  
  238.   
  239.     /** 
  240.      * Draw a bitmap with the result points highlighted instead of the live 
  241.      * scanning display. 
  242.      *  
  243.      * @param barcode 
  244.      *            An image of the decoded barcode. 
  245.      */  
  246.     public void drawResultBitmap(Bitmap barcode) {  
  247.         resultBitmap = barcode;  
  248.         invalidate();  
  249.     }  
  250.   
  251.     public void addPossibleResultPoint(ResultPoint point) {  
  252.         possibleResultPoints.add(point);  
  253.     }  
  254.   
  255. }  

中间画的线:

[java]  view plain copy 派生到我的代码片
  1. canvas.drawRect(frame.left + MIDDLE_LINE_PADDING, slideTop - MIDDLE_LINE_WIDTH/2, frame.right - MIDDLE_LINE_PADDING,slideTop + MIDDLE_LINE_WIDTH/2, paint);  

改成

[java]  view plain copy 派生到我的代码片
  1. Rect lineRect = new Rect();  
  2.             lineRect.left = frame.left;  
  3.             lineRect.right = frame.right;  
  4.             lineRect.top = slideTop;  
  5.             lineRect.bottom = slideTop + 18;  
  6.             canvas.drawBitmap(((BitmapDrawable)(getResources().getDrawable(R.drawable.qrcode_scan_line))).getBitmap(), null, lineRect, paint);  
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值