Search code examples
unity-game-enginezxing

Zxing change default orientation to horizontal


I'm using zxing on Unity3D and I noticed the default orientation is vertical. For my use case I need to scan barcode horizontally but I can't find how to rotate the default scan orientation. For now I can make it work using both AutoRotate = true and TryHarder = true but it slow down the process (since each image is processed in different orientations). Any idea of how to do that? Thanks.

EDIT: trial working on vertical barcode only

//video feed from camera:
cameraFeed = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.GRAYSCALE);
if (cameraFeed == null)
{
return;
}

//reduce the area to scan
barcodeWidth = cameraFeed.BufferWidth;
barcodeHeight = (int)Mathf.Round(cameraFeed.BufferHeight / 2f);
barcodeLeft = 0;
barcodeTop = (int)Mathf.Round(barcodeHeight * 0.25f);

//create a new luminance with this settings
PlanarYUVLuminanceSource barcodeTexture = new PlanarYUVLuminanceSource(cameraFeed.Pixels, cameraFeed.BufferWidth, cameraFeed.BufferHeight, barcodeLeft, barcodeTop, barcodeWidth, barcodeHeight, false);

//rotate LuminanceSource by 90 degree
barcodeTexture2 = barcodeTexture.rotateCounterClockwise();

//read barcode
data = reader.Decode(barcodeTexture2);

EDIT2: another trial, working but too slow

Texture2D screenshot = new Texture2D(Screen.width, Screen.height/3);
screenshot.ReadPixels(new Rect(0, (int)Mathf.Round(Screen.height / 3f), Screen.width, Screen.height / 3), 0, 0);
screenshot.Apply();
Color32[] color = screenshot.GetPixels32();
barcodeTexture3 = new Color32LuminanceSource(color, Screen.width, Screen.height / 3);
data = reader.Decode(barcodeTexture3);

Solution

  • I finally found the problem. By default the screen on mobile has a 90 rotation when using the portrait mode. Using rotateCounterClockwise rotate by 90 again so in none of these configuration I could read an horizontal barcode. The solution I found is to use rotateCounterClockwise 3 time: 90(default)+3*90 = 0!

    Thanks @Draco18s @Michael for the debug.