In: Computer Science
C# Assignment:
Create a DLL that contains a class used to generate QR codes. From a console application, reference the project that generates the DLL. Allow the user to enter an address and pass it to the method in the separate DLL that will generate the QR code. You only need to allow the user to generate a single QR code at a time (you may close the application once you have generated it).
Following is the code for Class Library:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
namespace QRGenWrapper
{
public class QRCodeGenerator
{
public Stream Generate(string address)
{
var url =
string.Format("http://chart.apis.google.com/chart?cht=qr&chs={1}x{2}&chl={0}",
address, 200, 200);
WebResponse response = default(WebResponse);
Stream remoteStream = default(Stream);
StreamReader readStream = default(StreamReader);
WebRequest request = WebRequest.Create(url);
response = request.GetResponse();
remoteStream = response.GetResponseStream();
readStream = new StreamReader(remoteStream);
return remoteStream;
}
}
}
After you build this class library project, Refer the dll in console application.
Here is the code for generating QR code and download that QR code in your local computer as png format
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QRGenWrapper;
using System.Drawing;
namespace QRUsage
{
class QRGenerator
{
static void Main(string[] args)
{
QRCodeGenerator generator = new QRCodeGenerator();
Console.WriteLine("Enter Address");
var address = Console.ReadLine();
Stream output = generator.Generate(address);
Image img = Image.FromStream(output);
img.Save("D:/QRCode/" + address + ".png");
Console.WriteLine(img);
output.Close();
Console.ReadLine();
}
}
}