我目前正在尝试在Unity(iOS平台)中制作一个应用程序,允许用户扫描QR码。
我正在使用为Unity优化的ZXing.NET库。
这是我正在使用的当前解码线程:
void DecodeQR()
{
// create a reader with a custom luminance source
var barcodeReader = new BarcodeReader {AutoRotate=false, TryHarder=false};
while (true)
{
if (isQuit)
break;
try
{
string result = "Cry if you see this.";
// decode the current frame
if (c != null){
print ("Start Decode!");
result = barcodeReader.Decode(c, W, H).Text; //This line of code is generating unknown exceptions for some arcane reason
print ("Got past decode!");
}
if (result != null)
{
LastResult = result;
print(result);
}
// Sleep a little bit and set the signal to get the next frame
c = null;
Thread.Sleep(200);
}
catch
{
continue;
}
}
}
执行到“Start Decode!”打印语句,但未能执行到“Got past decode!”语句。这是因为Decode()方法每次都会生成一个未知异常,即使摄像头正在查看非常清晰的QR码也是如此。
参考: c的类型为Color32[],使用WebCamTexture.GetPixels32()生成 W、H是表示相机纹理的宽度和高度的整数。
由于某种原因,在catch子句中无法捕获一般性异常,这意味着无法确定Decode()方法生成了什么样的异常。
编辑:我使用的代码改编自ZXing.NET项目提供的Unity演示。我正在使用最新版本的ZXing.NET。我还应该提到,我目前正在iMac上进行测试,而不是在iOS设备或模拟器上进行测试。我尝试了从头开始运行Unity演示,并获得了相同的结果。
这是如何更新变量c(Color32[])的内容:
void Update()
{
if (c == null)
{
c = camTexture.GetPixels32();
H = camTexture.height;
W = camTexture.width;
}
}
编辑2:我将解码阶段分为两个部分,首先生成结果对象,然后检索结果的文本属性,如下所示:
if (c != null){
print ("Start Decode!");
var initResult = barcodeReader.Decode(c, W, H);
print ("Got past decode!");
result = initResult.Text; //This line of code is generating unknown exceptions for some arcane reason
print ("Got past text conversion!");
}
问题出在获取结果的文本值时发生了错误。但我仍然不知道如何解决它。
请问有人能给我建议吗?
谢谢
print ("解码成功!"); if (initResult != null) result = initResult.Text; print ("文本转换成功!");
如果BarcodeReader无法解码图像中的条形码,则initResult为null。 - Michael