java生成二维码,qrcode

本文介绍如何使用barcode4j和zxing库生成条形码和二维码。通过配置参数,可以生成不同类型的条形码图片,并设置其高度、宽度等属性。
通过barcode4j生成

@Controller
@RequestMapping("/bar/{code}")
public class BarCodeController {
@RequestMapping(method = RequestMethod.GET)
public void show(@PathVariable("code") String code,
HttpServletRequest request, HttpServletResponse response)
throws ConfigurationException, BarcodeException, IOException {
DefaultConfiguration cfg = new DefaultConfiguration("barcode");
DefaultConfiguration child = new DefaultConfiguration("datamatrix"); //code128
DefaultConfiguration attr;
//attr= new DefaultConfiguration("height");
//attr.setValue("10");
cfg.addChild(child);
//child.addChild(attr);
attr = new DefaultConfiguration("module-width");
attr.setValue("0.6");
child.addChild(attr);
int orientation = 0;
int resolution = 300;

BarcodeUtil util = BarcodeUtil.getInstance();
BarcodeGenerator gen = util.createBarcodeGenerator(cfg);

ByteArrayOutputStream bout = new ByteArrayOutputStream(4096);


BitmapCanvasProvider bitmap = new BitmapCanvasProvider(bout,
MimeTypes.MIME_JPEG, resolution, BufferedImage.TYPE_BYTE_BINARY,
false, orientation);
gen.generateBarcode(bitmap,code);
try {
bitmap.finish();
} catch (IOException e) {
} finally {
try {
bout.close();
} catch (IOException e) {
}
}
response.setContentType(MimeTypes.MIME_JPEG);
response.setContentLength(bout.size());
response.getOutputStream().write(bout.toByteArray());
response.getOutputStream().flush();
}
}

通过zxing生成

package gov.rsj.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

/**
* @author fox
* @date 2012-3-22 下午3:12:33
* @version 1.0
* @description QRCODE 条形码 需要javase.jar和core.jar两个包
*/
@Controller
@RequestMapping("/qrbar/{code}")
public class QrBarCodeController {
@RequestMapping(method = RequestMethod.GET)
public void show(@PathVariable("code") String code,
HttpServletRequest request, HttpServletResponse response){
QRCodeWriter writer = new QRCodeWriter();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
BitMatrix bitMatrix = null;
try {
bitMatrix = writer.encode(code, BarcodeFormat.QR_CODE, 300, 300);
MatrixToImageWriter.writeToStream(bitMatrix, "jpeg", response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值