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

ZXing BarcodeWriter to Image not displaying (Xamarin.Forms) #197

Closed
davidtavarez opened this issue Sep 5, 2015 · 11 comments
Closed

ZXing BarcodeWriter to Image not displaying (Xamarin.Forms) #197

davidtavarez opened this issue Sep 5, 2015 · 11 comments

Comments

@davidtavarez
Copy link

Using BarcodeWriter from the ZXing.Net.Mobile Nuget package. The code compiles without error does not display the barcode.

public BarcodePage : Page
{
    var barcodeWriter = new BarcodeWriter {
        Format = BarcodeFormat.QR_CODE,
        Options = new ZXing.Common.EncodingOptions {
            Width = 300,
            Height = 50,
            Margin = 30
        }

    };

    byte[] barcode = barcodeWriter.Write ("ABC12345678");

    ImageSource imageSource = ImageSource.FromStream (() => {
        MemoryStream ms = new MemoryStream (barcode);
        ms.Position = 0;
        return ms;
    });

    Image imageBarcode = new Image { Aspect = Aspect.AspectFill };
    imageBarcode.Source = imageSource; 

    Content = imageBarcode;
}
@Redth
Copy link
Owner

Redth commented Nov 10, 2015

Which platform are you running this code on? does it work on any of them?

@rayeckel
Copy link

I am having the same issue. Does not work in WinPhone either. I have a PCL project, with code exactly as posted by davidavarez.

Windows throws the following error:
"The component cannot be found. (Exception from HRESULT: 0x88982F50)"

...with the following stack trace:
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource, CValue& byteStream)
at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource)
at System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource)
at System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource)
at Xamarin.Forms.Platform.WinPhone.StreamImagesourceHandler.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Xamarin.Forms.Platform.WinPhone.ImageRenderer.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__3(Object state)

Looks to me like something is wrong in the "writer.encode" function that is causing the byte array to be malformed.

Thanks.

EDIT:
I got this working by leveraging the platform-specific versions of ZXing.Mobile.BarcodeWriter.

(per this post: http://forums.xamarin.com/discussion/28161/barcode-rendering-with-zxing-net-in-my-xamarin-forms-app )

Thanks.

@davidtavarez
Copy link
Author

Using Xamarin.Forms on iOS and Android.

El 10 nov 2015, a las 10:51 a.m., Jonathan Dick notifications@github.com escribió:

Which platform are you running this code on? does it work on any of them?


Reply to this email directly or view it on GitHub.

@pedrollanes
Copy link

I have the same problem, with android. The code not throws any exception but don't show the image. My code is:

var writer = new BarcodeWriter
            {
                Format = BarcodeFormat.CODE_39,
                Options = new EncodingOptions
                {
                    Height = 400,
                    Width = 400,
                    PureBarcode = true,
                    Margin = 0
                },
            };
var imageBytes = writer.Write("48497094");

_barCodeImage.SetImageBitmap(BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length));

@prefectlyspelt
Copy link

I am also seeing this issue, apparently the only workaround is to implement a custom interface to get the stream in the right format?

Cf. https://forums.xamarin.com/discussion/28161/barcode-rendering-with-zxing-net-in-my-xamarin-forms-app

@Redth
Copy link
Owner

Redth commented Jan 27, 2016

Have you tried the latest version? I specifically fixed this for the Forms implementation on Android :)

@Redth Redth closed this as completed Jan 27, 2016
@rlingineni
Copy link

I don't think it works on iOS? I don't see an image

@VangeM
Copy link

VangeM commented Mar 28, 2016

I have a simple solution for those who have had issues displaying a barcode after upgrading ZXing libraries.
My problem was that the BarcodeWriter.Write() function now returns a Byte array instead of a UIImage. I tried many ways of converting the byte array to a stream or an UIImage and then to a stream, etc., but nothing worked. The byte array seemed to contain the reasonable data, but it could not be converted to an image through any means.
The solution: It appears that there is now more than one version of the Write() function. I had a “Using ZXing.Common;” which worked fine before. You need to remove this or explicitly target the Write function in the ZXing.Mobile library. The right Write() function will go back to returning a UIImage. Simple solution in the end….just wish it didn’t take me several hours to get there!

@stephenhauck
Copy link

I tried to use a Write from ZXing.Mobile but did not see one....

@IsmaelMendonca
Copy link

IsmaelMendonca commented Jan 17, 2017

I am having the same problem, seems that the Write() function returns an invalid byte array, I coudn't convert it to an image. I suppose that is not possible to use it in a generic Xamarin Form PCL way, we gonna need to perform it individually for each plataform using dependency injection.

@nikolaynn1984
Copy link

Hello, I need to save the result generated using
<forms:ZXingBarcodeImageView VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" BarcodeFormat="{Binding ValidatedSelectedFormat}" BarcodeValue="{Binding ValidatedCode}" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" >
in jpeg format on the device. I searched for a lot of time on the Internet, I did not find the result. Can you help?
Thanks in advance,

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

No branches or pull requests

10 participants