Skip to content

Instantly share code, notes, and snippets.

@MichaelAstreiko
Created March 29, 2012 18:15
Show Gist options
  • Save MichaelAstreiko/2241402 to your computer and use it in GitHub Desktop.
Save MichaelAstreiko/2241402 to your computer and use it in GitHub Desktop.
BarCode renderer
import com.google.zxing.MultiFormatWriter
import com.google.zxing.client.j2se.MatrixToImageWriter
import com.google.zxing.common.BitMatrix
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
/**
* @author Michael Astreiko
*/
class BarCodeRendererService {
static transactional = false
MultiFormatWriter barCodeWriter = new MultiFormatWriter()
void renderImage(response, String data, int width, int height, BarcodeFormat format = BarcodeFormat.QR_CODE) {
Hashtable hints = [(EncodeHintType.CHARACTER_SET): 'UTF8']
BitMatrix bitMatrix = barCodeWriter.encode(data, format, width, height, hints)
MatrixToImageWriter.writeToStream(bitMatrix, "png", response.outputStream)
}
void renderImageToFile(File file, String data, int width, int height, BarcodeFormat format = BarcodeFormat.QR_CODE) {
Hashtable hints = [(EncodeHintType.CHARACTER_SET): 'UTF8']
BitMatrix bitMatrix = barCodeWriter.encode(data, format, width, height, hints)
MatrixToImageWriter.writeToFile(bitMatrix, "png", file)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment