| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package UIB.COM;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import javax.imageio.ImageIO;
- import jp.sourceforge.qrcode.QRCodeDecoder;
- import jp.sourceforge.qrcode.data.QRCodeImage;
- import jp.sourceforge.qrcode.exception.DecodingFailedException;
- import com.swetake.util.Qrcode;
- public class QRCodeHandler {
- public static void main(String[] args) throws Exception {
-
- StringBuffer context =new StringBuffer();
- context.append("制造商:新余钢铁股份有限公司\n");
- context.append("牌号:").append("50").append("\n");
- context.append("标准号:").append("GB/T710-2008 GB/T699-1999").append("\n");
- context.append("交货状态:").append("AR").append("\n");
- context.append("发货日期:").append("2015-09-19").append("\n");
- context.append("合同号:").append("302015080042").append("\n");
- context.append("证明书编号:").append("XR5-042382").append("\n");
- QRCodeHandler.QRCodeEncoder(context.toString(),"D:\\Program Files\\Tomcat5.5.23\\webapps\\CoreFS\\WEB-INF\\excelConfig\\qr.png");
- }
- /**
- * 生成二维码
- * @throws Exception
- */
- public static void QRCodeEncoder(String context,String path) throws Exception {
- Qrcode qrcode = new Qrcode();
- qrcode.setQrcodeErrorCorrect('M');
- qrcode.setQrcodeEncodeMode('B');
- qrcode.setQrcodeVersion(10);
-
- byte[] d = context.getBytes("gb2312");
- BufferedImage bi = new BufferedImage(180, 180,BufferedImage.TYPE_INT_RGB);
- // createGraphics
- Graphics2D g = bi.createGraphics();
- // set background
- g.setBackground(Color.WHITE);
- g.clearRect(0, 0, 180, 180);
- g.setColor(Color.BLACK);
- // 设置偏移量 不设置可能导致解析出错
- int pixoff = 2;
- // 输出内容> 二维码
- //System.out.println(d.length);
- if (d.length > 0 && d.length < 214) {
- boolean[][] codeOut = qrcode.calQrcode(d);
- for (int i = 0; i < codeOut.length; i++) {
- for (int j = 0; j < codeOut.length; j++) {
- if (codeOut[j][i]) {
- g.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
- }
- }
- }
- } else {
- throw new Exception("内容过长,生成二维码失败.请与技术中心联系.");
- // System.err.println("QRCode content bytes length = "
- // + d.length + " not in [ 0,214 ]. ");
- }
- g.dispose();
- bi.flush();
- File f = new File(path);
- ImageIO.write(bi, "png", f);
- }
-
- /**
- * 解析二维码
- * @throws Exception
- */
- public static void QRCodeDecoderTest() {
- QRCodeDecoder decoder = new QRCodeDecoder();
- File imageFile = new File("TestQRCode.png");
- BufferedImage image = null;
- try {
- image = ImageIO.read(imageFile);
- } catch (IOException e) {
- System.out.println("Error: " + e.getMessage());
- }
- try {
- String decodedData = new String(
- decoder.decode(new J2SEImage(image)), "GBK");
- System.out.println(decodedData);
- } catch (DecodingFailedException dfe) {
- System.out.println("Error: " + dfe.getMessage());
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- }
- class J2SEImage implements QRCodeImage {
- BufferedImage image;
- public J2SEImage(BufferedImage image) {
- this.image = image;
- }
- public int getWidth() {
- return image.getWidth();
- }
- public int getHeight() {
- return image.getHeight();
- }
- public int getPixel(int x, int y) {
- return image.getRGB(x, y);
- }
- }
|