GithubHelp home page GithubHelp logo

Comments (6)

jamesingreersc avatar jamesingreersc commented on June 10, 2024 1

@spinspin Thanks for this. It helped a bunch!

from zxing.net.mobile.

spinspin avatar spinspin commented on June 10, 2024

@jamesingreersc Did you find a solution to this? I'm running into the same issue .

from zxing.net.mobile.

jamesingreersc avatar jamesingreersc commented on June 10, 2024

@spinspin No, I found nothing helpful. I can see that the UINavigationController is null when the scanner is being initialized. I'm not sure if this a bug in .Net or not. I am going to test the old version of the app that does work and see if it's null there or not.

from zxing.net.mobile.

spinspin avatar spinspin commented on June 10, 2024

It's probably a bug. I've just written some native net7.0-ios code which handles barcode scanning so I don't need to use this library let me know if you want it.

from zxing.net.mobile.

jamesingreersc avatar jamesingreersc commented on June 10, 2024

@spinspin That would be awesome if I could use your code. Thanks bunches!!

from zxing.net.mobile.

spinspin avatar spinspin commented on June 10, 2024

@jamesingreersc this should be enough to get going , I'm using MvvmCross but the core functionality to create a barcode scanner is in here

public class BarcodeScannerViewController : MvxViewController
{
AVCaptureSession captureSession;
AVCaptureVideoPreviewLayer previewLayer;
public Action BarcodeScanned { get; set; }

    public BarcodeScannerViewController(IntPtr handle) : base(handle) { }

    public BarcodeScannerViewController(){}

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        SetUI();
        StartScanning();
    }

    private void SetUI()
    {        
        View.BackgroundColor = UIColor.White;

        NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes
        {
            Font = UIFont.FromName("Gill Sans", 20),
            ForegroundColor = UIColor.Black
        };

        NavigationItem.BackButtonTitle = "";
        NavigationItem.Title = "Barcode Scanning";

        var closeImage = UIImage.FromBundle("Close36pt").ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);

        if (DeviceInfo.Version.Major >= 13)
        {
            closeImage.ApplyTintColor(UIColor.Black);
        }

        var closeButton = new UIButton();
        closeButton.SetImage(closeImage, UIControlState.Normal);
        closeButton.TintColor = UIColor.Black;
        NavigationItem.SetRightBarButtonItem(new UIBarButtonItem(closeButton),true);

        closeButton.TouchUpInside += (sender, args) =>
        {
            ViewModel.OnBarcodeScanCancel();
        };
    }


    void StartScanning()
    {
        try
        {
            captureSession = new AVCaptureSession();
            var videoCaptureDevice = AVCaptureDevice.GetDefaultDevice(AVMediaTypes.Video);
            // Create an input from the video capture device
            var videoInput = AVCaptureDeviceInput.FromDevice(videoCaptureDevice, out NSError error);

            if (error != null)
            {
                ViewModel.OnBarcodeScanError();
                return;
            }
            else
            {
                captureSession.AddInput(videoInput);
            }

            // Create a metadata output and set its delegate
            var metadataOutput = new AVCaptureMetadataOutput
            {
                RectOfInterest = UIScreen.MainScreen.Bounds
            };

            metadataOutput.SetDelegate(new OutputDelegate(DidScanBarcode), DispatchQueue.MainQueue);
            captureSession.AddOutput(metadataOutput);
            metadataOutput.MetadataObjectTypes = AVMetadataObjectType.EAN13Code
                                   | AVMetadataObjectType.QRCode
                                   | AVMetadataObjectType.EAN8Code
                                   | AVMetadataObjectType.Code128Code;

            // Create a preview layer to show the view from the camera
            previewLayer = new AVCaptureVideoPreviewLayer(captureSession)
            {
                Frame = new CGRect(View.Bounds.X, View.Bounds.Y +50 , View.Bounds.Width, View.Bounds.Height-50)
            };


            View.Layer.AddSublayer(previewLayer);

            // Create a new UIView with the desired dimensions.
            var redLineView = new UIView
            {
                BackgroundColor = UIColor.Red,
                Frame = new CGRect(0, previewLayer.Frame.GetMidY(), View.Bounds.Width, 2) // This line is 2 points thick and centered in the preview layer.
            };

            // Add the view to the parent view.
            View.AddSubview(redLineView);

            // Start the capture session
            captureSession.StartRunning();
        }

        catch (Exception)
        {
            ViewModel.OnBarcodeScanError();
        }
    }

    private void DidScanBarcode(string barcode)
    {
        // Stop the capture session and notify the delegate
        captureSession.StopRunning();
        captureSession.Dispose(); // release the capture session
        captureSession = null; // set it to null

        // Remove the preview layer
        previewLayer.RemoveFromSuperLayer();
        previewLayer.Dispose(); // Dispose the preview layer
        previewLayer = null; // set it to null

        ViewModel.OnBarcodeScanSuccess(barcode);
    }


    public override void ViewWillDisappear(bool animated)
    {
        base.ViewWillDisappear(animated);

        if (IsBeingDismissed || IsMovingFromParentViewController)
        {
            Cleanup();
        }
    }

    void Cleanup()
    {
        // Stop the capture session and release resources
        captureSession.StopRunning();
        captureSession.Dispose();
        captureSession = null;

        previewLayer.RemoveFromSuperLayer();
        previewLayer.Dispose();
        previewLayer = null;
    }
}

class OutputDelegate : AVCaptureMetadataOutputObjectsDelegate
{
    Action<string> barcodeHandler;

    public OutputDelegate(Action<string> barcodeHandler)
    {
        this.barcodeHandler = barcodeHandler;
    }

    public override void DidOutputMetadataObjects(AVCaptureMetadataOutput captureOutput, AVMetadataObject[] metadataObjects, AVCaptureConnection connection)
    {
        if (metadataObjects != null && metadataObjects.Length > 0)
        {
            var readableObject = metadataObjects[0] as AVMetadataMachineReadableCodeObject;

            if (readableObject != null)
            {
                var barcode = readableObject.StringValue;
                barcodeHandler?.Invoke(barcode);
            }
        }
    }
}

public interface IBarcodeScanningServiceDelegate
{
    void DidFinishScanning(string barcode);
}

from zxing.net.mobile.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.