一个简单的javaweb项目
注释比较多直接上代码 附上使用的jar包Qrcode
package com.fehorizon.erp.pda.utils;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.fehorizon.erp.pda.base.BaseService;
import com.swetake.util.Qrcode;
public class printServiceImpl extends BaseService {
public void barCodeGenera() {
init(false);
String text = strRequiredParam("barcode","二维码信息");
String desc = strRequiredParam("desc","文字内容");
String logoPath = "d:\\aa.png";
int logoWidth = 40;
int logoHeight = 40;
try{
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(7);
int width = 67+12*(7-1);
int height = 67+12*(7-1);
byte [] qrData = text.getBytes("UTF-8");
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);
int pixoff = 2;
boolean [][] d = qrcode.calQrcode(qrData);
for(int y=0;y<d.length;y++) {
for(int x=0;x<d.length;x++) {
if(d[x][y]) {
gs.fillRect(x*3+pixoff, y*3+pixoff, 3, 3);
}
}
}
gs.dispose();
BufferedImage bm = bufferedImage;
File logoFile = new File(logoPath);
BufferedImage logoImg = ImageIO.read(logoFile);
int x = (width-logoWidth)/2;
int y = (height-logoHeight)/2;
Graphics g = bm.getGraphics();
g.drawImage(logoImg, x, y, logoWidth, logoHeight, null);
int whiteWidth = 0;
Font font = new Font("黑体", Font.BOLD, 12);
int fontHeight = g.getFontMetrics(font).getHeight();
int lineNum = 1;
int currentLineLen = 0;
for(int i=0;i<desc.length();i++){
char c = desc.charAt(i);
int charWidth = g.getFontMetrics(font).charWidth(c);
if(currentLineLen+charWidth>width){
lineNum++;
currentLineLen = 0;
continue;
}
currentLineLen += charWidth;
}
int totalFontHeight = fontHeight*lineNum;
int wordTopMargin = 4;
BufferedImage bm1 = new BufferedImage(width, height+totalFontHeight+wordTopMargin-whiteWidth, BufferedImage.TYPE_INT_RGB);
Graphics g1 = bm1.getGraphics();
g1.setColor(Color.WHITE);
g1.fillRect(0, height, width, totalFontHeight+wordTopMargin-whiteWidth);
g1.setColor(Color.black);
g1.setFont(font);
g1.drawImage(bm, 0, 0, null);
int currentLineIndex = 0;
currentLineLen = lineNum-1==currentLineIndex?(width-g.getFontMetrics(font).stringWidth(desc))/2:0;
int baseLo = g1.getFontMetrics().getAscent();
for(int i=0;i<desc.length();i++){
String c = desc.substring(i, i+1);
int charWidth = g.getFontMetrics(font).stringWidth(c);
if(currentLineLen+charWidth>width){
currentLineIndex++;
currentLineLen = lineNum-1==currentLineIndex?(width-g.getFontMetrics(font).stringWidth(desc.substring(i)))/2:0;
g1.drawString(c, currentLineLen + whiteWidth, height+baseLo+fontHeight*(currentLineIndex)+wordTopMargin);
currentLineLen = charWidth;
continue;
}
g1.drawString(c, currentLineLen+whiteWidth, height+baseLo+fontHeight*(currentLineIndex) + wordTopMargin);
currentLineLen += charWidth;
}
g1.dispose();
bm = bm1;
response.setContentType("image/jpeg");
ImageIO.write(bm , "JPEG", response.getOutputStream());
}catch(Throwable e){
e.printStackTrace();
}
}
}