Created
July 29, 2013 01:40
-
-
Save AngeloYazar/6101673 to your computer and use it in GitHub Desktop.
QRCode to Texture2D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using ZXing.Common; | |
public class QRCode : MonoBehaviour { | |
private ZXing.QrCode.QRCodeWriter QRWriter; | |
void Start () { | |
string url = "http://www.yazarmediagroup.com/ouya/index.html?" + Network.player.ipAddress.ToString(); | |
QRWriter = new ZXing.QrCode.QRCodeWriter(); | |
BitMatrix encoded = QRWriter.encode(url, ZXing.BarcodeFormat.QR_CODE, 512, 512); | |
Texture2D tex = new Texture2D(512,512,TextureFormat.RGBA32,false); | |
Color[] pixels = tex.GetPixels(); | |
int k = 0; | |
for(int j = 0; j < 512; j++ ) { | |
ZXing.Common.BitArray row = new ZXing.Common.BitArray(512); | |
row = encoded.getRow(j,null); | |
int[] intRow = row.Array; | |
for( int i = intRow.Length-1; i >= 0; i-- ) { | |
int thirtyTwoPixels = intRow[i]; | |
for(int b = 31; b >= 0; b--) { | |
int pixel = ( (thirtyTwoPixels >> b) & 1 ); | |
if( pixel == 0 ) { | |
pixels[k] = Color.white; | |
} | |
else { | |
pixels[k] = Color.black; | |
} | |
k++; | |
} | |
} | |
} | |
tex.SetPixels( pixels ); | |
tex.Apply(); | |
renderer.material.mainTexture = tex; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment