package com.qxueyou.scc.base.util; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; //import com.qxueyou.scc.base.service.IConfigService; import com.qxueyou.scc.shorturl.util.ShortUrlUtils; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageConfig; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * ¶þάÂëÉú³É¹¤¾ßÀà * * @author µÂ»¢ * */ public final class QrCodeUtils { private static Logger log = LogManager.getLogger(QrCodeUtils.class); /** * Éú³É¶þάÂëͼƬ²¢Êä³öµ½Á÷ * @param msg ÐèÒªÉú³ÉµÄ¶þάÂëÐÅÏ¢ * @param os ¶þάÂëͼƬÊä³öÁ÷£¬Êä³öºó»á×Ô¶¯¹Ø±Õ * @param logoFlag ÊÇ·ñÌí¼Ólogo * */ public static void createQRCodeImgAndSend(String msg, OutputStream os, boolean logoFlag) { try { //Èç¹ûÊÇÍøÖ·£¬Ôòת»»Îª¶ÌÍøÖ· /*if(ShortUrlUtils.IsUrl(msg)){ IShortUrlService shortUrlService = SpringUtil.getBean(IShortUrlService.class); ShortUrl oldShortUrl = shortUrlService.queryShortUrl(msg); if(oldShortUrl != null){//ÅжÏÊÇ·ñÒѾ­´æÔÚÊý¾Ý msg = oldShortUrl.getShortUrl(); }else{ //»ñÈ¡Óò String domain = SpringUtil.getBean(IConfigService.class).getConfigByEnv("domain-name"); //»ñÈ¡¶ÌÁ´½Ó±àÂë String strShortUrlCode = ShortUrlUtils.shortUrl(msg); //Æ´½Ó³É¶ÌÁ´½Ó String shortUrl = domain+(domain.indexOf("/qxueyou", 10)>=0?"/s/":"/qxueyou/s/")+strShortUrlCode; ShortUrl objShortUrl = new ShortUrl(); //±£´æ¶ÌÁ´½ÓµÄÖµ objShortUrl.setShortUrl(shortUrl); objShortUrl.setLongUrl(msg); objShortUrl.setShortUrlCode(strShortUrlCode); objShortUrl.setDeleteFlag(false); TraceUtils.setCreateTrace(objShortUrl); msg = shortUrl; shortUrlService.saveShortUrl(objShortUrl); } }*/ MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); Map hints = new HashMap( 2); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); BitMatrix bitMatrix = multiFormatWriter.encode( msg, BarcodeFormat.QR_CODE, 300, 300, hints); MatrixToImageConfig config = new MatrixToImageConfig(0xFF000000, -1); //Ìí¼Ó»ú¹¹Í¼±ê if(StringUtils.isNoneBlank(ClientUtils.getOrgLogoPath()) && ShortUrlUtils.IsUrl(msg) && logoFlag){ 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) ? 0xFF000000 : 0xFFFFFFFF); } } addLogo_QRCode(image, os); }else{ MatrixToImageWriter.writeToStream(bitMatrix, "PNG", os, config); } } catch (WriterException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } catch (Exception e) { log.error(e, e); } } /** * Ìí¼Ó»ú¹¹logo * * @param buf Éú³ÉµÄ¶þάÂë * @return * @throws IOException */ public static void addLogo_QRCode(BufferedImage buf, OutputStream os) throws IOException{ CloseableHttpClient httpClient = null; HttpGet httpget = null; try{ // String domain = SpringUtil.getBean(IConfigService.class).getConfigByEnv("oss-domain"); String domain =null; httpClient = HttpClients.createDefault(); //»ñÈ¡»ú¹¹logo httpget = new HttpGet(domain+ClientUtils.getOrgLogoPath()); HttpResponse response = httpClient.execute(httpget); HttpEntity entity = response.getEntity(); /** * ¶ÁÈ¡LogoͼƬ */ BufferedImage logo = ImageIO.read(entity.getContent()); /** * ¶ÁÈ¡¶þάÂëͼƬ£¬²¢¹¹½¨»æÍ¼¶ÔÏó */ Graphics2D g = buf.createGraphics(); int widthLogo = 65, heightLogo = 65; // ¼ÆËãͼƬ·ÅÖÃλÖà int x = (buf.getWidth() - widthLogo) / 2; int y = (buf.getHeight() - heightLogo) / 2; //¿ªÊ¼»æÖÆÍ¼Æ¬ g.setColor(Color.WHITE); g.drawImage(logo, x, y, widthLogo, heightLogo, null); g.setStroke(new BasicStroke(1)); g.dispose(); ImageIO.write(buf, "PNG", os); }catch (Exception e) { ImageIO.write(buf, "PNG", os); log.error("¶þάÂëÌí¼Ólogoʧ°Ü"+e); }finally { httpClient.close(); } } }