ZXing.Net icon indicating copy to clipboard operation
ZXing.Net copied to clipboard

Performance and detection problems using BarCodeReader on Unity 2020, Android Platform

Open kdpkke opened this issue 4 years ago • 4 comments

Hello there. First of all, thank you for making this very useful project.

Right now I just created a new project using Unity 2020.3.13f1 and Android 10 (OnePlus 6):

  1. I have terrible performances when the method BarcodeReader.Decode inside the Update() is called (both Unity Editor and Android);
  2. On Unity Editor, the bar codes are detected correctly, unfortunately on Android it is not the case (it detects a bar code, but the result is totally random). I tried it with an ISBN code in a book I own on both the platforms. I tried with both TryHarder and TryInverted options enabled without success.

I show you the code which is attached to an empty GameManager:

IEnumerator GetQRCode()
{
		startDecodeBarCode = false;
		try
		{
			var Result = barCodeReader.Decode(cameraTexture.GetPixels32(), cameraTexture.width, cameraTexture.height); //cameraTexture is the WebCamTexture object
			if (Result != null)
			{
				BarCodeText = Result.Text;
				if (!string.IsNullOrEmpty(BarCodeText))
				{
					Debug.LogError("DECODED TEXT FROM BARCODE " + BarCodeText);
				}
			}
		}
		catch (Exception ex) { 
			Debug.LogError(ex.Message); 
		}
		yield return null;

		if(!string.IsNullOrEmpty(BarCodeText))
                {
			GetComponent<BarCodeManager>().enabled = false;
		}

		yield return new WaitForSeconds(1f); //every second, both Windows and Android lags a lot.
		startDecodeBarCode = true;
}

void Update()
	{
		if (!camAvailable)
			return;

		float ratio = (float)cameraTexture.width / (float)cameraTexture.height;
		fit.aspectRatio = ratio; // Set the aspect ratio

		float scaleY = cameraTexture.videoVerticallyMirrored ? -1f : 1f; // Find if the camera is mirrored or not
		background.rectTransform.localScale = new Vector3(1f, scaleY, 1f); // Swap the mirrored camera

		int orient = -cameraTexture.videoRotationAngle;
		background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);

		if(startDecodeBarCode) //in Start() method it is set to true.
			StartCoroutine(GetQRCode());
	}

Last but not least, this is what the deep profile shows when the app lags:

profiler

Do you know what I'm doing wrong? Thank you in advance

kdpkke avatar Aug 24 '21 19:08 kdpkke

no ideas?

kdpkke avatar Oct 04 '21 07:10 kdpkke

Sorry, no idea. Perhaps you can provide a dump of a Pixel32 array that returns a false result.

micjahn avatar Oct 07 '21 06:10 micjahn

Sorry, no idea. Perhaps you can provide a dump of a Pixel32 array that returns a false result.

Running it on a separate thread does seem to fix the problem but it does not work with IOS. Any idea?

pinnakkkk avatar Jun 20 '23 18:06 pinnakkkk

Sorry, no idea. Perhaps you can provide a dump of a Pixel32 array that returns a false result.

Running it on a separate thread does seem to fix the problem but it does not work with IOS. Any idea?

Can you please share your code? 'Cause I stopped trying to do this because of this problem

kdpkke avatar Jun 20 '23 20:06 kdpkke