Zxing 将默认方向更改为水平
Zxing change default orientation to horizontal
我在 Unity3D 上使用 zxing,我注意到默认方向是垂直的。对于我的用例,我需要水平扫描条形码,但我找不到如何旋转默认扫描方向。现在我可以同时使用 AutoRotate = true
和 TryHarder = true
来让它工作,但它会减慢这个过程(因为每个图像都是在不同的方向上处理的)。
知道怎么做吗?谢谢
编辑:试用仅垂直条形码
//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:另一个试验,运行速度太慢
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);
终于找到问题了。默认情况下,手机屏幕在使用纵向模式时会旋转 90 度。再次使用 rotateCounterClockwise
旋转 90,因此在这些配置的 none 中我可以读取水平条形码。
我找到的解决方案是使用 rotateCounterClockwise
3 次:90(默认)+3*90 = 0!
感谢@Draco18s @Michael 的调试。
我实际上 运行 遇到了完全相同的问题。我可以想出一个适合我的解决方案。在通过
获得结果之前
var result = barcodeReader.Decode(scannedPicture,CamTexture.width, CamTexture.height);
我将 scannedPicture
(这是一个 Color32[])旋转 90 度,然后将其提供给 barcodeReader.Decode(...)
。
你会找到一种在线旋转一维数组的方法。这不仅可以解决设备方向的问题,还可以让您选择额外检查当前扫描图像的旋转版本,因此也可以扫描未水平对齐的条形码。这让我对自己构建的扫描仪功能有了更加灵敏的体验。
我在 Unity3D 上使用 zxing,我注意到默认方向是垂直的。对于我的用例,我需要水平扫描条形码,但我找不到如何旋转默认扫描方向。现在我可以同时使用 AutoRotate = true
和 TryHarder = true
来让它工作,但它会减慢这个过程(因为每个图像都是在不同的方向上处理的)。
知道怎么做吗?谢谢
编辑:试用仅垂直条形码
//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:另一个试验,运行速度太慢
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);
终于找到问题了。默认情况下,手机屏幕在使用纵向模式时会旋转 90 度。再次使用 rotateCounterClockwise
旋转 90,因此在这些配置的 none 中我可以读取水平条形码。
我找到的解决方案是使用 rotateCounterClockwise
3 次:90(默认)+3*90 = 0!
感谢@Draco18s @Michael 的调试。
我实际上 运行 遇到了完全相同的问题。我可以想出一个适合我的解决方案。在通过
获得结果之前var result = barcodeReader.Decode(scannedPicture,CamTexture.width, CamTexture.height);
我将 scannedPicture
(这是一个 Color32[])旋转 90 度,然后将其提供给 barcodeReader.Decode(...)
。
你会找到一种在线旋转一维数组的方法。这不仅可以解决设备方向的问题,还可以让您选择额外检查当前扫描图像的旋转版本,因此也可以扫描未水平对齐的条形码。这让我对自己构建的扫描仪功能有了更加灵敏的体验。