Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alfredljm · Jun 04, 2019 at 08:20 PM · camerazxing

Local Camera to show QRcode with ZXing not turning on in Unity

I wrote a Script with ZXing to call the local Camera on my PC, I attach the script to my camera in unity and the camera showed blocking. Please Help

alt text

The script is shown:

 using System.Threading;
 
 using UnityEngine;
 
 using ZXing;
 using ZXing.QrCode;
 
 public class BarcodeCam : MonoBehaviour
 {
    // Texture for encoding test
    public Texture2D encoded;
 
    private WebCamTexture camTexture;
    private Thread qrThread;
 
    private Color32[] c;
    private int W, H;
       
    private Rect screenRect;
 
    private bool isQuit;
 
    public string LastResult;
    private bool shouldEncodeNow;
 
    void OnGUI()
    {
       GUI.DrawTexture(screenRect, camTexture, ScaleMode.ScaleToFit);
    }
 
    void OnEnable()
    {
       if (camTexture != null)
       {
          camTexture.Play();
          W = camTexture.width;
          H = camTexture.height;
       }
    }
 
    void OnDisable()
    {
       if (camTexture != null)
       {
          camTexture.Pause();
       }
    }
 
    void OnDestroy()
    {
       qrThread.Abort();
       camTexture.Stop();
    }
 
    // It's better to stop the thread by itself rather than abort it.
    void OnApplicationQuit()
    {
       isQuit = true;
    }
 
    void Start()
    {
       encoded = new Texture2D(256, 256);
       LastResult = "http://www.bing.com";
       shouldEncodeNow = true;
 
       screenRect = new Rect(0, 0, Screen.width, Screen.height);
 
       camTexture = new WebCamTexture();
       camTexture.requestedHeight = Screen.height; // 480;
       camTexture.requestedWidth = Screen.width; //640;
       OnEnable();
 
       qrThread = new Thread(DecodeQR);
       qrThread.Start();
    }
 
    void Update()
    {
       if (c == null)
       {
          c = camTexture.GetPixels32();
       }
 
       // encode the last found
       var textForEncoding = LastResult;
       if (shouldEncodeNow && 
           textForEncoding != null)
       {
          var color32 = Encode(textForEncoding, encoded.width, encoded.height);
          encoded.SetPixels32(color32);
          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
          {
             // decode the current frame
             var result = barcodeReader.Decode(c, 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);
             c = null;
          }
          catch
          {
          }
       }
    }
 
    private 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);
    }
 }

The Script need the Zxing.unity.dll file, it is attached with the link below, take it if you need to rebuild the situation: https://drive.google.com/drive/folders/1O3PbiMAdW_TLfDMadPQZWlbCVGgLL4oC?usp=sharing

capture.png (59.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

167 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is going on with Zxing Libary in portrait mode? 0 Answers

How to make camera position relative to a specific target. 1 Answer

Getting Vuforia to work with zxing 3 Answers

Rotate a sphere in the same direction the camera is pointing at 2 Answers

Camera Limit Rotations Problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges