1 module qrcode.qrcodewriter; 2 3 import qrcode.renderer.rendererinterface; 4 import qrcode.encoder.encoder; 5 import qrcode.common.errorcorrectionlevel; 6 7 /** 8 * QR code writer. 9 */ 10 class QrCodeWriter 11 { 12 /** 13 * Renderer instance. 14 * 15 * @var RendererInterface 16 */ 17 protected RendererInterface renderer; 18 19 this(RendererInterface irender) 20 { 21 this.renderer = irender; 22 } 23 24 /** 25 * Sets the renderer used to create a byte stream. 26 * 27 * @param RendererInterface $renderer 28 * @return Writer 29 */ 30 public QrCodeWriter setRenderer(RendererInterface renderer) 31 { 32 this.renderer = renderer; 33 return this; 34 } 35 /** 36 * Gets the renderer used to create a byte stream. 37 * 38 * @return RendererInterface 39 */ 40 public RendererInterface getRenderer() 41 { 42 return this.renderer; 43 } 44 45 /** 46 * Writes QR code and returns it as string. 47 * 48 * Content is a string which *should* be encoded in UTF-8, in case there are 49 * non ASCII-characters present. 50 * 51 * @param string $content 52 * @param string $encoding 53 * @param integer $ecLevel 54 * @return string 55 * @throws Exception\InvalidArgumentException 56 */ 57 public string writeString(string content, string encoding = Encoder.DEFAULT_BYTE_MODE_ECODING, 58 ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.L) 59 { 60 if (content.length == 0) 61 { 62 throw new Exception("Found empty contents"); 63 } 64 auto qrCode = Encoder.encode(content, ecLevel, encoding); 65 return this.getRenderer().render(qrCode); 66 } 67 68 /** 69 * Writes QR code to a file. 70 * 71 * @see Writer::writeString() 72 * @param string $content 73 * @param string $filename 74 * @param string $encoding 75 * @param integer $ecLevel 76 * @return void 77 */ 78 public void writeFile(string content, string filename, string encoding = Encoder.DEFAULT_BYTE_MODE_ECODING, 79 ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.L) 80 { 81 import std.file; 82 83 write(filename, this.writeString(content, encoding, ecLevel)); 84 } 85 }