Comments (6)
@spinspin Thanks for this. It helped a bunch!
from zxing.net.mobile.
@jamesingreersc Did you find a solution to this? I'm running into the same issue .
from zxing.net.mobile.
@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.
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.
@spinspin That would be awesome if I could use your code. Thanks bunches!!
from zxing.net.mobile.
@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)
- Will this project support multiple barcodes at once while scan by camera? HOT 1
- The type or namespace name 'Net'
- ZXingScannerView shows camera stream rotated by 90 degrees in some devices. How can I fix rotation?
- Detect barcode type after scanning
- iOs swipe down to dismiss camera scanner error
- ZXing doesn't scan
- Mirror image when using front camera
- Xamarin forms ZXingScannerView not working on ios
- ZXing in Maintenance Mode Only
- BarCode scanner freezing in Android 7.1 Qubic device.
- Reader photo quality
- Dark mode on Android renders background black, making barcode unusable
- Problem decoding Code39
- Zxing and WinUI3
- [UWP] Camera not capturing when the app is built for .NET Native
- Project dead? Alternatives? HOT 1
- Cannot get ZXing to scan dense/wide 2D barcodes
- Need Help / Alternatives / Anything HOT 2
- [Question] ZXing does not work in ".Net 8 for iOS" - "MobileBarcodeScanner is unsupported on this platform" HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zxing.net.mobile.