XZing and Xamarin Forms

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



XZing and Xamarin Forms



I'm trying to use ZXing to render a simple barcode on screen in Xamarin. I've added the ZXing package to my solution and have the following code:


BarcodeWriterPixelData writer = new BarcodeWriterPixelData

Format = BarcodeFormat.CODE_39 ,
Options = new ZXing.Common.EncodingOptions
Height = 80,
Width = 30,
Margin = 0

;
var data = writer.Write("18898798790");
IDBarCode.Source = ImageSource.FromStream(() => new MemoryStream(data.Pixels));



However my code runs without rendering anything on screen. I've been looking for some sort of sample I can use without any sucess.





Have you downloaded and ran the sample on their github? try that first!
– Axemasta
Aug 8 at 20:39




2 Answers
2



Don't forget to initialize the ZXing library in your platform projects, else it will show up empty. You can do this by adding:


// On iOS in your AppDelegate.cs in the FinishedLaunching method
ZXing.Net.Mobile.Forms.iOS.Platform.Init();



and


// On Android in the MainActivity.cs in the OnCreate method
ZXing.Net.Mobile.Forms.Android.Platform.Init();





Hi,Yes I do have that code but still nothing is shown on the page!
– Amir Peivandi
Aug 9 at 3:07





What does your layout look like? Where is IDBarCode placed?
– Gerald Versluis
Aug 9 at 6:07





My layout is the simplest of all! just a Image on page and I can display any image I want from a Uri or local file and the issue is when I try to render a barcode. I've debugged the code and changing the barcode actually changes the data I get from the function. I'm not sure if the data is valid but it's changing.
– Amir Peivandi
Aug 9 at 10:46





<ContentPage xmlns="xamarin.com/schemas/2014/forms&quot; xmlns:x="schemas.microsoft.com/winfx/2009/xaml&quot; xmlns:local="clr-namespace:PointAndReward" x:Class="PointAndReward.MainPage"> <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" BackgroundColor="Lime"> <Image HorizontalOptions="StartAndExpand" Source="cart" WidthRequest="250" HeightRequest="250" x:Name="IDBarCode"/> <Button Clicked="Handle_Clicked" Text="Render"/> </StackLayout> </ContentPage>
– Amir Peivandi
Aug 9 at 10:51





it shows the cart image I asked and when I click on the button the image is replaced with nothing!
– Amir Peivandi
Aug 9 at 10:51



After trying tons of different things and getting nowhere and contacting the ZXing's dev team I gave up and used a different method.



I used a ZXing component instead if using a normal Image and all works fine now. Here is the code:


private ZXingBarcodeImageView barcode=null;
public MainPage()

InitializeComponent();


void Handle_Clicked(object sender, System.EventArgs e)

if (barcode==null)
barcode = new ZXingBarcodeImageView

HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
;
barcode.BarcodeFormat = ZXing.BarcodeFormat.CODE_128;
barcode.BarcodeOptions.Width = 500;
barcode.BarcodeOptions.Height = 100;
barcode.BarcodeValue = contentEntry.Text.Trim();
barResult.Content = barcode;
else
barcode.BarcodeValue = contentEntry.Text.Trim();





barResult is just a simple ContentView with nothing in it.


<ContentView x:Name="barResult">
</ContentView>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS