Get Position and Size of GameObject in pixels and then crop a screenshot
Hi all,
Im using Vuforia AR
I intent to take a screenshot for a number (Barcode - OCR code) from a specific place in the screen, i create a Plane gameobject for use like a marker.
This is my code for the momment for find the Plane in screen and then take a screenshot:
private void TakeScreenShot()
{
Vector3 screenPos = Camera.main.WorldToScreenPoint(ocrCropGrid.GetComponent<Transform>().position);
Bounds ocrBounds = ocrCropGrid.GetComponent<Renderer>().bounds;
Vector3 posStart = Camera.main.WorldToScreenPoint(new Vector3(ocrBounds.min.x, ocrBounds.min.y, ocrBounds.min.z));
Vector3 posEnd = Camera.main.WorldToScreenPoint(new Vector3(ocrBounds.max.x, ocrBounds.max.y, ocrBounds.min.z));
Debug.Log("posStart:" + posStart);
Debug.Log("posEnd:" + posEnd);
int widthX = (int)(posEnd.x - posStart.x);
int widthY = (int)(posEnd.y - posStart.y);
Debug.Log("widthX:" + widthX);
Debug.Log("widthY:" + widthY);
screenShotOCRCode.ReadPixels(new Rect(screenPos.x - (widthX / 2), screenPos.y - (widthY / 2), widthX, widthY), 0, 0);
screenShotOCRCode.Apply();
//SAVE SCREENSHOT
byte[] image = screenShotOCRCode.EncodeToJPG();
File.WriteAllBytes(Application.dataPath + "/../Screenshots/CardOCR.jpg", image);
Debug.Log("Screenshot Saved");
}
The problem is the position, the X (screenPos.x) position not match camera the Y (screenPos.y) is fine with a common. And with vuforia camera the screenshots go wrong
Any help is appreciated.
Sorry for my english.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612062732im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Glitch Effect On Text 0 Answers
,Attack and chase range issue 2 Answers
script works but has no code inside 1 Answer
Drag and drop on unity Build 0 Answers
How to assign GameObject to Script? 1 Answer