C# 基于4点实例化

C# 基于4点实例化,c#,unity3d,zxing,arcore,C#,Unity3d,Zxing,Arcore,我是zxing,估计二维码的4个角点。下面是我估计角点的代码 LuminanceSource source = new RGBLuminanceSource(barcodeBitmap,QRTexture.width, QRTexture.height); var options = new DecodingOptions { PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.QR_CODE }, TryHarder

我是zxing,估计二维码的4个角点。下面是我估计角点的代码

LuminanceSource source = new RGBLuminanceSource(barcodeBitmap,QRTexture.width, QRTexture.height);
var options = new DecodingOptions { PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.QR_CODE }, TryHarder = true };
this.reader = new BarcodeReader(null, null, ls => new GlobalHistogramBinarizer(ls)) { AutoRotate = false, TryInverted = false, Options = options };
Result result = this.reader.Decode(source);
LuminanceSource source=新的RGBLuminanceSource(条码位图、QRTexture.width、QRTexture.height);
var options=newdecodingoptions{possibleformas=newlist{BarcodeFormat.QR_CODE},TryHarder=true};
this.reader=new barcodereder(null,null,ls=>new globalhistorogrambinarizer(ls)){AutoRotate=false,TryInverted=false,Options=Options};
结果=this.reader.Decode(源);

这给了我一个结果点,有四个角落的二维码。如何根据这些角点的位置将3D对象覆盖在二维码上?

我不知道您正在使用的qr阅读器,但通常您只需要3个点,例如

A-------B
|
|   X
|
C
X是要放置对象的位置

就这么简单

// Assuming given values
Vector3 A; // top-left corner
Vector3 B; // top-right corner
Vector3 C; // bottom-left corner
GameObject obj;

var vectorAtoB = B - A;
var vectorAtoC = C - A;
obj.transform.position = A + vectorAtoB * 0.5f + vectorAtoC * 0.5f;

然后你还需要物体的方向。当然,这取决于您的需要,但最简单的方法是设置对象的和(设置两个轴就足够了,因为第三个轴将自动正确)


如果您还需要比例,那么它会变得棘手-或者至少您还需要一个给定值:

// This is the expected QR code edge length
// if the QR code has exactly this size then the model will have scale 1,1,1
// otherwise it is scaled according to the QR code size    
float normalSize;

var qrEdgeLength = vectorAtoB.magnitude;
obj.transform.localScale = Vector3.one * qrEdgeLength / normalSize;

一个对象有四个属性1)顶部2)左侧3)宽度4)高度。QR角有一个XY坐标系,您可以将左上角的QR点设置为对象的上/左属性,并根据需要缩放宽度和高度的底部。您可能需要为该职位增加额外的利润。谢谢!。。我正在使用ZXing读取二维码。它给出了二维坐标(矢量2),而不是(矢量3)。如何从这些通过zxing获得的2D图像坐标中确定矢量3 A、矢量3 B和矢量3 C?@user1241我假设您获得的2D坐标在相机像素空间中。。。在这种情况下,如果你不知道二维码离相机有多远,这将是非常棘手的。。。。因为我不知道你正在使用的图书馆。。您是否有任何文档链接,说明它返回的结果是什么?从中,他们只能获取文本。。没有任何位置数据。。仅在显示平面中通过像素位置。。但这些数据还不足以获得三维空间坐标。。。除非您事先知道二维码的确切大小,以便您可以使用其透视拉伸等重新计算三维位置。。但这绝不是小事,也不是这个论坛的范围是的!我用zxing来确定二维码。请检查此repo,其中包含代码和文档。也可以检测二维坐标。目前,我正在通过您提到的手动计算姿势,但正如我们所知,pnp是一个棘手的数值问题,有时会变得不稳定。非常感谢。
// This is the expected QR code edge length
// if the QR code has exactly this size then the model will have scale 1,1,1
// otherwise it is scaled according to the QR code size    
float normalSize;

var qrEdgeLength = vectorAtoB.magnitude;
obj.transform.localScale = Vector3.one * qrEdgeLength / normalSize;