Hi, I have created an QR code with next code and show correctly in my app but I need to export it like an image on the gallery folder device. I don't know how to do it.
barcode = new ZXingBarcodeImageView { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, }; barcode.BarcodeFormat = ZXing.BarcodeFormat.QR_CODE; barcode.BarcodeOptions.Width = 500; barcode.BarcodeOptions.Height = 500; barcode.BarcodeValue = card_code; QRCode.Content = barcode;
Thanks in advanced.
Hi 10Cjbellido,
You have to create dependency to save Image on Device.
"text" is the string which represents your QR.
TaskCompletionSource SaveQRComplete = null;
public Task SaveQRAsImage(string text)
{
SaveQRComplete = new TaskCompletionSource();
try
{
var barcodeWriter = new ZXing.Mobile.BarcodeWriter
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new ZXing.Common.EncodingOptions
{
Width = 1000,
Height = 1000,
Margin = 10
}
};
barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer(); var bitmap = barcodeWriter.Write(text); var stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream); // this is the diff between iOS and Android stream.Position = 0; byte[] imageData = stream.ToArray(); var dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim); var pictures = dir.AbsolutePath; //adding a time stamp time file name to allow saving more than one image... otherwise it overwrites the previous saved image of the same name string name = "MY_QR" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"; string filePath = System.IO.Path.Combine(pictures, name); System.IO.File.WriteAllBytes(filePath, imageData); //mediascan adds the saved image into the gallery var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile); mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(filePath))); Xamarin.Forms.Forms.Context.SendBroadcast(mediaScanIntent); SaveQRComplete.SetResult(filePath); return SaveQRComplete.Task; } catch (System.Exception e) { System.Console.WriteLine(e.ToString()); return null; } }
Hi 10Cjbellido,
You Can use following code for iOS app
public Task SaveMyQR(string text)
{
TaskCompletionSource SaveQRComplete = new TaskCompletionSource();
try
{
var barcodeWriter = new ZXing.Mobile.BarcodeWriter
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new ZXing.Common.EncodingOptions
{
Width = 1000,
Height = 1000,
Margin = 10
}
};
barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer(); var bitmap = barcodeWriter.Write(text); var stream = bitmap.AsPNG().AsStream(); byte[] imageData = bitmap.AsPNG().ToArray(); var chartImage = new UIImage(NSData.FromArray(imageData)); chartImage.SaveToPhotosAlbum((image, error) => { //you can retrieve the saved UI Image as well if needed using //var i = image as UIImage; if (error != null) { Console.WriteLine(error.ToString()); } }); SaveQRComplete.SetResult("true"); } catch (Exception ex) { SaveQRComplete.SetResult("false"); } return SaveQRComplete.Task; }
Answers
Hi 10Cjbellido,
You have to create dependency to save Image on Device.
"text" is the string which represents your QR.
TaskCompletionSource SaveQRComplete = null;
public Task SaveQRAsImage(string text)
{
SaveQRComplete = new TaskCompletionSource();
try
{
var barcodeWriter = new ZXing.Mobile.BarcodeWriter
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new ZXing.Common.EncodingOptions
{
Width = 1000,
Height = 1000,
Margin = 10
}
};
Thank you very much. It is working on Android.
Do you know how to do for iOS too?
Thanks!
Hi 10Cjbellido,
You Can use following code for iOS app
public Task SaveMyQR(string text)
{
TaskCompletionSource SaveQRComplete = new TaskCompletionSource();
try
{
var barcodeWriter = new ZXing.Mobile.BarcodeWriter
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new ZXing.Common.EncodingOptions
{
Width = 1000,
Height = 1000,
Margin = 10
}
};
@GauravKatdare Thank you very much. You saved my day.
how to create dependency service?
Hi guys, can you share an example please? It is impossible to implement.
Hello everyone,
The code is not working.
I'm using:
ZXing.Net.Mobile (2.4.1)
ZXing.Net.Mobile.Forms (2.4.1)
I also want save the QRCode that I'm generating/showin in my page.
If anyone have this code working for this ZXing version, please share the code please.
Many thanks!