没有 Unity SDK 的 Cardboard QR 扫描 [英] Cardboard QR Scanning without Unity SDK

查看:67
本文介绍了没有 Unity SDK 的 Cardboard QR 扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究如何在无需使用 Unity API 的情况下扫描和使用 Cardboard 设备提供的二维码.我已经为带有 Obj-c 的 iOS 设备基于 SceneKit 的 VR 编写了 SCN-VR,我希望扫描 QR 码也能简化配置文件的设置.

I've been trying to figure out how to scan and use the QR codes provided with Cardboard devices without needing to use the Unity API. I've already written SCN-VR for SceneKit based VR for iOS devices with Obj-c and I would want scanning QR codes to also work to make setting up profiles simpler.

我看到了对 goo.gl/pdNRON 的 QR 码扫描,这会导致一个关于如何下载 Google Cardboard 应用程序的页面,但 Google Cardboard 应用程序要下载实际配置文件的 HTTP 服务是什么?

I've seen a QR code scan to goo.gl/pdNRON, which leads to a page on how to download the Google Cardboard app, but what HTTP service is the Google Cardboard app going to download the actual profile?

推荐答案

可以使用 Google 的协议缓冲区(https://developers.google.com/protocol-buffers/docs/cpptutorial?hl=en0).从代码扫描的缩短的 URL 被重定向到包含 p= 查询字段中的实际信息的 URL.例如,您的网址 (goo.gl/pdNRON) 会重定向到

The QR codes can be parsed with Google's protocol buffers (https://developers.google.com/protocol-buffers/docs/cpptutorial?hl=en0). The shortened URL scanned from the code is redirected to an URL that contains the actual info in the p= query field. For example, your URL (goo.gl/pdNRON) redirects to https://www.google.com/get/cardboard/download/?p=CgxNYXR0ZWwsIEluYy4SGFZJRVctTUFTVEVS4oSiIFZSIFZJRVdFUh0xCCw9JWiRbT0qEAAASEIAAEhCAABIQgAASEJYATUpXA89OggpXA8-SOE6P1AAYAM (which displays as the Cardboard download site in a browser). The string you want is the long one starting with CgxNYXR. The field is base64 encoded.

协议缓冲区定义文件可在 https://github.com/google/wwgc/blob/master/www/CardboardDevice.proto.一旦您构建了 Protocol Buffers 编译器(protoc,来自上面的教程链接),只需在 CardboardDevice.proto 上运行它,并在您的项目中包含输出 .cc 和 .h 文件.向 DeviceParams 类型发送解码数据后,您可以访问该信息.

The protocol buffer definition file is available at https://github.com/google/wwgc/blob/master/www/CardboardDevice.proto. Once you have the Protocol Buffers compiler built (protoc, from the tutorial link above), just run it on CardboardDevice.proto, and include the output .cc and .h files in your project. You can access the info through the DeviceParams type, after sending the decoded data to it.

用于构建 Protocol Buffers 库并将其包含在您的 iOS 项目中:https://gist.github.com/BennettSmith/9487468ae3375d0db0cc.或者,如果您在生成/链接库时遇到问题,请尝试以下替代方法:iOS 上的 Google 协议缓冲区

For building the Protocol Buffers library and including it in your iOS project: https://gist.github.com/BennettSmith/9487468ae3375d0db0cc. Or if you have problems generating/linking the libraries, try this as an alternative: Google protocol buffers on iOS

以下是一些帮助您入门的代码:

Here's some code to get you started:

// Retrieve HEAD only (don't want the whole page)
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", myURL]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                   timeoutInterval:10.0f];
[request setHTTPMethod:@"HEAD"];

// Start the request
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    if (connectionError) {
        NSLog(@"%@", [connectionError localizedDescription]);
    } else {
        // Find the p attribute
        NSArray *comps = [[[response URL] query] componentsSeparatedByString:@"&"];
        for (NSString *comp in comps) {
            NSArray *subComps = [comp componentsSeparatedByString:@"="];
            if ([subComps count] == 2 && [subComps[0] isEqualToString:@"p"]) {
                NSString *base64 = subComps[1];

                // Replace _ with /, - with +, and pad with = to multiple of 4
                base64 = [base64 stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
                base64 = [base64 stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
                base64 = [base64 stringByPaddingToLength:(([base64 length]+3)/4)*4 withString:@"=" startingAtIndex:0];

                // Decode from base 64
                NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64
                                                                          options:NSDataBase64DecodingIgnoreUnknownCharacters];

                // Get the device parameters
                DeviceParams deviceParams;
                deviceParams.ParseFromArray([decodedData bytes], (int)[decodedData length]);

                // Do stuff with deviceParams
                // eg deviceParams.inter_lens_distance()
                break;
            }
        }
    }
}];

这篇关于没有 Unity SDK 的 Cardboard QR 扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆