Unity结合ZXing生成中间带图标的二维码并保存
来源:互联网 发布:大数据信息存储机制 编辑:程序博客网 时间:2024/06/03 08:47
1、环境
Win10
Unity3d 2017.1.0f3
ZXing.Net ZXing.Net.0.16.0.0
下载:http://zxingnet.codeplex.com/
2、效果截图
3、代码
using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using ZXing;using ZXing.Common;public class Demo : MonoBehaviour { public RawImage img1; public RawImage img2; public RawImage img3; public Texture2D icon; // Use this for initialization void Start () { GenerateQRImage1("Hello Wrold!", 256, 256); GenerateQRImage2("I Love You!", 256, 256); GenerateQRImage3("中间带图片的二维码图片", 256, 256, icon); } /// <summary> /// 生成2维码 方法一 /// 经测试:只能生成256x256的 /// </summary> /// <param name="content"></param> /// <param name="width"></param> /// <param name="height"></param> void GenerateQRImage1(string content, int width, int height) { // 编码成color32 EncodingOptions options = null; BarcodeWriter writer = new BarcodeWriter(); options = new EncodingOptions { Width = width, Height = height, Margin = 1, }; options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); writer.Format = BarcodeFormat.QR_CODE; writer.Options = options; Color32[] colors = writer.Write(content); // 转成texture2d Texture2D texture = new Texture2D(width, height); texture.SetPixels32(colors); texture.Apply(); img1.texture = texture; //// 存储成文件 //byte[] bytes = texture.EncodeToPNG(); //string path = System.IO.Path.Combine(Application.dataPath, "qr.png"); //System.IO.File.WriteAllBytes(path, bytes); } /// <summary> /// 生成2维码 方法二 /// 经测试:能生成任意尺寸的正方形 /// </summary> /// <param name="content"></param> /// <param name="width"></param> /// <param name="height"></param> void GenerateQRImage2(string content, int width, int height) { // 编码成color32 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.Add(EncodeHintType.MARGIN, 1); hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.M); BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 转成texture2d int w = bitMatrix.Width; int h = bitMatrix.Height; print(string.Format("w={0},h={1}", w, h)); Texture2D texture = new Texture2D(w, h); for (int x=0; x<h; x++) { for(int y=0; y<w; y++) { if(bitMatrix[x,y]) { texture.SetPixel(y, x, Color.red); } else { texture.SetPixel(y, x, Color.white); } } } texture.Apply(); img2.texture = texture; //// 存储成文件 //byte[] bytes = texture.EncodeToPNG(); //string path = System.IO.Path.Combine(Application.dataPath, "qr.png"); //System.IO.File.WriteAllBytes(path, bytes); } /// <summary> /// 生成2维码 方法三 /// 在方法二的基础上,添加小图标 /// </summary> /// <param name="content"></param> /// <param name="width"></param> /// <param name="height"></param> void GenerateQRImage3(string content, int width, int height, Texture2D centerIcon) { // 编码成color32 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.Add(EncodeHintType.MARGIN, 1); hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H); BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 转成texture2d int w = bitMatrix.Width; int h = bitMatrix.Height; Texture2D texture = new Texture2D(w, h); for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { if (bitMatrix[x, y]) { texture.SetPixel(y, x, Color.blue); } else { texture.SetPixel(y, x, Color.white); } } } // 添加小图 int halfWidth = texture.width / 2; int halfHeight = texture.height / 2; int halfWidthOfIcon = centerIcon.width / 2; int halfHeightOfIcon = centerIcon.height / 2; int centerOffsetX = 0; int centerOffsetY = 0; for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { centerOffsetX = x - halfWidth; centerOffsetY = y - halfHeight; if(Mathf.Abs(centerOffsetX) <= halfWidthOfIcon && Mathf.Abs(centerOffsetY) <= halfHeightOfIcon) { texture.SetPixel(x, y, centerIcon.GetPixel(centerOffsetX + halfWidthOfIcon, centerOffsetY + halfHeightOfIcon)); } } } texture.Apply(); img3.texture = texture; // 存储成文件 byte[] bytes = texture.EncodeToPNG(); string path = System.IO.Path.Combine(Application.dataPath, "qr.png"); System.IO.File.WriteAllBytes(path, bytes); }}
阅读全文
0 0
- Unity结合ZXing生成中间带图标的二维码并保存
- Unity结合ZXing生成二维码并保存
- Unity结合ZXing生成二维码并保存
- .NET使用ZXing.NET生成中间带图片的二维码
- Zxing生成二维码(可带图标)
- java zxing 生成带图片的二维码
- java zxing 生成带图片的二维码
- 使用Zxing生成带logo的二维码
- Java:生成带logo的二维码(ZXing)
- (转)java zxing 二维码工具类,中间带Logo 的
- java生成中间带图片的二维码。
- 生成中间带logo的二维码
- 使用google的zxing工具在jsp页面中生成二维码以及带logo图标的二维码
- android 使用zxing生成中间嵌套logo的二维码
- android 使用zxing生成中间嵌套logo的二维码
- 二维码及带logo二维码的生成和解析-Zxing
- ZXing生成二维码和带logo的二维码
- zxing 二维码、带logo二维码生成
- 苹果限制用户购买应用渠道,App Store或被判垄断
- 移动互联网2016年度盘点 群星闪耀腾讯应用宝“星APP之夜”
- 没用骁龙821及Type-C vivo Xplay6值得买吗?
- CPU统治者如何打GPU的这一战?
- 支付宝:敬业福被吐槽一年,今年把欠的“敬业福”还给大家
- Unity结合ZXing生成中间带图标的二维码并保存
- 2017春运大数据报告出炉 火车返乡人均耗费15.36小时
- 微信新规:赞赏限额由单日5万调整至10万
- 雷军:小米 MIX 超预期,要开始从线下突围
- 运营人必备的7大技能:数据分析能力是未来运营的分水岭
- 回顾iPhone诞生十年,小米要开始从线下突围|唯物周刊
- “BT下载”发明者的风雨之路:伟大的技术为何没能成就伟大的商业?
- 微信小程序开发科普,让你1小时内从入门到上手|硬创公开课
- App Store被博彩行业霸屏50个小时,苹果和刷榜大军的叕一次攻防战