Issue getting a Frame from the Device Camera in Unity

0 votes
asked Apr 19, 2023 by thedapperlab (260 points)

Hi, hope you can help...

We're trying to get a frame from the device camera every so often (10th of a second of so), and we have some code in place but it seems to always be returning a value of 0 for all bytes - we're obviously missing something!

Here's our Start method code for the Object attached to the EasyAR GameObject:

IEnumerator Start()
{
    QRreader = new BarcodeReader();
    
    CameraDeviceFrameSource easyarSource = EasyARCameraObject.GetComponentInChildren<CameraDeviceFrameSource>();
    if (easyarSource != null)
    {
        yield return new WaitUntil(() => easyarSource.IsAvailable.Value);
        yield return new WaitWhile(() => easyarSource.Device == null);
        deviceCam = easyarSource.Device;
        sourceWidth = deviceCam.cameraParameters().size().data[0];
        sourceHeight = deviceCam.cameraParameters().size().data[1];
        Debug.Log("QR Device Camera Frame Size: " + sourceWidth + " x " + sourceHeight);
        easyARFrameBytes = new Color32[sourceWidth * sourceHeight];
        EasyARBuffer = Buffer.create(sourceWidth * sourceHeight * bytesPerPixel);
        rawbytes = new byte[sourceWidth * sourceHeight * bytesPerPixel];
        currentFrame = new Color32[sourceWidth * sourceHeight]; 
        image = new Image(EasyARBuffer, PixelFormat.RGBA8888, sourceWidth, sourceHeight);
        previewTexture = new Texture2D(sourceWidth,sourceHeight);
        readyToScan = true;
        Debug.Log("QR Scanning Started...");
    }
    else ShapUtils.traceError("No CameraDeviceFrameSource found on easyAR Object!");
}

The code that runs every 100ms or so is this:

void checkEasyARFrame()
{
    var cameraParams = deviceCam.cameraParameters();
    InputFrame newframe = InputFrame.createWithImageAndCameraParameters(image, cameraParams);

    newframe.image().buffer().copyToByteArray(0,rawbytes,0,rawbytes.Length);

    for (int p = 0; p < (sourceWidth * sourceHeight); p++)
    {
        for (int b = 0; b < 4; b++)
        {
            currentFrame[p][b] = rawbytes[(p * 4) + b];
        }
    }
    
    previewTexture.SetPixels32(currentFrame);
    outputWindow.texture = previewTexture;
    
    processThread = new Thread(ProcessFrame);
    processThread.Start();
    
    newframe.Dispose();
}

What are we doing wrong?!  We've tried different PixelFormats and conversions from NV21 and Grayscale, but I think the issue is that we're not getting any actual data back in the image...

Please log in or register to answer this question.

Welcome to EasyAR SDK Q&A, where you can ask questions and receive answers from other members of the community.
...