Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make customCameraController internal #29

Closed
maciek-s opened this issue Feb 6, 2025 · 11 comments
Closed

Make customCameraController internal #29

maciek-s opened this issue Feb 6, 2025 · 11 comments

Comments

@maciek-s
Copy link

maciek-s commented Feb 6, 2025

Hello, it would be great to have access to the customCameraController field inside CameraController. I need access to the AVCaptureVideoPreviewLayer for my implementation.
Thanks for your great work!”

@Kashif-E
Copy link
Owner

Kashif-E commented Feb 6, 2025

@maciek-s could you share your usecase? maybe we could find a better way to handle it

@maciek-s
Copy link
Author

maciek-s commented Feb 6, 2025

I think I was a bit rushed (I’m an Android developer with limited experience in iOS), but I found that I can extract it from AVCaptureConnection

@maciek-s maciek-s closed this as completed Feb 6, 2025
@maciek-s
Copy link
Author

maciek-s commented Feb 6, 2025

Unfortunately previewLayer is always null... I would like to map scanned barcode corners/bound to camera preview position

override fun captureOutput(
    output: AVCaptureOutput,
    didOutputMetadataObjects: List<*>,
    fromConnection: AVCaptureConnection
) {
    for (metadata in didOutputMetadataObjects) {
        if (metadata is AVMetadataMachineReadableCodeObject) {
            metadata.stringValue?.let { code ->
                fromConnection.videoPreviewLayer?.let { previewLayer ->
                }
                onResult(
                    CardBarcode(
                        code = code,
                        type = metadata.getCardCodeType()
                    )
                )
            }
        }
    }
}

@maciek-s maciek-s reopened this Feb 6, 2025
@Kashif-E
Copy link
Owner

Kashif-E commented Feb 6, 2025

you should use the plugin api to build your own plugin

@maciek-s
Copy link
Author

maciek-s commented Feb 6, 2025

I know, this is the full code based on your qr_scanner_plugin. I’m not limiting the type to QR codes and also returning a custom result. However, videoPreviewLayer is always null. I would like to map the detected code points to the camera preview points.

actual fun startScanning(
    controller: CameraController,
    private val onResult: (CardBarcode) -> Unit
) {
    val analyzer = BarcodeAnalyzer(onQrScanner = onQrScanner)

    controller.setMetadataObjectsDelegate(analyzer)
    controller.updateMetadataObjectTypes(
        listOf(
            AVMetadataObjectTypeQRCode!!,
            AVMetadataObjectTypeEAN13Code!!,
            AVMetadataObjectTypeEAN8Code!!,
            AVMetadataObjectTypeCode128Code!!,
            AVMetadataObjectTypePDF417Code!!
        )
    )
    controller.startSession()
}


private class BarcodeAnalyzer(private val onResult: (CardBarcode) -> Unit) : NSObject(),
    AVCaptureMetadataOutputObjectsDelegateProtocol {

    override fun captureOutput(
        output: AVCaptureOutput,
        didOutputMetadataObjects: List<*>,
        fromConnection: AVCaptureConnection
    ) {
        for (metadata in didOutputMetadataObjects) {
            if (metadata is AVMetadataMachineReadableCodeObject) {
                fromConnection.videoPreviewLayer?.let { previewLayer ->
                }
                metadata.stringValue?.let { code ->
                    onResult(
                        CardBarcode(
                            code = code,
                            type = metadata.getCardCodeType()
                        )
                    )
                }
            }
        }
    }
}

@Kashif-E
Copy link
Owner

Kashif-E commented Feb 6, 2025

are you working on a fork? could you share project link?

@maciek-s
Copy link
Author

maciek-s commented Feb 6, 2025

This is not a fork, and I can’t share the project link as it is private. However, I manually calculated the necessary information based on the corner points from metadata.corners and my scanner area fraction points. I just need to map it based on the device orientation, which on my device is always AVCaptureVideoOrientationLandscapeRight. When I rotate, the preview becomes incorrect, as I reported here.

@Kashif-E
Copy link
Owner

Kashif-E commented Feb 6, 2025

ill look into it will fix this in the next release

@Kashif-E
Copy link
Owner

@maciek-s i have added a function to get the preview layer and support for barcodes in qr scanner plugin let me know if that works for you or you need something else

@Kashif-E
Copy link
Owner

check the latest master

@maciek-s
Copy link
Author

Hi, thanks for the function to access the preview layer—it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants