二维码比起条形码更有自身的优势,比如它高达千个字符的容量、纠错能力及汉字支持。二维码现在在国内已经有不少领域应用到,特别是在网站及移动端中更是应用得更加广泛。

最近的项目中也需要用到二维码去显示一些摘要信息,然后用手机或者其它客户端显示出来。网上其实也不少demo,但大多是java跟C#之类。

因为unity3d本身也支持C#语法并在mono环境下运行,所以我们查找二维码在C#下的示例即可。于是又拼命的google,发现两个精品包,一个为zxing(地址为http://code.google.com/p/zxing/downloads/list),另一个则为QrCode(地址为http://qrcodenet.codeplex.com/)。zxing之听就早有耳闻,特别是在java上用得特别多,而其实QrCode也不少人用,是一个新神器来的,之前是采用了zxing的端口。

QrCode算是比较新颖的包,而且支持也相当不错,但很可惜,只能是在.net 4.0环境下运行,所以在unity3d下我只能选择zxing。

下面我们做一个简单的示例去显示一个二维码。

先把mono版本的System.Drawing.dll复制到untiy3d项目的任意位置下。

引入命名空间

1
2
3
4
5
6
7
8
9
10
11
12
13
using  UnityEngine;
using  System;
using  System.IO;
using  System.Collections;
using  System.Drawing;
using  com.google.zxing.qrcode;
using  com.google.zxing;
using  com.google.zxing.common;

using  ByteMatrix = com.google.zxing.common.ByteMatrix;
using  EAN13Writer = com.google.zxing.oned.EAN13Writer;
using  EAN8Writer = com.google.zxing.oned.EAN8Writer;
using  MultiFormatWriter = com.google.zxing.MultiFormatWriter;

定义一个Texture2D类型用于显示二维码,命名为_texure2d,长与宽为300像素;定义一个ByteMatrix类,用于写入二维码信息。

1
2
3
4
5
6
private  int  _width = 300;
private  int  _height = 300;
public  Texture2D _texure2d;
private  bool  _success;
private  string  input =  "" ;
ByteMatrix byteMatrix;

记得初始化Texture2D。

1
2
3
4
void  Start()
{
     _texure2d =  new  Texture2D(_width, _height);
}

下面我们用一下文本输入框作为二维码的信息来源,再添加一个按钮作为触发。发明一个MultiFormatWriter用于二维码写入用,用其encode方法生成一个ByteMatrix类。再用ByteMatrix的方法ToBitmap成生bitmap对象,最后得到bitmap的数组再赋值于texture2d。在这里我定义了一个ImageToByte提取bitmap的数组。
因为是UI事件,所以我们把代码写到OnGUI中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void  OnGUI()
{

     input = GUI.TextField( new  Rect(100, 50, 100, 40), input);
     if  (GUI.Button( new  Rect(100, 100, 100, 40),  "生成" ))
     {

             byteMatrix =  new  MultiFormatWriter().encode(input, BarcodeFormat.QR_CODE, _width, _height);
             _texure2d.LoadImage(ImageToByte(byteMatrix.ToBitmap()));
             _success =  true ;

     }

     //画图
     if  (_success) GUI.DrawTexture( new  Rect(100, 300, 300, 300), _texure2d);
}

 

1
2
3
4
5
static  byte [] ImageToByte(Image img)
{
     ImageConverter converter =  new  ImageConverter();
     return  ( byte [])converter.ConvertTo(img,  typeof ( byte []));
}

PS:

  1. zxing是提供了项目源码,所以我们要把整个项目下载下来,我是下载ZXing-2.1.zip。之后解压,然后编译它的csharp目录下的代码。
  2. 也许zxing中文会显示乱码,这时用“DEFAULT_BYTE_MODE_ENCODING ”查找整个项目。然后把 System.String DEFAULT_BYTE_MODE_ENCODING = “ISO-8859-1″的ISO-8859-1改为UTF-8。

最终的运行效果如下










GitHub 加速计划 / zx / zxing
32.53 K
9.31 K
下载
ZXing ("Zebra Crossing") barcode scanning library for Java, Android
最近提交(Master分支:24 天前 )
8944e607 1 个月前
6ea3726b Bumps `spring.version` from 6.1.5 to 6.1.8. Updates `org.springframework:spring-test` from 6.1.5 to 6.1.8 - [Release notes](https://github.com/spring-projects/spring-framework/releases) - [Commits](https://github.com/spring-projects/spring-framework/compare/v6.1.5...v6.1.8) Updates `org.springframework:spring-web` from 6.1.5 to 6.1.8 - [Release notes](https://github.com/spring-projects/spring-framework/releases) - [Commits](https://github.com/spring-projects/spring-framework/compare/v6.1.5...v6.1.8) --- updated-dependencies: - dependency-name: org.springframework:spring-test dependency-type: direct:development update-type: version-update:semver-patch - dependency-name: org.springframework:spring-web dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Sean Owen <srowen@gmail.com> 2 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐