I am developing a QR code decoder for my project.
I took the reference from Unity Zxing QR code scanner integration
I am using unity 2018+ and Vuforia 7+
Can Anyone help me where I have made a mistake?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
using ZXing;
using System;
[AddComponentMenu("System/QRScanner")]
public class QRScanner : MonoBehaviour
{
private bool cameraInitialized;
private BarcodeReader barCodeReader;
void Start()
{
barCodeReader = new BarcodeReader();
StartCoroutine(InitializeCamera());
}
private IEnumerator InitializeCamera()
{
// Waiting a little seem to avoid the Vuforia's crashes.
yield return new WaitForSeconds(1.25f);
var isFrameFormatSet = CameraDevice.Instance.SetFrameFormat(Image.PIXEL_FORMAT.GRAYSCALE, true);
Debug.Log(String.Format("FormatSet : {0}", isFrameFormatSet));
// Force autofocus.
var isAutoFocus = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
if (!isAutoFocus)
{
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
Debug.Log(String.Format("AutoFocus : {0}", isAutoFocus));
cameraInitialized = true;
}
private void Update()
{
if (cameraInitialized)
{
try
{
var cameraFeed = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.GRAYSCALE);
if (cameraFeed == null)
{
return;
}
var data = barCodeReader.Decode(cameraFeed.Pixels, cameraFeed.BufferWidth, cameraFeed.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);
if (data != null)
{
// QRCode detected.
Debug.Log("Detected");
Debug.Log(data.Text);
}
else
{
Debug.Log("No QR code detected !");
}
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}
}
}
Result right now: "QR code not detected".
Target which I am using working on other qr code reader app
Related
I'm trying to use the FMETP asset implementing the pun script following the information from the web page and the process that I'm doing in unity editor is object with FMETP Encoder assignig the FMStreamPun method to send the bit -> New Object with FMStreamPun script signing the FMETP Encoder.
Doing that the decoder doesn't receive the information from the encoder.
Do you know something about that topic?
This is the FMStreamPun Script
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FMETP;
public class FMStreamPun : Photon.Pun.MonoBehaviourPun, IPunObservable
{
private Queue<byte[]> appendQueueSendData = new Queue<byte[]>();
public int appendQueueSendDataCount { get { return appendQueueSendData.Count; } }
public UnityEventByteArray OnDataByteReadyEvent = new UnityEventByteArray();
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
Debug.Log("entro al seriaize view");
if (stream.IsWriting)
{
Debug.Log("is writting");
//Send the meta data of the byte[] queue length
stream.SendNext(appendQueueSendDataCount);
//Sending the queued byte[]
while (appendQueueSendDataCount > 0)
{
byte[] sentData = appendQueueSendData.Dequeue();
stream.SendNext(sentData);
}
}
if (stream.IsReading)
{
if (!photonView.IsMine)
{
//Get the queue length
int streamCount = (int)stream.ReceiveNext();
for (int i = 0; i < streamCount; i++)
{
//reading stream one by one
byte[] receivedData = (byte[])stream.ReceiveNext();
OnDataByteReadyEvent.Invoke(receivedData);
}
}
}
}
public void Action_SendData(byte[] inputData)
{
Debug.Log("entro al action send data");
//inputData(byte[]) is the encoded byte[] from your encoder
//doesn't require any stream, when there is only one player in the room
//if (PhotonNetwork.CurrentRoom.PlayerCount > 1)
//{
appendQueueSendData.Enqueue(inputData);
//}
}
}
I'm using both assets but I can't find any way to receive the information ito de decoder
I'm trying to fetch a collection with Mongo Atlas Data API. In API logs, Status seems as "OK" but i'm getting "Bad Request" error in Unity editor. Am I missing something? Thanks for helps.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System;
using System.Text;
public class Queries : MonoBehaviour
{
public static void DumpToConsole(object obj)
{
var output = JsonUtility.ToJson(obj);
Debug.Log(output);
}
void OnMouseDown()
{
StartCoroutine(GetDB());
IEnumerator GetDB() {
WWWForm form = new WWWForm();
form.AddField("dataSource","Cluster0");
form.AddField("database","Herbarium");
form.AddField("collection","Herbs-Specs");
/*byte [] bodyRaw = form.data;
string formInput = Convert.ToBase64String(bodyRaw);*/
UnityWebRequest www = UnityWebRequest.Post("https://data.mongodb-api.com/**********/data/v1/action/find", form);
www.SetRequestHeader("Content-Type", "application/json");
www.SetRequestHeader("Access-Control-Request-Headers", "*");
www.SetRequestHeader("api-key", "***********");
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
DumpToConsole(www.downloadHandler.text);
}
}
}
}
What I am trying to do is take a texture(any texture) from unity and use a webhook from discord to send it to discord.
This is what I have so far keep in mind I started coding not long ago so I'm not very familiar with this;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class SendMessage : MonoBehaviour
{
public string Message, Subject;
public string webhook;
public Texture2D screenshot;
// Start is called before the first frame update
void Start()
{
}
IEnumerator SendWebHook(string link, string message, System.Action<bool> action)
{
WWWForm form = new WWWForm();
form.AddField("content", message + screenshot.EncodeToPNG());
using (UnityWebRequest www = UnityWebRequest.Post(link, form))
{
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
action(false);
}
else
{
action(true);
}
}
}
public void send()
{
StartCoroutine(SendWebHook(webhook, Subject + Message, (success) =>
{
if(success)
{
Debug.Log("good");
}
}));
}
}
This is the code I am using for QR Scan. It uses XR for Camera input and ZXing to decode the QR Code. I have been at this for 3 days and still have no clue what I am doing wrong. I do not know whether it is that code isn't able to decode the QR, or it can but isn't able to print it out on Output text. Any help would be greatly appreciated. Thank you! :)
These are the libraries I am using :
using System.Collections;
using UnityEngine;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.XR.ARFoundation;
using ZXing;
using TMPro;
This is the code :
IBarcodeReader _reader;
ARCameraManager _arCam;
AudioSource _audioSrc;
ARRaycastManager _arRay;
private Texture2D _arTexture;
private TextMeshPro _outputText;
private void Awake()
{
_arCam = FindObjectOfType<ARCameraManager>();
_audioSrc = FindObjectOfType<AudioSource>();
_arRay = FindObjectOfType<ARRaycastManager>();
_reader = new BarcodeReader();
_arCam.frameReceived += OnCameraFrameReceived; //When camera receives frame.
}
//Fires off when Frame is received.
void OnCameraFrameReceived(ARCameraFrameEventArgs eventArgs)
{
if ((Time.frameCount % 15) == 0)
{
XRCpuImage _image;
if(_arCam.TryAcquireLatestCpuImage(out _image))
{
StartCoroutine(ProcessQRCode(_image));
_image.Dispose();
}
}
}
//Processes the QR Code in the _image.
IEnumerator ProcessQRCode(XRCpuImage _image)
{
var request = _image.ConvertAsync(new XRCpuImage.ConversionParams { inputRect = new RectInt(0, 0, _image.width, _image.height),
outputDimensions = new Vector2Int(_image.width / 2, _image.height / 2),
outputFormat = TextureFormat.RGB24
});
while (!request.status.IsDone())
yield return null;
if (request.status != XRCpuImage.AsyncConversionStatus.Ready)
{
Debug.LogErrorFormat("Request failed with status {0}", request.status);
request.Dispose();
yield break;
}
var rawData = request.GetData<byte>();
if(_arTexture == null)
{
_arTexture = new Texture2D(
request.conversionParams.outputDimensions.x,
request.conversionParams.outputDimensions.y,
request.conversionParams.outputFormat,
false
);
}
_arTexture.LoadRawTextureData(rawData);
_arTexture.Apply();
byte[] barcodeBitmap = _arTexture.GetRawTextureData();
LuminanceSource source = new RGBLuminanceSource(barcodeBitmap, _arTexture.width, _arTexture.height);
var result = _reader.Decode(source);
if(result != null)
{
var QRContents = result.Text;
_outputText.text = QRContents;
_audioSrc.Play();
}
}
I have a piece of code that I want to use with Unity 2018.2.14.f1.
#if UNITY_WSA && !UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.SerialCommunication;
namespace SerialForUWP
{
public class SerialComHelper
{
public async static Task<string[]> GetPorts()
{
var serials = SerialDevice.GetDeviceSelector();
var coms = await DeviceInformation.FindAllAsync(serials);
List<string> results = new List<string>();
foreach (var device in coms)
{
using (var serialDevice = await SerialDevice.FromIdAsync(device.Id))
{
if (serialDevice != null)
{
var port = serialDevice.PortName;
results.Add(port.ToString());
}
}
}
return results.ToArray();
}
}
}
#endif
I compile for UWP using IL2CPP :
I also already updated the manifest of the cpp projet :
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
As soon as I try to use the GetPorts() function my app freeze.
void Update()
{
text.text = Time.time.ToString();
if (Input.GetKeyUp(KeyCode.Space))
{
try
{
#if UNITY_WSA && !UNITY_EDITOR
foreach (var com in SerialForUWP.SerialComHelper.GetPorts().Result)
{
text.text = com;
}
#endif
}
catch (Exception ex)
{
text.text = ex.ToString();
}
}
}
I got no error, no exception.
Can anyone help please ?
To get the exceptions use:
Debug.Log(ex.ToString()) instead of Text.text = ex.ToString()
According to this answer I can't use .Result