我目前正在尝试用Unity (iOS版)开发一个允许用户扫描二维码的应用程序。我使用的是ZXing.NET库,它已经针对Unity进行了优化。
这是我正在使用的当前解码线程
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;
}
}
}执行到达“开始解码!”打印语句,但无法到达"Got decode!“语句。
这是因为Decode()方法每次都会生成未知的异常,即使相机正在查看非常清晰的二维码。
供参考:C是Color32[]类型,使用WebCamTexture.GetPixels32()生成W,H是表示摄影机纹理的宽度和高度的整数。
由于某些原因,我无法在catch子句中捕获泛型异常,这意味着我无法确定Decode()方法正在生成哪种类型的异常。
编辑:我使用的代码改编自ZXing.NET项目中的Unity demo。我使用的是当前版本的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!");
}正是在检索结果的文本值时,才会导致错误。我仍然不知道如何修复它。
有人能给我提个建议吗?
谢谢
发布于 2012-11-27 14:52:49
代码看起来像来自ZXing.Net项目的unity演示。我用当前版本0.10.0.0再次尝试了演示。它对我来说就像一种护身符。但我只能在windows上用unity来测试它。我没有机会和iOS一起尝试。
你有没有试过最新版本的ZXing.Net?您使用的unity版本是什么?在Update方法中是否正确设置了变量c?
发布于 2012-12-18 09:26:58
我已经解决了这个问题。问题是,从相机获得的纹理没有在正确的纵横比中,并被‘挤压’。因此,ZXing库无法正确识别代码。
纠正这个问题后,识别就完美无缺地工作了。
https://stackoverflow.com/questions/13576333
复制相似问题