Problems Implementing a QR Code Generator with Xamarin Forms - c#

I'm having some trouble with an implementation of the ZXing.Net.Mobile (Android) implementation in Xamarin Forms. I have the following class in my Android project which implements the IBarcodeWriter interface (which is in my Shared Project). The application isn't throwing any errors but it isn't showing an image when added into a stacklayout with DependencyService.Get<IBarcodeWriter> ().GetImage()
This is the class from my Droid project:
namespace SmartCart {
public class BarcodeGenerator : IBarcodeWriter
{
public BarcodeGenerator () {}
public Image image = new Image();
public byte[] bitmapBytes;
public String qrData (String s) {
return s;
}
public void CreateBarcode () {
image.Source = ImageSource.FromStream(() =>
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new EncodingOptions
{
Height = 200,
Width = 600
}
};
var bitmapBytes = writer.Write ("Encode this to QRCode");
MemoryStream ms = new MemoryStream(bitmapBytes);
ms.Position = 0;
return ms;
});
}
public Image GetImage() {
return image;
}
}

We use following method to generate a QR-Code. (look at the bitmap.Compress row, maybe that solves your Problem):
public byte[] GenerateQrImage(string content, int width, int height)
{
var options = new QrCodeEncodingOptions
{
Height = height,
Width = width,
Margin = 0,
PureBarcode = true
};
var writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = options
};
// Generate bitmap
var bitmap = writer.Write(content);
if (bitmap != null)
{
// Get bytes from bitmap
using (var stream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
return stream.ToArray();
}
}
return null;
}
Edit: It turns out, that the problem may be a wrong nuget package. use this package to generate the qr-code

Related

Resize Image When Upload in Asp Core

I need to change the size of the photo and save it to a new size when the user selects a photo for the upload, before saving the photo.
i using this code but it not save with new size . whats the problem ?
public async Task<IActionResult> UploadNewsPic()
{
var file = Request.Form.Files[0];
try
{
if (file.Length > 0)
{
string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
string fullPath = Path.Combine(_applicationRoot.UploadNewPath(), file.Name);
using (var stream = new FileStream(fullPath, FileMode.Create))
{
if (file.IsImage())
{
await file.ResizeImage(3, 3);
file.CopyTo(stream);
}
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
and this is Resize Extention :
public async static Task<Image> ResizeImage(this IFormFile file, int width, int height)
{
using (var memoryStream = new MemoryStream())
{
await file.CopyToAsync(memoryStream);
using (var img = Image.FromStream(memoryStream))
{
return img.Resize(width, height);
}
}
}
public static Image Resize(this Image image, int width, int height)
{
var res = new Bitmap(width, height);
using (var graphic = Graphics.FromImage(res))
{
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.DrawImage(image, 0, 0, width, height);
}
return res;
}
You need to save the result from ResizeImage to the stream. Right now you are just copying the original file.
var img = await file.ResizeImage(3, 3);
img.Save(stream, SomeFormat);
Your ResizeImage() function return a resized image, it doesn't edit the image itself, so you must set it to a variable.
if (file.IsImage())
{
Image imageResized = await file.ResizeImage(3, 3);
// ...
}

ZXing QrCode renderer exception with .Net Core 2.1

I would like to create a QR code with using ZXing(0.16.4) But I meet following exception,
System.InvalidOperationException: 'You have to set a renderer
instance.'
Almost the same code works well with .Net Framework 4.6.1
here is my code
static void Main(string[] args)
{
var qrCode = CreateQrCode("test");
Console.ReadKey();
}
public static byte[] CreateQrCode(string content)
{
BarcodeWriter<Bitmap> writer = new BarcodeWriter<Bitmap>
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Width = 100,
Height = 100,
}
};
var qrCodeImage = writer.Write(content); // BOOM!!
using (var stream = new MemoryStream())
{
qrCodeImage.Save(stream, ImageFormat.Png);
return stream.ToArray();
}
}
I solved the issue, Basically I used https://www.nuget.org/packages/ZXing.Net.Bindings.CoreCompat.System.Drawing
I create BarcodeWriter generated from following namespace
ZXing.CoreCompat.System.Drawing
here is my CreateQrCode method
public static byte[] CreateQrCode(string content)
{
BarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Width = 100,
Height = 100,
}
};
var qrCodeImage = writer.Write(content); // BOOM!!
using (var stream = new MemoryStream())
{
qrCodeImage.Save(stream, ImageFormat.Png);
return stream.ToArray();
}
}
Here is the read QR code method, maybe someone will need as well.
BarcodeReader also generated from the same namespace like create.
Here is the method
public static string ReadQrCode(byte[] qrCode)
{
BarcodeReader coreCompatReader = new BarcodeReader();
using (Stream stream = new MemoryStream(qrCode))
{
using (var coreCompatImage = (Bitmap)Image.FromStream(stream))
{
return coreCompatReader.Decode(coreCompatImage).Text;
}
}
}
Hope this answer will protect someone's hair against pulling.
There is a newer version of the package available and it works with .NET Core 3.1.
https://www.nuget.org/packages/ZXing.Net.Bindings.Windows.Compatibility/
I needed to add "Renderer = new ZXing.Rendering.BitmapRenderer()" when using ZXing.Net v0.16.6
public static byte[] CreateQrCode(string content)
{
byte[] imageData;
var qrWriter = new ZXing.BarcodeWriter<System.Drawing.Bitmap>
{
Format = BarcodeFormat.QR_CODE,
Options = new ZXing.Common.EncodingOptions { Height = 100, Width = 100, Margin = 0 },
Renderer = new ZXing.Rendering.BitmapRenderer()
};
using (var ms = new System.IO.MemoryStream())
using (System.Drawing.Bitmap pixelData = qrWriter.Write(content))
{
pixelData.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
imageData = ms.ToArray();
}
return imageData;
}
I'm currently moving to .net 6 and I used BarcodeWriter from ZXing.Net.Bindings.SkiaSharp NuGet package.
using ZXing.SkiaSharp;
var barcodeWriter = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new EncodingOptions
{
Height = _height,
Width = _width,
Margin = _margin
}
};
using var bitmap = barcodeWriter.Write(qrValue);
using var stream = new MemoryStream();
bitmap.Encode(stream, SKEncodedImageFormat.Png, 100);
Your stream is filled now :)

Cannot convert bitmap to byte[]

So I'm making a Xamarin.Forms app. I have a StackLayout, of which I'm taking a snapshot(only the element, not the whole screen.)
This is the interface:
public interface IViewSnapShot
{
Task<byte[]> CaptureAsync(Xamarin.Forms.View view);
}
this is the event:
private async Task SavePic_ClickedAsync(object sender, EventArgs e)
{
var imageByte = await DependencyService.Get<IViewSnapShot>().CaptureAsync(BlueprintSnapshot);
}
and this is Android platform specific:
public class MakeViewSnapshot : IViewSnapShot
{
Task<byte[]> IViewSnapShot.CaptureAsync(Xamarin.Forms.View view)
{
var nativeView = view.GetRenderer().View;
var wasDrawingCacheEnabled = nativeView.DrawingCacheEnabled;
nativeView.DrawingCacheEnabled = true;
nativeView.BuildDrawingCache(false);
Bitmap bitmap = nativeView.GetDrawingCache(false);
// TODO: Save bitmap and return filepath
nativeView.DrawingCacheEnabled = wasDrawingCacheEnabled;
byte[] bitmapData;
using (var stream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
}
return bitmapData;
}
}
The problem is bitmapData gives error
Cannot implicitly convert type 'byte[]' to 'System.Threading.Tasks.Task'
I have search internet, and every post says that this is the way to convert bitmap to byte[] array. Any idea how to fix this error?
Later I'll want to upload the byte[] array to web api.
Instead of returning a byte[], you can use the Task.FromResult() to wrap a result into a Task:
return Task.FromResult(bitmapData);
Your code might looks like this:
public class MakeViewSnapshot : IViewSnapShot
{
Task<byte[]> IViewSnapShot.CaptureAsync(Xamarin.Forms.View view)
{
var nativeView = view.GetRenderer().View;
var wasDrawingCacheEnabled = nativeView.DrawingCacheEnabled;
nativeView.DrawingCacheEnabled = true;
nativeView.BuildDrawingCache(false);
Bitmap bitmap = nativeView.GetDrawingCache(false);
// TODO: Save bitmap and return filepath
nativeView.DrawingCacheEnabled = wasDrawingCacheEnabled;
byte[] bitmapData;
using (var stream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
}
return Task.FromResult(bitmapData);
}
}
And then later when you want to get the byte[] returned by CaptureAsync() you just need to call:
byte[] result = CaptureAsync(<Your_parameters>).Result;

Set Secondary Tile BackgroundImage from Image in Isolated Storage

This is how I get the stream from an image url:
using (var httpClient = new HttpClient())
{
response = await httpClient.GetStreamAsync(new Uri(IMAGEURL_HERE, UriKind.Absolute));
}
SaveImage(response);
And this is how I save it to IsoloatedStorage:
private void SaveImage(Stream result)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(result);
var wb = new WriteableBitmap(bitmap);
using (IsolatedStorageFileStream fileStream = file.CreateFile("FILENAME.jpg"))
{
int width = wb.PixelWidth;
int height = wb.PixelHeight;
if (wb.PixelWidth > 336)
{
width = 336;
}
if (wb.PixelHeight > 336)
{
height = 336;
}
Extensions.SaveJpeg(wb, fileStream, width, height, 0, 100);
}
}
}
So let's say the file is FILENAME.jpg, I thought I could set it as BackgroundImage to a Secondary Tile like this:
var tileData = new FlipTileData()
{
...
BackgroundImage = new Uri("isostore:/Shared/ShellContent/FILENAME.jpg", UriKind.Absolute),
...
It won't work. It throws no exception, only the image won't be displayed. What do I miss? Of course if I put the Image Url as Uri to BackgroundImage it works, but this is not what I want.
Edit: And I have seen similar questions here but it did not help me with my code.
Try this. May be its help.
string imageFolder = #"\Shared\ShellContent";
string shareJPEG = "FILENAME.jpg";
private void SaveImage(Stream result)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
if(!myIsolatedStorage.DirectoryExists(imageFolder))
{
myIsolatedStorage.CreateDirectory(imageFolder);
}
if (myIsolatedStorage.FileExists(shareJPEG))
{
myIsolatedStorage.DeleteFile(shareJPEG);
}
string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(filePath);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(result);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode WriteableBitmap object to a JPEG stream.
int width = wb.PixelWidth;
int height = wb.PixelHeight;
if (wb.PixelWidth > 336)
{
width = 336;
}
if (wb.PixelHeight > 336)
{
height = 336;
}
Extensions.SaveJpeg(wb, fileStream, width, height, 0, 100);
fileStream.Close();
}
}
private void CreateTile()
{
var tileData = new FlipTileData()
{
....
string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);
BackgroundImage = new Uri(#"isostore:" + filePath, UriKind.Absolute);
....
}
}

Image Resizing from SQL Database on the fly with MVC2

I have a simple MVC2 app that uploads a file from the browser to an MS SQL database as an Image blob.
Then I can return the results with something like:
public FileContentResult ShowPhoto(int id)
{
TemporaryImageUpload tempImageUpload = new TemporaryImageUpload();
tempImageUpload = _service.GetImageData(id) ?? null;
if (tempImageUpload != null)
{
byte[] byteArray = tempImageUpload.TempImageData;
return new FileContentResult (temp, "image/jpeg");
}
return null;
}
But I want to return these images resized as both thumbnails and as a gallery-sized view. Is this possible to do within this Result? I've been playing around with the great imageresizer.net but it seems to want to store the images on my server which I want to avoid. Is it possible to do this on the fly..?
I need to keep the original file and don't, if possible, want to store the images as files on the server.
Thanks for any pointers!
ImageResizer.NET allows you to pass a stream to it for resizing, see Managed API usage
The method you'd use is:
ImageResizer.ImageBuilder.Current.Build(object source, object dest, ResizeSettings settings)
I modified your method to go about it this way, but it is untested. Hope it helps.
public FileContentResult ShowPhoto(int id)
{
TemporaryImageUpload tempImageUpload = new TemporaryImageUpload();
tempImageUpload = _service.GetImageData(id) ?? null;
if (tempImageUpload != null)
{
byte[] byteArray = tempImageUpload.TempImageData;
using(var outStream = new MemoryStream()){
using(var inStream = new MemoryStream(byteArray)){
var settings = new ResizeSettings("maxwidth=200&maxheight=200");
ImageResizer.ImageBuilder.Current.Build(inStream, outStream, settings);
var outBytes = outStream.ToArray();
return new FileContentResult (outBytes, "image/jpeg");
}
}
}
return null;
}
There was a recent Hanselminutes podcast on Image Resizing with Nathanael Jones discussing some of the pitfalls of image resizing.
Even if you do not have 30 odd minutes to listen to the full podcast, the show notes point to some interesting resizing pitfalls, as well as an image resizing library also written by Nathanael Jones.
You could resize the image on the fly:
public void ResizeImage(Stream input, Stream output, int newWidth, int maxHeight)
{
using (var srcImage = Image.FromStream(input))
{
int newHeight = srcImage.Height * newWidth / srcImage.Width;
if (newHeight > maxHeight)
{
newWidth = srcImage.Width * maxHeight / srcImage.Height;
newHeight = maxHeight;
}
using (var newImage = new Bitmap(newWidth, newHeight))
using (var gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
newImage.Save(output, ImageFormat.Jpeg);
}
}
}
and then you could have 2 controller actions (one that displays the full image size and one that displays a thumbnail):
public ActionResult Thumbnail(int id)
{
var tempImageUpload = new TemporaryImageUpload();
tempImageUpload = _service.GetImageData(id) ?? null;
if (tempImageUpload == null)
{
return HttpNotFound();
}
using (var input = new MemoryStream(tempImageUpload.TempImageData))
using (var output = new MemoryStream())
{
ResizeImage(input, output, 640, 1000);
return File(output.ToArray(), "image/jpeg");
}
}

Categories