Unity 生成二维码,并将二维码和已有背景图片融合,保存到本地,,,

1、场景的搭建


2、代码的编写(参考链接)

using System.IO;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using ZXing.Common;public class CreateCode_2 : MonoBehaviour
{//生成二维码指定RawImagepublic RawImage Raw_QRCode; //要和二维码融合的背景图public RawImage Raw_Bg;//和融合后的结果图 (保存到本地的图)public RawImage Raw_Result;void Start(){//生成二维码Raw_QRCode.texture = GenerateQRImageConstantSize("https://blog.csdn.net/czhenya", 256, 256);//融合Raw_Result.texture = MixImagAndQRCode(TextureToTexture2D(Raw_Bg.texture), GenerateQRImageConstantSize("http://www.baidu.com", 256, 256));//保存知道本地 ... 在本地的路径 C:\Users\Administrator\AppData\LocalLow\DefaultCompany\DefaultCompany  //Application.persistentDataPath + "/ShotShare.png" 直接设置此路径为分享图片路径Debug.Log(saveMainTextureToPng((Application.persistentDataPath + "/ShotShare.png"), Raw_Result.texture));}/// <summary>/// 生成固定大小的二维码(256,256).../// </summary>/// <param name="content">要显示的内容</param>/// <param name="width">宽</param>/// <param name="height">高</param>/// <returns></returns>public static Texture2D GenerateQRImageConstantSize(string content, int width, int height){// 编码成color32EncodingOptions 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);// 转成texture2dTexture2D texture = new Texture2D(width, height);texture.SetPixels32(colors);texture.Apply();return texture;}/// <summary>/// 融合图片和二维码,得到新Texture2D .../// 根据背景图的大小比例/// </summary>/// <param name="tex_base">底图</param>/// <param name="tex_code">二维码</param>public static Texture2D MixImagAndQRCode(Texture2D tex_base, Texture2D tex_code){Texture2D newTexture = Instantiate(tex_base) as Texture2D; ;Vector2 uv = new Vector2((tex_base.width - tex_code.width) / tex_base.width, (tex_base.height - tex_code.height) / tex_base.height);for (int i = 0; i < tex_code.width; i++){for (int j = 0; j < tex_code.height; j++){float w = uv.x * tex_base.width - tex_code.width + i;float h = uv.y * tex_base.height - tex_code.height + j;//从底图图片中获取到(w,h)位置的像素Color baseColor = newTexture.GetPixel((int)w, (int)h);//从二维码图片中获取到(i,j)位置的像素Color codeColor = tex_code.GetPixel(i, j);//融合newTexture.SetPixel((int)w, (int)h, codeColor);}}newTexture.Apply();return newTexture;}/// <summary>/// Texture转换成Texture2D.../// </summary>/// <param name="texture"></param>/// <returns></returns>Texture2D TextureToTexture2D(Texture texture){Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);RenderTexture currentRT = RenderTexture.active;RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);Graphics.Blit(texture, renderTexture);RenderTexture.active = renderTexture;texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);texture2D.Apply();RenderTexture.active = currentRT;RenderTexture.ReleaseTemporary(renderTexture);return texture2D;}/// <summary>/// 将Texture转为本地PNG .../// </summary>/// <param name="filePath">保存路径,(分享时直接使用)</param>/// <param name="teture">要保存的Texture</param>/// <returns></returns>public static bool saveMainTextureToPng(string filePath, Texture teture){if (teture.GetType() != typeof(Texture2D)){return false;}Texture2D savedTexture = (Texture2D)teture;try{Texture2D newTexture = new Texture2D(savedTexture.width, savedTexture.height, TextureFormat.RGBA32, false);newTexture.SetPixels(0, 0, savedTexture.width, savedTexture.height, savedTexture.GetPixels());newTexture.Apply();byte[] bytes = newTexture.EncodeToPNG();if (bytes != null && bytes.Length > 0){if (File.Exists(filePath)){File.Delete(filePath);}System.IO.File.WriteAllBytes(filePath, bytes);}}catch (IOException ex){return false;}return true;}
}

Unity 生成二维码 融合图片相关推荐

  1. 简单的二维码生成接口,自动生成二维码,返回图片地址

    自动生成二维码,返回图片地址 本来不想写的,但是怕太久不写这个东西,就要荒废了,就先记录一下简单的东西 这里因为,返回地址的时候,通过了nginx ,我试了很多方法都抓取不到对应的IP地址, 就在ng ...

  2. unity生成二维码

    我这里用到GUI和UGUI来实现了一次,下载地址为http://zxingnet.codeplex.com/,我这里已经下载好了,引用一个zxing.unity.dll,下载网址https://pan ...

  3. vue-qr 自动生成二维码+logo图片

    vue-qr 可根据文本内容生成二维码,还可以添加logo图片.设置logo大小.设置二维码大小.留白边距.背景图片.背景颜色等等. 1.安装 npm install vue-qr --save 2. ...

  4. 前端生成二维码与图片合成

    首先前端生成二维码 使用插件完成,插件为DrawQRCode.unitypackage  合成二维码直接使用DrawQRCode 类里提供的方法即可 生成二维码的方法 DrawCode_Color32 ...

  5. java 通过Qrcode生成二维码添加图片logo和文字描述

    一个简单的javaweb项目 注释比较多直接上代码 附上使用的jar包Qrcode package com.fehorizon.erp.pda.utils;import java.awt.Color; ...

  6. Java解决生成二维码后图片蒙上红色背景问题

    一.在生成图片,或者将二维码放在图片上生成新图片后,生成的图片会造成红色蒙版问题,这问题是因为jdk中 ImageIO.read(inputStream);里在识别RGB的时候会出现偏差,目前公司用的 ...

  7. springboot2整合二维码 生成二维码图片及输出web端及打印

    step1 导包 <!-- 二维码 --><dependency><groupId>com.google.zxing</groupId><arti ...

  8. Android利用zxing生成二维码,识别二维码,中间填充图片超详细、超简易教程

    gayhub上的zxing可用于生成二维码,识别二维码 gayhub地址:https://github.com/zxing/zxing 此文只是简易教程,文末附有完整代码和demo下载地址,进入正题: ...

  9. Thinkphp5+ phpqrcode 在线批量生成二维码图片并保存在本地

    第一步: 首先需要先下载phpqrcode包,解压在项目中,我们只用到 phpqrcode.php 文件就可以; phpqrcode下载链接为: http://phpqrcode.sourceforg ...

最新文章

  1. 大数据与数据挖掘考试题_大数据时代下的数据挖掘试题及答案
  2. OpenCV使用模板与遮罩匹配的实例(附完整代码)
  3. Sql语句里的递归查询
  4. 使用 Solid 私有化存储 IPFS 文件哈希值
  5. C++11 右值引用、移动语义、完美转发、万能引用
  6. 使用注解装配Bean
  7. 04简单线性回归实战
  8. 图文结合,白话Go的垃圾回收原理
  9. 云计算仿真框架CloudSim介绍
  10. 存储引擎和Mysql服务层出现索引信息不一致错误提示
  11. java开发教程-excel文件的读写
  12. 课设2--线性表的操作
  13. parallelstudio license 位置_UAV位置测量仪器Apogee-D Dual GPS INS
  14. PolarDB-X 一致性共识协议 (X-Paxos)
  15. 深入探索WebSockets
  16. 学习周刊-2021年第19周
  17. 求两个数最小公倍数的7种方法
  18. Gnome3桌面美化
  19. MCE公司:重磅!明星靶点TLR4助力攻克脑血管难题!
  20. 翻译文章“AST 模块:用 Python 修改 Python 代码”---!!注意ironpathyon未实现此功能...

热门文章

  1. symbian os:描述符
  2. 杀猪盘、杀鸟盘、杀鱼盘...网络诈骗为何如此难以防范?
  3. 微信聊天记录能恢复吗?值得试手的2个方法推荐
  4. 天津股权交易所做市规则
  5. 主板升级nvme 教程
  6. WIN7+MySQL+ActivePerl+bugzilla+Apache安装配置
  7. nginx配置赛门铁克ssl
  8. Oracle10g Bug 4612267 补丁安装备忘录
  9. autoCaptcha谷歌浏览器扩展工具-调用api识别验证码图片
  10. HP小型机MP简易使用手册