Generating a QR code with Unity and ZXing(zebra crossing)
Recently in a project of my internship, I need to use the unity to read a particular URL in a txt file, and generate a QR code with this URL. Reading a txt file and transfer its content into string seems easy, while I still encountered some problem. After browsed some tutorials, I know that I should save the file's path in a string, and use a StreamReader object to read the content in the file, and save them in a list of string. But found that if I use:
It would work in the unity editor undoubtedly, but if I build the file and run in windows OS, I found that I can not find that path anymore. Soon I found that I should build a StreamingAssets folder in the Assets Folder, and put the txt file in the new folder, use the Application.streamingAssetsPath + "file name" to access it. Each platform of builded unity exe program has unique file path, so that try to access a file with path "Assets/URL.txt" in a builded program is not possible. But if I put those file in StreamingAssets folder, then the path Application.streamingAssetsPath would be the same, whatever the platform of the builded program. It should be like that:
Then the QrCodeStr would save all the URLs that was included in the URL.txt file. So how to generate a QR code?At first I input the ZXing.unity.dll(you can download it at http://zxingnet.codeplex.com/) and save the file "ZXing.unity.dll" in the Assets folder, so that "using ZXing" is available in my scripts.
We can define a function following to transfer the URL of string format to a QR code which saved in Color32[] format:
And this function will return a Color32[], in order to show the QR code in unity, we should transfer it into Texture2D, and apply it to the RawImage component in our game scene. Here I fulfill those requirements in a function.
Once the player press button space, the function above will be called and generate a QR code and apply it to the RawImage component which named as "image" in the program, and the variable Number will plus 1. So if you have a few URLs in the txt file, you can change which QR code you want to show by press the button space several times.
The complete project file can be downloaded here: https://drive.google.com/open?id=1XHbfRrFkEePijWuB7wRKJ3_qvaxsVpgv
path = "Assets/URL.txt";
It would work in the unity editor undoubtedly, but if I build the file and run in windows OS, I found that I can not find that path anymore. Soon I found that I should build a StreamingAssets folder in the Assets Folder, and put the txt file in the new folder, use the Application.streamingAssetsPath + "file name" to access it. Each platform of builded unity exe program has unique file path, so that try to access a file with path "Assets/URL.txt" in a builded program is not possible. But if I put those file in StreamingAssets folder, then the path Application.streamingAssetsPath would be the same, whatever the platform of the builded program. It should be like that:
path = Application.streamingAssetsPath + "/URL.txt";
sr = File.OpenText(path);
while((str = sr.ReadLine()) != null)
{
QrCodeStr.Add(str);
}
sr.Close();
using ZXing;
using ZXing.QrCode;
private static Color32[] Encode(string textForEncoding, int width, int height)
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Height = height,
Width = width
}
};
return writer.Write(textForEncoding);
}
public void Btn_CreatQr(Texture2D pic)
{
if (QrCodeStr[Nmuber].Length > 1)
{
var color32 = Encode(QrCodeStr[Nmuber], pic.width, pic.height);
pic.SetPixels32(color32);
pic.Apply();
image.texture = pic;
}
}

评论
发表评论