Log in or register to post comments

Get Position and Size of GameObject in pixels and then crop a sc

February 2, 2016 - 6:24am #1
Hi all,
 
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.

Get Position and Size of GameObject in pixels and then crop a sc

February 26, 2016 - 10:01am #5

AlessandroB wrote:

In your code snippet here:

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));
 

You seem to be using the "min.z" instead of "max.z" as the third coordinate of the posEnd vector:

Vector3 posEnd = Camera.main.WorldToScreenPoint(new Vector3(ocrBounds.max.x, ocrBounds.max.y, ocrBounds.min.z));

 

However, you use ocrBounds.max.x and ocrBounds.max.y for the first 2 coordinates. At a first glance, this seems to be an inconsitency, is that intentional ?

 

Secondly, the whole code snippet does not make any use of the Vuforia API, while it uses standard Unity API to project points to screen and to take a screenshot; it seems this question may not be directly related to Vuforia, but rather a more general Unity programming question.

 

 

Hi,

 

The ocrBounds.min.z is a mistake when a copy and paste and It's strange i edit my post with this type error but never success the edit.
 
 
 
The problem is:
 
Normal Camera and ARVuforia Camera have the same values in camera components and see the same object in the same position, rotation, etc.But with the ARVuforia Camera the objects appear zooming and i can't find in the vuforia components when execute this extrange zoom.

 

For the moment i have a very accurate crop screenshot but It is not as perfect as I would like it to be.

 

Greetings.-

Get Position and Size of GameObject in pixels and then crop a sc

February 26, 2016 - 7:03am #4

In your code snippet here:

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));
 

You seem to be using the "min.z" instead of "max.z" as the third coordinate of the posEnd vector:

Vector3 posEnd = Camera.main.WorldToScreenPoint(new Vector3(ocrBounds.max.x, ocrBounds.max.y, ocrBounds.min.z));

 

However, you use ocrBounds.max.x and ocrBounds.max.y for the first 2 coordinates. At a first glance, this seems to be an inconsitency, is that intentional ?

 

Secondly, the whole code snippet does not make any use of the Vuforia API, while it uses standard Unity API to project points to screen and to take a screenshot; it seems this question may not be directly related to Vuforia, but rather a more general Unity programming question.

 

Get Position and Size of GameObject in pixels and then crop a sc

February 18, 2016 - 10:55am #3

Any dev? or any idea?

Get Position and Size of GameObject in pixels and then crop a sc

February 3, 2016 - 11:10am #2

+1?

Log in or register to post comments