Skip to content

How to take photos with a scanner

Prerequisite: Early Access License activated. To test the Photo feature, please apply for the Early Access Program.

To initiate the image-taking mode and receive images taken on the connected device, do this call pgManager.takeImage(...) after you have initialized the pgManager.

You should provide a configuration for the image mode, which is done through the PgImageConfig class, wrapped in a PgCommand and a callback that handles the response. For this callback, implement the IPgImageCallback interface (code in Kotlin).
The PgImageConfig contains one parameter called timeoutMs. This value represents the amount of milliseconds your worker will have to take the image. The callback informing you about a possible time-out error may be delayed to account for processing and transmission time.

The convenience function PgImageConfig.toCommand() used here to wrap PgImageConfig optionally takes a PgCommandParams object, which provides additional control over how the command is executed. For now we only support the replaceQueue flag here, which, if set to true, cancels all commands currently in the queue and adds itself as the first element.

To learn more, see Queueing behavior.

takeImageButton.setOnClickListener {
            val config = PgImageConfig()
            val imageCallback = object : IPgImageCallback {
                override fun onImageReceived(image: PgImage) {
                //sample implementation for showing the picture in an imageView
                    val bmp = BitmapFactory.decodeByteArray(image.bytes, 0, image.bytes.size)
                    //make sure you execute any UI related code on the UI Thread
                    runOnUiThread {
                        imageTaken.setImageBitmap(bmp)
                    }
                }

                override fun onImageError(errorReason: PgError) {
                //implement your logic depending on the error reason returned
                // make sure you execute any UI related code on the UI Thread
                    runOnUiThread {
                        // handle error code here
                    }
                }
            }

            pgManager.takeImage(config.toCommand(), imageCallback)
        }