unity打印生成之后的条形码(二维码也行)
QR CodeBarcode Scanner and Generator Cross PlatformPro 4.3.unitypackage + LCPrinter.unitypackage (Unity二维码条形码生成工具+unity打印机)
稍微修改了一下里面的脚本改为CodeController。
/// <summary>
/// write by 52cwalk,if you have some question ,please contract lycwalk@gmail.com
/// </summary>
/// using UnityEngine;
using System.Collections;
using System.Collections.Generic;using ZXing;
using ZXing.QrCode;
using ZXing.QrCode.Internal;
using ZXing.Common;public class CodeController : MonoBehaviour {public enum CodeMode{QR_CODE,EAN_13,EAN_8,CODE_128,//DATA_MATRIX,NONE}private Texture2D m_EncodedTex;public int e_QRCodeWidth = 400;public int e_QRCodeHeight = 400;public delegate void QREncodeFinished(Texture2D tex); public event QREncodeFinished onQREncodeFinished; BitMatrix byteMatrix;public CodeMode eCodeFormat = CodeMode.QR_CODE;public Texture2D e_QRLogoTex;Texture2D tempLogoTex = null;public float e_EmbedLogoRatio = 0.2f;void Start (){int targetWidth = Mathf.Min(e_QRCodeWidth,e_QRCodeHeight);targetWidth = Mathf.Clamp (targetWidth, 128, 1024);e_QRCodeWidth = e_QRCodeHeight = targetWidth;}void Update (){}/// <summary>/// Encode the specified string ./// </summary>/// <param name="valueStr"> content string.</param>public int Encode(string valueStr){// var writer = new QRCodeWriter();var writer = new MultiFormatWriter();Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>(); //set the code typehints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);switch (eCodeFormat) {case CodeMode.QR_CODE:byteMatrix = writer.encode( valueStr, BarcodeFormat.QR_CODE, e_QRCodeWidth, e_QRCodeHeight,hints); break;case CodeMode.EAN_13:if ((valueStr.Length == 12 || valueStr.Length == 13) && bAllDigit(valueStr)) {if (valueStr.Length == 13) {valueStr = valueStr.Substring (0, 12);}byteMatrix = writer.encode( valueStr, BarcodeFormat.EAN_13, e_QRCodeWidth, e_QRCodeWidth/2,hints); } else {return -13;}break;case CodeMode.EAN_8:if ((valueStr.Length == 7 || valueStr.Length == 8) && bAllDigit(valueStr)) {if (valueStr.Length == 8) {valueStr = valueStr.Substring (0, 7);}byteMatrix = writer.encode( valueStr, BarcodeFormat.EAN_8, e_QRCodeWidth, e_QRCodeWidth/2,hints); } else {return -8;}break;case CodeMode.CODE_128:if (valueStr.Length <= 80) {byteMatrix = writer.encode( valueStr, BarcodeFormat.CODE_128, e_QRCodeWidth, e_QRCodeWidth/2,hints); } else {return -128;}break;/*case CodeMode.DATA_MATRIX:byteMatrix = writer.encode( valueStr, BarcodeFormat.DATA_MATRIX, e_QRCodeWidth, e_QRCodeHeight,hints); break;*/case CodeMode.NONE:return -1;break;}if (m_EncodedTex != null) {Destroy (m_EncodedTex);m_EncodedTex = null;}m_EncodedTex = new Texture2D(byteMatrix.Width, byteMatrix.Height);for (int i =0; i!= m_EncodedTex.width; i++) {for(int j = 0;j!= m_EncodedTex.height;j++){if(byteMatrix[i,j]){m_EncodedTex.SetPixel(i,j,Color.black);}else{m_EncodedTex.SetPixel(i,j,Color.white);}}}///rotation the image Color32[] pixels = m_EncodedTex.GetPixels32();//pixels = RotateMatrixByClockwise(pixels, m_EncodedTex.width);m_EncodedTex.SetPixels32(pixels); m_EncodedTex.Apply ();if (eCodeFormat == CodeMode.QR_CODE) {AddLogoToQRCode ();}onQREncodeFinished (m_EncodedTex);return 0;}/// <summary>/// Encode the specified string ./// </summary>/// <param name="valueStr"> content string.</param>public Texture2D EncodeT2D(string valueStr){// var writer = new QRCodeWriter();var writer = new MultiFormatWriter();Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();//set the code typehints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);switch (eCodeFormat){case CodeMode.QR_CODE:byteMatrix = writer.encode(valueStr, BarcodeFormat.QR_CODE, e_QRCodeWidth, e_QRCodeHeight, hints);break;case CodeMode.EAN_13:if ((valueStr.Length == 12 || valueStr.Length == 13) && bAllDigit(valueStr)){if (valueStr.Length == 13){valueStr = valueStr.Substring(0, 12);}byteMatrix = writer.encode(valueStr, BarcodeFormat.EAN_13, e_QRCodeWidth, e_QRCodeWidth / 2, hints);}else{//return -13;}break;case CodeMode.EAN_8:if ((valueStr.Length == 7 || valueStr.Length == 8) && bAllDigit(valueStr)){if (valueStr.Length == 8){valueStr = valueStr.Substring(0, 7);}byteMatrix = writer.encode(valueStr, BarcodeFormat.EAN_8, e_QRCodeWidth, e_QRCodeWidth / 2, hints);}else{//return -8;}break;case CodeMode.CODE_128:if (valueStr.Length <= 80){byteMatrix = writer.encode(valueStr, BarcodeFormat.CODE_128, e_QRCodeWidth, e_QRCodeWidth / 2, hints);}else{//return -128;}break;/*case CodeMode.DATA_MATRIX:byteMatrix = writer.encode( valueStr, BarcodeFormat.DATA_MATRIX, e_QRCodeWidth, e_QRCodeHeight,hints); break;*/case CodeMode.NONE://return -1;break;}if (m_EncodedTex != null){Destroy(m_EncodedTex);m_EncodedTex = null;}m_EncodedTex = new Texture2D(byteMatrix.Width, byteMatrix.Height);for (int i = 0; i != m_EncodedTex.width; i++){for (int j = 0; j != m_EncodedTex.height; j++){if (byteMatrix[i, j]){m_EncodedTex.SetPixel(i, j, Color.black);}else{m_EncodedTex.SetPixel(i, j, Color.white);}}}///rotation the image Color32[] pixels = m_EncodedTex.GetPixels32();//pixels = RotateMatrixByClockwise(pixels, m_EncodedTex.width);m_EncodedTex.SetPixels32(pixels);m_EncodedTex.Apply();if (eCodeFormat == CodeMode.QR_CODE){AddLogoToQRCode();}//onQREncodeFinished(m_EncodedTex);//return 0;return m_EncodedTex;}/// <summary>/// Rotates the matrix.Clockwise/// </summary>/// <returns>The matrix.</returns>/// <param name="matrix">Matrix.</param>/// <param name="n">N.</param>static Color32[] RotateMatrixByClockwise(Color32[] matrix, int n) {Color32[] ret = new Color32[n * n];for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {ret[i*n + j] = matrix[(n - i - 1) * n + j];}}return ret;}/// <summary>/// anticlockwise/// </summary>/// <returns>The matrix.</returns>/// <param name="matrix">Matrix.</param>/// <param name="n">N.</param>static Color32[] RotateMatrixByAnticlockwise(Color32[] matrix, int n) {Color32[] ret = new Color32[n * n];for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {ret[i*n + j] = matrix[(n - j - 1) * n + i];}}return ret;}bool isContainDigit(string str){for (int i = 0; i != str.Length; i++) {if (str [i] >= '0' && str [i] <= '9') {return true;}}return false;}bool isContainChar(string str){for (int i = 0; i != str.Length; i++) {if (str [i] >= 'a' && str [i] <= 'z') {return true;}}return false;}bool bAllDigit(string str){for (int i = 0; i != str.Length; i++) {if (str [i] >= '0' && str [i] <= '9') {} else {return false;}}return true;}public void AddLogoToQRCode(){if (e_QRLogoTex != null) {int maxLength = Mathf.Max (e_QRLogoTex.width, e_QRLogoTex.height);if (maxLength > (m_EncodedTex.width * e_EmbedLogoRatio)) {if (tempLogoTex == null) {tempLogoTex = new Texture2D (e_QRLogoTex.width, e_QRLogoTex.height, TextureFormat.RGBA32, true);tempLogoTex.SetPixels (e_QRLogoTex.GetPixels ());tempLogoTex.Apply ();}float scaleRatio = m_EncodedTex.width * e_EmbedLogoRatio / maxLength * 1.0f;int newLogoWidth = (int)(e_QRLogoTex.width * scaleRatio);int newLogoHeight = (int)(e_QRLogoTex.height * scaleRatio);TextureScale.Bilinear (tempLogoTex, newLogoWidth, newLogoHeight);} else {if (tempLogoTex == null) {tempLogoTex = new Texture2D (e_QRLogoTex.width, e_QRLogoTex.height, TextureFormat.RGBA32,true);tempLogoTex.SetPixels (e_QRLogoTex.GetPixels());tempLogoTex.Apply ();}}}else{return;}int startX = (m_EncodedTex.width - tempLogoTex.width)/2;int startY = (m_EncodedTex.height - tempLogoTex.height)/2;for (int x = startX; x < tempLogoTex.width + startX; x++) {for (int y = startY; y < tempLogoTex.height + startY; y++) {Color bgColor = m_EncodedTex.GetPixel (x, y);Color wmColor = tempLogoTex.GetPixel (x - startX, y - startY);Color finalColor = Color.Lerp (bgColor, wmColor, wmColor.a / 1.0f);m_EncodedTex.SetPixel (x, y, finalColor);}}Destroy (tempLogoTex);tempLogoTex = null;m_EncodedTex.Apply ();}
}
再次修改一下LCPrinterScript
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System;
using System.IO;
using LCPrinter;
using UnityEngine.UI;public class LCPrinterScript : MonoBehaviour {public StorageScript storageScript;public Texture2D texture2D;public string printerName = "";public int copies = 1;public InputField inputField;public void printSmileButton(){if (storageScript.IsSingleNumber()){storageScript.Status_Color_Text(Color.blue, "入库条码打印成功");Print.PrintTexture(storageScript.PrintQrCreat().EncodeToPNG(), copies, printerName);//Print.PrintTexture(texture2D.EncodeToPNG(), copies, printerName);return;}storageScript.Status_Color_Text(Color.red, "没有有效的打印内容");}public void printByPathButton(){Print.PrintTextureByPath("D:\\pic.png", copies, printerName);//Print.PrintTextureByPath(inputField.text.Trim(), copies, printerName);}
}
调用脚本
/// <summary>/// 打印条码/// </summary>public Texture2D PrintQrCreat(){return e_qrController.EncodeT2D(InputField_Text[1].text);}
就可以使用了。
unity打印生成之后的条形码(二维码也行)相关推荐
- 个人用户永久免费,可自动升级版Excel插件,使用VSTO开发,Excel催化剂功能第12波-快速生成、读取、导出条形码二维码...
根据指定的内容生成对应的条形码或二维码,在如今移动互联网时代,并不是一件什么新鲜事,随便百度一下,都能找到好多的软件或在线网站可以帮我们做到,但细想一下,如果很偶然地只是生成一个两这样的图形,百度一下 ...
- 【C#】最全单据打印(打印模板、条形码二维码、字体样式、项目源码)
系列文章 [C#]编号生成器(定义单号规则.固定字符.流水号.业务单号) 本文链接:https://blog.csdn.net/youcheng_ge/article/details/12912978 ...
- JS生成条形码/二维码 barcode.js、JsBarcode
JS生成条形码/二维码 barcode.JsBarcode JsBarcode Barcode.js 以下代码均非纯原创.新手小白.网上一搜一大把的概念也不写了,直接上可运行的代码及遇到的小坑. Js ...
- 条形码/二维码生成探索
条形码/二维码生成探索 所用依赖 <!--条形码生成依赖(轻量型,推荐使用这个)(生成条码的同时会把信息生成到条形码下)--><dependency><groupId&g ...
- python——生成带logo的二维码图片并且保存、控制打印机打印图片二维码、整合打印(获取输入框的值)、打包成exe文件
1.生成带logo的二维码图片并且保存 前提条件:在D盘里有logo.png的图片,生成的二维码图片在D盘里的111.png import qrcode from PIL import Image# ...
- 条码条形码二维码检错系统供应
条码条形码二维码检错系统可防止因人为误操作.设备损坏等原因导致的错误,减少产品的退货返工,避免客户的投诉,提高工作效率.减少人力成本.适用于各种需要对条码进行检查的场合,如标签打印检查.装箱前对箱型 ...
- Python MyQR 生成不一样的二维码
Python MyQR 生成不一样的二维码 MyQR 最近,闲暇时学习了一点Python的东西,还是比较有趣,想和大家分享一下.顺便说一句,Python 真的很好玩 [手动滑稽] MyQR 二维码又称 ...
- asp生成带参数的二维码并合成推广海报图片,asp合并合成推广海报图片asp代码
最近做的一个项目中,客户要求用asp生成二维码,然后合并到一张背景图片上,合并生成一张推广海报来,可把我愁坏了,经过一个晚上的努力,成功了,下面把这个:asp生成带参数的二维码并合成推广海报,asp合 ...
- 生成带有logo的二维码
效果 准备: <dependency><groupId>com.google.zxing</groupId><artifactId>javase< ...
- 条码打印软件如何制作渐变色二维码
随着微信二维码.付款二维码的逐渐普及,目前各种大小单位的宣传也开始使用起了二维码,而且很多门牌上也使用了二维码,我们平时所见到的二维码也是各种各样,有各种颜色或者图案的.本站的其他教程中有简单介绍过如 ...
最新文章
- c语言程序设计自评报告,石家庄学院c语言程序设计自评报告.docx
- 基于协同过滤算法实现选课推荐系统
- js url参数的获取和设置以及删除
- 在2011年QCon北京大会上的主题分享内容——Keynote
- 20189211 《网络攻防》第五周作业
- 在Teams中对网站的URL特殊解析
- Django之管理权限
- 协议簇:ICMP 解析
- DISPLAY变量和xhost(原创)
- 从Myeclipe转向Idea,各种遇坑与填坑经验,持续更新(图文)
- Java中队列的使用
- Tensorflow API + OpenCV (Real Time Object Detection)
- 20190901 On Java8 第十五章 异常
- Mybaits 3.2.6设计的一个缺陷,欢迎拍砖交流
- 竞价推广的流程有哪些?
- JavaScript 基础知识个人总结
- c语言中平方根怎么用算法表示,C语言中平方根实现的详细分析
- LED背光源的使用寿命多久?
- matlab二维插值绘制地貌图
- TCP协议之《MTU探测功能》