How could I encode my string into a QR Code using ZXing.Net?
I can already decode, but having problems in encoding. It has an error that says: no encoder available for format AZTEC.
Here is my code:
IBarcodeWriter writer = new BarcodeWriter();
Bitmap barcodeBitmap;
var result = writer.Encode("Hello").ToBitmap();
barcodeBitmap = new Bitmap(result);
pictureBox1.Image = barcodeBitmap;
You don't fully initialize the BarcodeWriter. You have to set the barcode format.
Try the following code snippet:
IBarcodeWriter writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE };
var result = writer.Write("Hello");
var barcodeBitmap = new Bitmap(result);
pictureBox1.Image = barcodeBitmap;
#dizzytri99er
Seems that I have sucessfully encoded a message with ZXing.net therefore I think it does support Aztec encoding
This is the code I have used;
static void Main(string[] args)
{
IBarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.AZTEC
};
Bitmap aztecBitmap;
var result = writer.Write("I love you ;)");
aztecBitmap = new Bitmap(result);
using (var stream = new FileStream("test.bmp", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
var aztecAsBytes = ImageToByte(aztecBitmap);
stream.Write(aztecAsBytes, 0, aztecAsBytes.Length);
}
}
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
could it possibly be the size of the codes your are scanning?
take a look here
best way to generate and encode QR codes would be...
QR code encoder and Zbar
Related
I am writing a UWP application using the MapControl I have implemented my own MapTileSource like so:
var nativeTileLayer = new MapTileSource();
nativeTileLayer.DataSource = new UWPSyncTileLayer();
nativeTileLayer.TilePixelSize = outerItem.TileSize;
nativeTileLayer.AllowOverstretch = true;
// NativeMap.Style = MapStyle.None;
outerItem.NativeObject = nativeTileLayer;
NativeMap.TileSources.Clear();
NativeMap.TileSources.Add(nativeTileLayer);
return nativeTileLayer;
Where UWPSyncTileLayer is my own implementation of CustomMapTileDataSource
internal class UWPSyncTileLayer : CustomMapTileDataSource
{
public UWPSyncTileLayer(int tileSize = 256)
{
this.BitmapRequested += UWPSyncTileLayer_BitmapRequested;
}
private void UWPSyncTileLayer_BitmapRequested(CustomMapTileDataSource sender, MapTileBitmapRequestedEventArgs args)
{
var deferral = args.Request.GetDeferral();
var data = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAASQUlEQVR42u3dfZBeVX3A8W921+263W7jNk1pmomZqJmYiTHS8GKIaUSLNFIa0SJSYZRBy5Si0lZbHOgMwx8MoxQp06LT0oigtKLgtIIKyjvyJuHFQCCEgCRACHlZlnWTbLKb/nHOSoBN2Ow+z73n3Pv9zJxJhpnwPM+5v/O755x77jmTkPLSGct0YDZwUPzvPwUetXqkamkD5gGnACuAh4CXgD2vKXdaVeOrXCk1HcB84M+BY4FZQNcb/JvpVpuUr5bYpf8y8MAod/g3KuutQinPhr8AuBrYNo6GbwKQMh1+LgauBXZNoOGbAKTM7vhLgOuA7Q1o+CPlSatWStuceMcfamDDHykPWb1SmtqBzwHPN6Hhj5SbrWYpve7+4cA9TWz4I+Uqq/vAuQ5AzbzrnwecBnQX8HmbrXITgNIwE7gMOLLAz3zWapfKtwh4ooAu/2vLcqteKrc3eRqjr9NvdhkivC8gqQSdwMU05/HeWMqLvPJWoKQCdQBXlNj49xCeMnR4KaTi7/yXldjwR8qlXgqp+Dv/5SXf+UfG/yd6OaRi7/wrErjz7wF+TVhiLKkALcBXErjzj5Rfxu8kqQAnJ9T49wDne0mkYiwCtiTU+IcIewpIarLplLPCb3/lcYp5z0CqtU7gR4k1/j1xLkJSE7UA5yY27h+Z/T/YyyM111wmtllns8pdOPsvNVU7cGOCjX8P4WmEpCb6FI3ZsbfR5UlgspdHap4ZhK22U7z7f8nLIzXXpYk2/heAaV4eqXnmAC8nmgDO9vJIzXVJoo3/KaDHyyM1zyzCDjupNf4hwpZjkprogkTv/vfxxkeES5qAqaT1ss9I2Qks9fJIzXV2onf/b+CqP6mpugmba6Q48TfdyyM119Gk98LPLuA4L43UfFclePe/zK6/1HxdlHOiz/7K/cAUL43UfMsT6/5vw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[1]);
MemoryStream stream = new MemoryStream();
stream.Write(data, 0, data.Length);
stream.Position = 0;
var decoder = Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream.AsRandomAccessStream()).AsTask().Result;
var pixelProvider = decoder.GetPixelDataAsync().AsTask().Result;// Windows.Graphics.Imaging.BitmapPixelFormat.Rgba8, Windows.Graphics.Imaging.BitmapAlphaMode.Ignore, new Windows.Graphics.Imaging.BitmapTransform(), Windows.Graphics.Imaging.ExifOrientationMode.RespectExifOrientation, Windows.Graphics.Imaging.ColorManagementMode.ColorManageToSRgb).AsTask().Result;
var pixelData = pixelProvider.DetachPixelData();
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
IOutputStream outputStream = randomAccessStream.GetOutputStreamAt(0);
DataWriter writer = new DataWriter(outputStream);
writer.WriteBytes(pixelData);
var i = writer.StoreAsync().AsTask().Result;
var d = writer.FlushAsync().AsTask().Result;
args.Request.PixelData = RandomAccessStreamReference.CreateFromStream(randomAccessStream);
deferral.Complete();
}
}
But the image is always blank.
Does anyone know what I'm doing wrong?
I can't tell from your data stream, but it looks like you might be passing a compressed PNG stream to the PixelData property. You'll need to decode a PNG to RGB values and set PixelData to the decoded RGBA buffer. See the example code here:https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.maps.maptilebitmaprequest.pixeldata.aspx
I have the PNG image data like below but I can't able to decode it by using any of the decoding method.
People who have knowledge on this help me to get the image by using encoded/decoding technique of this.
"�PNG\r\n\u001a\n\0\0\0\rIHDR\0\0\u0013`\0\0\u001bf\u0001\0\0\0\0Nw�v\0\0 \0IDATx��O��H���(/�\u0017\u0006�b-tP\u001ej�U����4\u0005l)Y�}�Q\u001fa���9*\a���ڢgGo{\u001f\u0006\u001d_##C��\u0004:,�\u001e�\t\u001d\u0004�\u001c�0��o\n*a �\u0019��6��I���H�����o�#\u007f\u000f\"#��iF��9iƗ\u00165\0\u0010\u0011=t\u0001��\u0003d\u0003d#6#\u0006d\u0003d#6#\u0006d\u0003d#6#\u0006d\u0003d#6#\u0006d\u0003d....
It has all the png critical chunks like IHDR, IDAT, IEND.
//For Encoding
byte[] buf = File.ReadAllBytes(#"C:\Users\GPL\Desktop\Newfolder\balloon_PNG4957.png");
var s = Encoding.ASCII.GetString(buf);
File.WriteAllText(#"C:\Users\GPL\Desktop\balloon_PNG4957.txt", s);
//For Decoding
var rawdata = File.ReadAllText(#"C:\Users\GPL\Desktop\balloon_PNG4957.txt");
byte[] buf = Encoding.ASCII.GetBytes(rawdata);
var ms = new MemoryStream(buf);
var bitmap = Image.FromStream(ms); //Error
pictureBox1.Image = bitmap;
Here while decoding I am getting error - ""Parameter is not valid". "
It is called escaped string literal
try this (replace real text here after st=) like this:
string st= "�PNG\r\n\u001a\n\0\0\0\rIHDR\0\0\u0013`\0\0\u001bf...";
File.WriteAllBytes("png.png", st.Select(s => (byte) s).ToArray());
or simply convert it char by char :
var ba = new List<byte>();
foreach (var s in st)
{
ba.Add((byte) s);
}
File.WriteAllBytes("png.png", ba.ToArray());
note: for two bytes Unicode chars use another ba.Add((byte) (s>>8)); inside foreach.
this is what you need: C# escape characters in user input
see: Can I convert a C# string value to an escaped string literal
If it is a file you may read it like this and show inside pictureBox1:
var bitmap = Image.FromFile(#"filename.png");
pictureBox1.Image = bitmap;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
If it is Stream or MemoryStream or byte[] use this:
byte[] buf = File.ReadAllBytes(#"filename.png");
var ms=new MemoryStream(buf);
var bitmap = Image.FromStream(ms);
pictureBox1.Image = bitmap;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
It is ASCII Encoding:
byte[] buf = File.ReadAllBytes(#"filename.png");
var sb=new StringBuilder();
var s=Encoding.ASCII.GetString(buf );
textBox1.Text = buf.Length + #", " + s.Length;
File.WriteAllText("png.txt", s);
Stream mr = response.GetResponseStream();
var bitmap = Image.FromStream(mr);
Bitmap bmp = new Bitmap(bitmap);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
I am using zxing library to generate and decode the QR codes. I my application I am generating QR code dynamically and sending the file containing QR by fax API. If I get this fax message from the api and decode it, Qr code is read successfully, but when I send a scanned copy of this file by fax and then receive and read it I am unable to do that. But if I try to read this file using my mobile Qr application it properly reads the Qr code. I am unable to find a solution how to read this file.
Methods used for encoding:
public static System.Drawing.Image GenerateJSONQrCode(QRJsonFax model)
{
var jsonString = JsonConvert.SerializeObject(model);
//encrypt json string
jsonString = Hugo.BLL.Utilities.EncryptionHelper.EncryptQR(jsonString, FaxSetting.IsUseHashing);
var bw = new ZXing.BarcodeWriter();
var encOptions = new ZXing.Common.EncodingOptions() { Width = 200, Height = 200, Margin = 0 };
bw.Options = encOptions;
bw.Format = ZXing.BarcodeFormat.QR_CODE;
var image = new Bitmap(bw.Write(Compress(jsonString)));
return image;
}
private static string Compress(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
var ms = new MemoryStream();
using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return Convert.ToBase64String(gzBuffer);
}
Methods used for encoding and decoding
public static FaxReceiver.QrFinder DecodeQrCode(string imagePathToDecode)
{
long userId = 0;
Bitmap bitmapImage = (Bitmap)Image.FromFile(imagePathToDecode);
ZXing.BarcodeReader barcodeReader = new BarcodeReader() { AutoRotate = true, TryHarder = true }; ;
Result decode = barcodeReader.Decode(bitmapImage);
var scanResult = string.Empty;
if (decode != null)
{
scanResult= Decompress(decode.Text);
}
if (!string.IsNullOrWhiteSpace(scanResult))
{
//decrypt Qr received
var decryptedString = DecryptionHelper.Decrypt(scanResult, FaxSetting.IsUseHashing);
//deserialize JSON received
var resultJson = JsonConvert.DeserializeObject<QRJsonFax>(decryptedString);
if (resultJson != null)
{
long.TryParse(resultJson.UserID.ToString(), out userId);
return new QrFinder()
{
FilePath = imagePathToDecode,
UserId = userId,
PageNo = 0,
DataSourceID = resultJson.DataSourceID,
InboundFaxTypeID = resultJson.InboundFaxTypeID
};
}
}
return null;
}
private static string Decompress(string compressedText)
{
byte[] gzBuffer = Convert.FromBase64String(compressedText);
using (var ms = new MemoryStream())
{
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
byte[] buffer = new byte[msgLength];
ms.Position = 0;
using (var zip = new GZipStream(ms, CompressionMode.Decompress))
{
zip.Read(buffer, 0, buffer.Length);
}
return Encoding.UTF8.GetString(buffer);
}
}
File containing Qr code
The problem is that the QR Decoder is getting confused by the gaps between the pixels in your faxed image. If we zoom into a corner of it, this is what we see.
The scanner is looking for solid black squares to identify the QR code.
If we shrink the image by 50%, it becomes readable.
See for yourself at http://zxing.org/w/decode?u=http%3A%2F%2Fi.stack.imgur.com%2FSCYsd.png
I would suggest that after receiving the faxed image, you should either shrink it, or apply a filter to ensure that the QR codes are solid black. You could also look at sending it at a smaller resolution to see if that helps.
I can decode QR code from an image file as follows-
Bitmap bitmap = new Bitmap(imagePath);
BarcodeReader reader = new BarcodeReader();
Result result = reader.Decode(bitmap);
decodedData = result.Text;
But I want it to do from Byte[].
Byte[] imagefile;
using (var binaryReader = new BinaryReader(Request.Files["files"].InputStream))
{
imagefile = binaryReader.ReadBytes(Request.Files["files"].ContentLength);//image
}
I would like to read QR code from this imagefile variable. Is there any way to do it?
Thank you.
How about:
using (var binaryReader = new BinaryReader(Request.Files["files"].InputStream))
{
byte[] imagefile = binaryReader.ReadBytes(Request.Files["files"].ContentLength); //image
using (MemoryStream memory = new MemoryStream(imagefile))
using (Image bitmap = Image.FromStream(memory)
{
BarcodeReader reader = new BarcodeReader();
Result result = reader.Decode(bitmap);
decodedData = result.Text;
}
}
Or may be even shorter:
using (Image bitmap = Image.FromStream(Request.Files["files"].InputStream))
{
BarcodeReader reader = new BarcodeReader();
Result result = reader.Decode(bitmap);
decodedData = result.Text;
}
I've been fiddling around, but can't seem to find a good documentation for it.
Can I make ZXing generate a CODE_128 barcode image? How?
If you are using ZXing.Net you can do it with the following snippet:
var content = "123456789012345678";
var writer = new BarcodeWriter
{
Format = BarcodeFormat.CODE_128
};
var bitmap = writer.Write(content);
Based on some research this is a possible implementation in MVC:
using (MemoryStream memoryStream = new MemoryStream())
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.CODE_128
};
Bitmap barcodeBitmap;
var barcode = writer.Write(scan);
MemoryStream stream = new MemoryStream();
barcode.Save(memoryStream,ImageFormat.Png);
ViewBag.OneD = "data:image/png;base64," + Convert.ToBase64String(memoryStream.ToArray());
}