Показать сообщение отдельно
Старый 04.05.2013, 23:03   #4
pie
ПроЭктировщик
 
Аватар для pie
 
Регистрация: 04.11.2011
Сообщений: 176
Написано 19 полезных сообщений
(для 64 пользователей)
Ответ: Чтение QR кода

Для чтения с веб камеры.
using UnityEngine;
using System.Collections;
using System.Threading;
using com.google.zxing.qrcode;

public class 
SecurityTest : MonoBehaviour {    
    
WebCamTexture camTexture;
    
Thread qrThread;

    
Color32[] c;
    
sbyte[] d;
    
int w, h, wxh;
    
int x, y, z;
    
bool isQuit;
    
    public 
void Start () {
        
camTexture = new WebCamTexture ();
        
OnEnable ();
        
        
qrThread = new Thread (DecodeQR);
        
qrThread.Start ();
    }
    
    public 
void OnEnable () {
        if (
camTexture != null) {
            
camTexture.Play ();
            
w = camTexture.width;
            
h = camTexture.height;
            
wxh = w * h;
        }
    }
    
    public 
void OnDisable () {
        if (
camTexture != null) {
            
camTexture.Pause ();
        }
    }
    
    public 
void OnDestroy () {
        
qrThread.Abort ();
        
camTexture.Stop ();
    }
    
    public 
void OnApplicationQuit () {
        
isQuit = true;
    }
    
    public 
void Update () {
        
c = camTexture.GetPixels32 ();
    }
    
    public 
void DecodeQR () {
        while(
true) {
            if(
isQuit) break;
            try {                
                
d = new sbyte[wxh];
                
z = 0;
                for(
y = h - 1; y >= 0; y--) {
                    for(
x = 0; x < w; x++) {
                        
d[z++] = (sbyte)(((int)c[y * w + x].r) << 16 | ((int)c[y * w + x].g) << 8 | ((int)c[y * w + x].b));
                    }
                }
               
Debug.Log (new QRCodeReader().decode(d, w, h).Text);
            }
            catch {
                continue;
            }
        }
    }
    
    public 
void OnGUI () {
        
GUI.DrawTexture(new Rect (0, 0, Screen.width, Screen.height), camTexture, ScaleMode.ScaleToFit);
    }
} 
(Offline)
 
Ответить с цитированием
Сообщение было полезно следующим пользователям:
Andvrok (06.05.2013)