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);
答案 0 :(得分:0)
我终于找到了问题。默认情况下,使用纵向模式时,移动设备上的屏幕旋转90度。使用rotateCounterClockwise
再次旋转90,所以在这些配置中我都无法读取水平条形码。
我找到的解决方案是使用rotateCounterClockwise
3次:90(默认值)+ 3 * 90 = 0!
感谢@Draco18s @Michael进行调试。
答案 1 :(得分:0)
我实际上遇到了完全相同的问题。我可以想出一个适合我的解决方案。在通过
获得结果之前var result = barcodeReader.Decode(scannedPicture,CamTexture.width, CamTexture.height);
我将 scannedPicture
(它是 Color32[])旋转 90 度,然后将其提供给 barcodeReader.Decode(...)
。
您将找到一种在线旋转一维数组的方法。这不仅可以解决设备方向的问题,还可以让您选择额外检查当前扫描图像的旋转版本,从而扫描未水平对齐的条码。这为我构建的扫描仪功能提供了更灵敏的体验。