Reputation: 125
I'm trying to generate a QR Code using ZXing.Net and at first I have the problem where the .Save()
is not working because of an error CS1061. So, I scratched that idea then I tried to save .Write()
as image then render it in unity but Unity returns an error:
Cannot implicitly convert type 'UnityEngine.Color32[]' to 'UnityEngine.Sprite'
I tried using the answer from here where they used Sprite.Create()
as a solution but converted a Texture2D instead of a Color32[ ] but I wasn't able to confirm if the code worked for me since the code returns an error that:
The type or namespace name 'Image' could not be found
As I said, I wasn't able to find out if the code really worked or not. I don't know what caused the namespace
error since the script I'm using is under the Image UI.
These are the codes I'm using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ZXing;
using ZXing.QrCode;
using System.Drawing;
public class SampleScript : MonoBehaviour
{
public Texture2D myTexture;
Sprite mySprite;
Image myImage;
void Main()
{
var qrWriter = new BarcodeWriter();
qrWriter.Format = BarcodeFormat.QR_CODE;
this.gameObject.GetComponent<SpriteRenderer>().sprite = qrWriter.Write("text");
}
public void FooBar()
{
mySprite = Sprite.Create(myTexture, new Rect(0.0f, 0.0f, myTexture.width, myTexture.height), new Vector2(0.5f, 0.5f), 100.0f);
myImage.sprite = mySprite;
}
void Start()
{
FooBar();
Main();
}
I still haven't tested this code since the errors must first be resolved before running.
Upvotes: -2
Views: 1975
Reputation: 1
using UnityEngine;
public class ExampleScript : MonoBehaviour
{
WebCamTexture webcamTexture;
Color32[] data;
public UIScreen screen;
int HEIGHT, WIDTH;
void Start()
{
// Start web cam feed
webcamTexture = new WebCamTexture();
webcamTexture.Play();
HEIGHT = webcamTexture.height;
WIDTH = webcamTexture.width;
data = new Color32[WIDTH * HEIGHT];
}
void Update()
{
if (webcamTexture.didUpdateThisFrame)
{
webcamTexture.GetPixels32(data);
var texture = new Texture2D(WIDTH, HEIGHT);
texture.SetPixels32(data);
texture.Apply();
screen.imageUI.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f, 100);
}
}
}
incase if anyone needs the entire code to convert and show webcam to imageUI/sprite
screen.imageUI is just the Image ui element in canvas
Upvotes: 0
Reputation: 90679
The first
The type or namespace name 'Image' could not be found
is fixed by adding the according namespace
using UnityEngine.UI;
at the top of your file.
The exception
Cannot implicitly convert type 'UnityEngine.Color32[]' to 'UnityEngine.Sprite'
Can't simply be "fixed". It is as the exception tells you: You can't implicitly convert between those types .. not even explicitly.
qrWriter.Write("text");
returns the Color32[]
.
What you can try to do is creating a Texture using this color information BUT you will allways have to know the pixel dimensions of the target texture.
Then you can use Texture2D.SetPixels32
like
var texture = new Texture2D(HIGHT, WIDTH);
texture.SetPixels32(qrWriter.Write("text"));
texture.Apply();
this.gameObject.GetComponent<SpriteRenderer>().sprite = Sprite.Create(texture, new Rect(0,0, texture.width, texture.height), Vector2.one * 0.5f, 100);
Possible that you also will actively have to pass in the EncodingOptions
in order to set the desired pixel dimensions as shown in this blog:
using ZXing.Common; ... BarcodeWriter qrWriter = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = new EncodingOptions { Height = height, Width = width } }; Color32[] pixels = qrWriter.Write("text"); Texture2D texture = new Texture2D(width, height); texture.SetPixels32(pixels); texture.Apply();
there you can also find some more useful information about threading and scaling of the texture etc.
Upvotes: 1