Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class QrcodeController {
def referer = request.getHeader("REFERER")
String size = getSize(params)
String backupText = "This qrcode was rendered by http://grails.org/plugin/qrcode"
qrcodeRenderer.renderPng(response,params.url?:params.text?:referer?:backupText, size?.toInteger())
qrcodeRenderer.renderPng(response,params.url?:params.text?:referer?:backupText, size?.toInteger(), params.e, params.c)
}

/**
Expand All @@ -33,10 +33,10 @@ class QrcodeController {
def url = {
String uri = params.u?:params.id?:request.getHeader("REFERER")
String size = getSize(params)
qrcodeRenderer.renderPng(response, uri, size.toInteger().intValue())
qrcodeRenderer.renderPng(response, uri, size.toInteger().intValue(), params.e, params.c)
}

private String getSize(Map params) {
private static String getSize(Map params) {
String size = params.s?:params.size?:params.w?:params.width
if (!size || size.matches(/\D/)) { size = "300"}
return size
Expand All @@ -49,7 +49,7 @@ class QrcodeController {
def text = {
String content = params.t?:params.id
String size = getSize(params)
qrcodeRenderer.renderPng(response, content, size.toInteger().intValue())
qrcodeRenderer.renderPng(response, content, size.toInteger().intValue(), params.e, params.c)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class QrcodeTagLib {
def url = { attrs ->
def size = attrs.height?:attrs.width
String target = request.getRequestURL()
String src = createLink(controller:'qrcode',action:'url',params:[u:target,s:size])
String encoding = attrs.encoding ?: 'UTF-8'
String correction = attrs.correction ?: 'L'
String src = createLink(controller:'qrcode',action:'url',params:[u:target,s:size,e:encoding,c:correction]).replaceAll('&', '&')
def mkp = new groovy.xml.MarkupBuilder(out)
mkp {
img(alt:url,src:src)
Expand All @@ -38,7 +40,9 @@ class QrcodeTagLib {
def image = { attrs ->
def size = attrs.height?:attrs.width
String text = attrs.text
String src = createLink(controller:'qrcode',action:'text',params:[t:text,s:size])
String encoding = attrs.encoding ?: 'UTF-8'
String correction = attrs.correction ?: 'L'
String src = createLink(controller:'qrcode',action:'text',params:[t:text,s:size,e:encoding,c:correction]).replaceAll('&', '&')
def mkp = new groovy.xml.MarkupBuilder(out)
mkp {
img(alt:url,src:src)
Expand All @@ -52,7 +56,9 @@ class QrcodeTagLib {
def label = attrs.label?:"qrlink"
def size = attrs.height?:attrs.width
String target = request.getRequestURL()
String src = createLink(controller:'qrcode',action:'url',params:[u:target,s:size])
String encoding = attrs.encoding ?: 'UTF-8'
String correction = attrs.correction ?: 'L'
String src = createLink(controller:'qrcode',action:'url',params:[u:target,s:size,e:encoding,c:correction]).replaceAll('&', '&')
out << '''<style>
#qrcodebox span {
display: none;
Expand Down
Binary file removed lib/zxing-core-1.5.jar
Binary file not shown.
Binary file added lib/zxing-core-2.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ package org.codehaus.groovy.grails.plugins.qrcode

import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.BarcodeFormat
import com.google.zxing.common.ByteMatrix
import com.google.zxing.common.BitMatrix
import com.google.zxing.EncodeHintType

import ar.com.hjg.pngj.ImageInfo
import ar.com.hjg.pngj.PngWriter
Expand Down Expand Up @@ -48,7 +49,7 @@ class QRCodeRenderer {
* NOTE: this does not mean the <b>image</b> is that size.
* You will have to scale the image up or down to fit.
*/
int calculateMatrixSize(String data) {
static int calculateMatrixSize(String data) {
def length = data.length()
def sqr = Math.sqrt(length)
int size = Math.round(sqr + 0.5)
Expand All @@ -59,11 +60,31 @@ class QRCodeRenderer {
* Renders the data supplied in a QRCode of the pixel size (width == height)
* on the output stream you specify as a PNG.
*/
void renderPng(String data, int requestedSize, OutputStream outputStream) {
void renderPng(String data, int requestedSize, OutputStream outputStream, String characterSet, String errorCorrection) {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(2)
hints.put(EncodeHintType.CHARACTER_SET, characterSet ?: 'UTF-8')
hints.put(EncodeHintType.ERROR_CORRECTION, getErrorCorrectionLevel(errorCorrection))

int size = calculateMatrixSize(data)
ByteMatrix matrix = qrcodeWriter.encode(data, format, size, size)
BitMatrix matrix = qrcodeWriter.encode(data, format, size, size, hints)
renderMatrix(matrix, requestedSize, outputStream)
}

/**
* Get ErrorCorrectionLevel from error correction level string.
*/
private static ErrorCorrectionLevel getErrorCorrectionLevel(String errorCorrection) {
switch (errorCorrection.toUpperCase()) {
case 'M':
return ErrorCorrectionLevel.M
case 'H':
return ErrorCorrectionLevel.H
case 'Q':
return ErrorCorrectionLevel.Q
default:
return ErrorCorrectionLevel.L
}
}

/**
* Renders a String inside a QRCode in a RenderedImage object of the width and
Expand All @@ -72,7 +93,7 @@ class QRCodeRenderer {
* renderer will ignore your scale requests and simply report the smallest image
* it possibly can.
*/
void renderMatrix(ByteMatrix matrix, int size, OutputStream outputStream) {
void renderMatrix(BitMatrix matrix, int size, OutputStream outputStream) {
assert size > 0
int cols = matrix.width
double scale = size / cols;
Expand Down Expand Up @@ -100,7 +121,7 @@ class QRCodeRenderer {
for (int jj = 0; jj < size; jj++) {
int x = jj / scale // truncate
double color = 1.0
if (matrix.get(x, y) == 0) {
if (matrix.get(x, y)) {
color = 0.0
}
line.setValD(jj * channels, color)
Expand All @@ -126,10 +147,10 @@ class QRCodeRenderer {
/**
* A simple helper that reports the proper content type to use for a PNG.
*/
String getPngContentType() { "image/png" }
static String getPngContentType() { "image/png" }

void renderPng(response, String data, int size) {
void renderPng(response, String data, int size, String characterSet, String errorCorrection) {
response.contentType = "image/png"
renderPng(data, size, response.outputStream)
renderPng(data, size, response.outputStream, characterSet, errorCorrection)
}
}
}