Created
March 29, 2012 18:15
-
-
Save MichaelAstreiko/2241402 to your computer and use it in GitHub Desktop.
BarCode renderer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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