• <二维码>———二维码生成器之绘制二维码


    最近做项目的时候,制码方提供了一个制码实例中,对二维码的绘制部分使用Line,在电脑上显示速度还不错,但到了手机上速度让人难以忍受。

    所以写了这篇文章,介绍一种使用WriteableBitmap来显示二维码的方法。(本文制码库以zxing作为介绍)

    具体代码如下:

    1、高效代码

     1 public static WriteableBitmap CreateBarcode() 
     2 { 
     3     WriteableBitmap wb = null; 
     4     QRCodeWriter writer = new QRCodeWriter(); 
     5     ByteMatrix bitMatrix = null; 
     6     try 
     7     { 
     8         bitMatrix = writer.encode("HTTP://WWW.GOOGLE.COM", BarcodeFormat.QR_CODE, 300, 300); 
     9         wb = ConvertByteMartixToWriteableBitmap(bitMatrix); 
    10     } 
    11     catch (WriterException e) 
    12     { 
    13     } 
    14     catch (IOException e) 
    15     { 
    16     } 
    17     return wb; 
    18 } 
    19 
    20 public static WriteableBitmap ConvertByteMartixToWriteableBitmap(ByteMatrix bm)   
    21 {          
    22     WriteableBitmap wb = new WriteableBitmap(bm.Width, bm.Height); 
    23     for (int x = 0; x <= wb.PixelWidth - 1; x++)        
    24     {         
    25         for (int y = 0; y <= wb.PixelHeight - 1; y++)      
    26         {                 
    27             if (bm.Array[y][x] == -1)    
    28             {              
    29                 //白色            
    30                 wb.Pixels[wb.PixelWidth * y + x] = BitConverter.ToInt32(BitConverter.GetBytes(0xffffffff), 0);     
    31             }               
    32             else       
    33             {           
    34                 //黑色       
    35                 wb.Pixels[wb.PixelWidth * y + x] = BitConverter.ToInt32(BitConverter.GetBytes(0xff000000), 0);   
    36             }            
    37         }           
    38     }         
    39     return wb;  
    40 }

    2、低效代码

     1 public WriteableBitmap CreateBarcode() 
     2 { 
     3     WriteableBitmap wb = null; 
     4     QRCodeWriter writer = new QRCodeWriter(); 
     5     ByteMatrix bitMatrix = null; 
     6     try 
     7     { 
     8         bitMatrix = writer.encode("HTTP://WWW.GOOGLE.COM", BarcodeFormat.QR_CODE, 300, 300); 
     9         wb = ConvertByteMartixToWriteableBitmap(bitMatrix); 
    10     } 
    11     catch (WriterException e) 
    12     { 
    13     } 
    14     catch (IOException e) 
    15     { 
    16     } 
    17     return wb; 
    18 } 
    19 
    20 public void ConvertByteMartixToWriteableBitmap(ByteMatrix bm)   
    21 {                            
    22     sbyte[][] array = bm.Array;
    23     int h = bm.Height; int wid = 4;
    24     for (int i = 0; i < h; i++)
    25     {
    26         for (int j = 0; j < h; j++)
    27         {
    28             if (array[j][i] == 0)
    29             {                       
    30                 Line rc = new Line();
    31                 rc.X1 = i * wid; rc.Y1 = j * wid;
    32                 rc.X2 = (i + 1) * wid; rc.Y2 = j * wid;
    33                 rc.StrokeThickness = wid;
    34                 rc.Fill = new SolidColorBrush(Colors.Black);
    35                 rc.Stroke = new SolidColorBrush(Colors.Black);
    36 
    37                 this.canvas.Children.Add(rc);
    38             }
    39         }
    40     }  
    41 }


    但我看了公司里android的做法

     1 public void onDraw(Canvas mCanvas01, boolean[][] bEncoding, int color) 
     2 {
     3     super.onDraw(mCanvas01);
     4     Log.i("MainActivity", "开始画图");
     5     //int intPadding = 50;
     6     int intPadding = 0;
     7     // 设置画图绘图颜色
     8     mCanvas01.drawColor(Color.WHITE);
     9     // 创建画笔
    10     Paint mPaint01 = new Paint();
    11     // 设置画笔颜色和模板
    12     mPaint01.setStyle(Paint.Style.FILL);
    13     mPaint01.setColor(color);
    14     mPaint01.setStrokeWidth(1.0F);
    15     // 逐一加载boolean数组
    16     for (int i = 0; i < bEncoding.length; i++) 
    17     {
    18         for (int j = 0; j < bEncoding.length; j++) 
    19         {
    20             if (bEncoding[j][i])
    21             {
    22                 // 绘出条形码方块
    23                 mCanvas01.drawRect(new Rect(intPadding + j * 3 + 2,// 左上边x
    24                 intPadding + i * 3 + 2,// 左上边y
    25                 intPadding + j * 3 + 2 + 3,// 右下边x
    26                 intPadding + i * 3 + 2 + 3// 右下边y
    27                 ), mPaint01);
    28             }
    29         }
    30     }
    31 }

    是使用双重循环绘制,不知道和wp中Rectangle(或者Line)绘制有什么特别区别,是不是因为wp中Rectangle(或者Line)是一个控件需要申请控件,占用内存的原因,有知道的希望指教。

  • 相关阅读:
    联考20200604 T2 宝石
    联考20200604 T1 旅游
    联考20200603 T2 排列
    [HAOI2017]八纵八横
    联考20200603 T1 解码
    [POI2011]KON-Conspiracy
    CF917D Stranger Trees
    CF1278F Cards
    CF809E Surprise me!
    NOI2016 循环之美
  • 原文地址:https://www.cnblogs.com/qq278360339/p/2726399.html
Copyright © 2020-2023  润新知