add
This commit is contained in:
170
Assets/3rd/ZXing/BarcodeCam.cs
Normal file
170
Assets/3rd/ZXing/BarcodeCam.cs
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright 2012 ZXing.Net authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System.Threading;
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
using ZXing;
|
||||
using ZXing.QrCode;
|
||||
|
||||
public class BarcodeCam : MonoBehaviour
|
||||
{
|
||||
public BarcodeCam self;
|
||||
// Texture for encoding test
|
||||
public Texture2D encoded;
|
||||
|
||||
private WebCamTexture camTexture;
|
||||
private Thread qrThread;
|
||||
|
||||
private Color32[] color32;
|
||||
private int W, H;
|
||||
|
||||
private Rect screenRect;
|
||||
|
||||
private bool isQuit;
|
||||
|
||||
public string LastResult;
|
||||
private bool shouldEncodeNow;
|
||||
|
||||
public BarcodeCam()
|
||||
{
|
||||
self = this;
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
encoded = new Texture2D(256, 256);
|
||||
LastResult = "http://www.google.com";
|
||||
shouldEncodeNow = true;
|
||||
|
||||
screenRect = new Rect(Screen.width * 0.1f/2, -Screen.height * 0.2f/2, Screen.width*0.9f, Screen.height*0.8f);
|
||||
|
||||
camTexture = new WebCamTexture();
|
||||
camTexture.requestedHeight = Screen.height; // 480;
|
||||
camTexture.requestedWidth = Screen.width; //640;
|
||||
OnEnable();
|
||||
|
||||
qrThread = new Thread(DecodeQR);
|
||||
qrThread.Start();
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
GUI.DrawTexture(screenRect, camTexture, ScaleMode.ScaleToFit);
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (camTexture != null)
|
||||
{
|
||||
camTexture.Play();
|
||||
W = camTexture.width;
|
||||
H = camTexture.height;
|
||||
|
||||
if (color32 == null)
|
||||
{
|
||||
color32 = camTexture.GetPixels32();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
if (camTexture != null)
|
||||
{
|
||||
camTexture.Pause();
|
||||
}
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if(qrThread != null)
|
||||
qrThread.Abort();
|
||||
if(camTexture != null)
|
||||
camTexture.Stop();
|
||||
}
|
||||
|
||||
// It's better to stop the thread by itself rather than abort it.
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
isQuit = true;
|
||||
}
|
||||
|
||||
|
||||
//void Update()
|
||||
//{
|
||||
// encode the last found
|
||||
// var textForEncoding = LastResult;
|
||||
// if (shouldEncodeNow &&
|
||||
// textForEncoding != null)
|
||||
// {
|
||||
// var c = Encode(textForEncoding, encoded.width, encoded.height);
|
||||
// encoded.SetPixels32(c);
|
||||
// encoded.Apply();
|
||||
// shouldEncodeNow = false;
|
||||
// }
|
||||
//}
|
||||
|
||||
void DecodeQR()
|
||||
{
|
||||
// create a reader with a custom luminance source
|
||||
var barcodeReader = new BarcodeReader { AutoRotate = false, TryHarder = false };
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (isQuit)
|
||||
break;
|
||||
|
||||
try
|
||||
{
|
||||
if (color32 != null)
|
||||
{
|
||||
// decode the current frame
|
||||
var result = barcodeReader.Decode(color32, W, H);
|
||||
if (result != null)
|
||||
{
|
||||
LastResult = result.Text;
|
||||
shouldEncodeNow = true;
|
||||
print(result.Text);
|
||||
}
|
||||
}
|
||||
|
||||
// Sleep a little bit and set the signal to get the next frame
|
||||
Thread.Sleep(200);
|
||||
color32 = null;
|
||||
}
|
||||
catch(System.Exception e)
|
||||
{
|
||||
Debug.LogError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public 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);
|
||||
}
|
||||
}
|
||||
11
Assets/3rd/ZXing/BarcodeCam.cs.meta
Normal file
11
Assets/3rd/ZXing/BarcodeCam.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 569dd64ca2703457896c6eaefe0b7429
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/3rd/ZXing/libs.meta
Normal file
8
Assets/3rd/ZXing/libs.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d8d1b4187217483cb71eae92cf3f785
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
BIN
Assets/3rd/ZXing/libs/zxing.unity.dll
Executable file
BIN
Assets/3rd/ZXing/libs/zxing.unity.dll
Executable file
Binary file not shown.
33
Assets/3rd/ZXing/libs/zxing.unity.dll.meta
Normal file
33
Assets/3rd/ZXing/libs/zxing.unity.dll.meta
Normal file
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 68cdd7461a91a4a2c939846fed87997d
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 1
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
Windows Store Apps: WindowsStoreApps
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user