下载zxing
点击zxing下载
具体代码
public class QRCodeMaker {
private static final int PIC_WIDTH = 400 ;
private static final int PIC_HEIGHT = 400 ;
private static final int BLUE = 0xFF3ED547 ;
private static final int WHITE = 0xFFFFFFFF ;
private static final int BLACK = 0xFF000000 ;
private static final int RED = 0xFFFF0000 ;
private static final String IMG_FORMATE = "jpg" ;
private static final int LOGO_BORDER_WIDTH = 2 ;
private static final Color LOGO_BORDER_COLOR = Color.WHITE;
private static final String LOGO_PATH = "D:" + File.separator + "logo.png" ;
/**
* 生成二维码图片
* @param info 二维码信息
* @param out 目的图片地址
*/
public static void MakeQRCode (String info, String out) throws WriterException, IOException {
File outFile = new File(out);
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
BufferedImage image = toBufferedImg(getBitMatrix(info));
ImageIO.write(image, IMG_FORMATE, outFile);
}
/**
* 生成带logo的二维码图片
* @param info 图片信息
* @param out 输出图片地址
* @param logoPath logo图片地址
* @throws WriterException
* @throws IOException
*/
public static void MakeQrCodeWithLogo (String info, String out, String logoPath) throws WriterException, IOException {
File outFile = new File(out);
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
BufferedImage image = toBufferedImg(getBitMatrix(info));
addLogo2QrCode(image, new File(logoPath));
ImageIO.write(image, IMG_FORMATE, outFile);
}
/**
* get bitMatrix from content
* @param conten qr_code info
* @return
* @throws WriterException
*/
private static BitMatrix getBitMatrix (String conten) throws WriterException {
MultiFormatWriter writer = new MultiFormatWriter();
Map hint = new HashMap();
hint.put(EncodeHintType.CHARACTER_SET, "UTF-8" );
hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
return writer.encode(conten, BarcodeFormat.QR_CODE, PIC_WIDTH, PIC_HEIGHT, hint);
}
private static BufferedImage toBufferedImg (BitMatrix bitMatrix) {
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0 ; x < width; x++) {
for (int y = 0 ; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? BLUE : WHITE);
}
}
return image;
}
private static void addLogo2QrCode (BufferedImage qrImage, File logoFile) throws IOException {
if (qrImage == null ) {
throw new RuntimeException("qrpic is null" );
}
if (!logoFile.exists()) {
throw new FileNotFoundException("can not found logo file at " + logoFile.getAbsolutePath());
}
BufferedImage logo = ImageIO.read(logoFile);
int logoWidth = logo.getWidth();
int logoHeight = logo.getHeight();
int x = (qrImage.getWidth() - logoWidth) / 2 ;
int y = (qrImage.getHeight() - logoHeight) / 2 ;
Graphics2D graphics2D = qrImage.createGraphics();
graphics2D.drawImage(logo, x, y, logoWidth, logoHeight, null );
graphics2D.drawRoundRect(x, y, logoWidth, logoHeight, 15 , 15 );
graphics2D.setStroke(new BasicStroke(LOGO_BORDER_WIDTH));
graphics2D.setColor(LOGO_BORDER_COLOR);
graphics2D.drawRect(x, y, logoWidth, logoHeight);
graphics2D.dispose();
}
}