1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; 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; import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.image.WritableRaster; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.Hashtable; import java.util.List;
@Slf4j public class QRCodeUtil { private static int onColor =0xFF000001; private static int offColor =0xFFFFFFFF; private static int margin = 0; private static ErrorCorrectionLevel level = ErrorCorrectionLevel.L;
public static final String APPLET_CODE_FONT_TYPE = "PingFangSC";
private static BufferedImage logoImg=null;
private static final Integer outSpacing = 20; public static final Integer width = 240; public static final Integer height = 240; private static final Integer belowSpacing = 24; private static final Integer titleFontSize = 12; private static final Integer txtFontSize = 14; private static final Integer fontRow = 4; private static final Integer titlesSpacing = 16; private static final Integer txtRowSpacing = 5; private static final Integer bottomSpacing = 36;
public static InputStream generateQRImage(String url, InputStream logo){ Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); hints.put(EncodeHintType.ERROR_CORRECTION, level); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.MARGIN, margin); try { MatrixToImageConfig config = new MatrixToImageConfig(onColor,offColor); BitMatrix bitMatrix = new MultiFormatWriter().encode(url,BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image= MatrixToImageWriter.toBufferedImage(bitMatrix, config);
Graphics2D gs = image.createGraphics(); int ratioWidth = image.getWidth()*2/10; int ratioHeight = image.getHeight()*2/10; int logoWidth = image.getWidth(null)>ratioWidth?ratioWidth:image.getWidth(null); int logoHeight = image.getHeight(null)>ratioHeight?ratioHeight:image.getHeight(null); int x = (image.getWidth() - logoWidth) / 2; int y = (image.getHeight() - logoHeight) / 2;
Image img = ImageIO.read(logo); gs.drawImage(img, x, y, logoWidth, logoHeight, null); gs.setColor(Color.black); gs.setBackground(Color.WHITE); gs.dispose();
ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream imOut = ImageIO.createImageOutputStream(bs); ImageIO.write(image, "jpg", imOut); return new ByteArrayInputStream(bs.toByteArray()); } catch (Exception e) { log.error("生成二维码" + url + "异常",e); } return null; }
public static InputStream generateQRImage(String txt, List title,String type){ Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.ERROR_CORRECTION, level); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.MARGIN, margin); Integer totalHeight = outSpacing+height+belowSpacing+titleFontSize*2+txtFontSize*fontRow+txtRowSpacing*4+titlesSpacing+bottomSpacing; Integer totalHeightShort = outSpacing+height+belowSpacing+titleFontSize+txtFontSize+txtRowSpacing+bottomSpacing; try { MatrixToImageConfig config = new MatrixToImageConfig(onColor,offColor); BitMatrix bitMatrix = new MultiFormatWriter().encode(txt,BarcodeFormat.QR_CODE, width, height, hints); bitMatrix=deleteWhite(bitMatrix); BufferedImage image= MatrixToImageWriter.toBufferedImage(bitMatrix, config); if(logoImg!=null){ try { Graphics2D gs = image.createGraphics(); int ratioWidth = image.getWidth()*2/10; int ratioHeight = image.getHeight()*2/10; int logoWidth = logoImg.getWidth(null)>ratioWidth?ratioWidth:logoImg.getWidth(null); int logoHeight = logoImg.getHeight(null)>ratioHeight?ratioHeight:logoImg.getHeight(null); int x = (image.getWidth() - logoWidth) / 2; int y = (image.getHeight() - logoHeight) / 2; gs.drawImage(logoImg, x, y, logoWidth, logoHeight, null); gs.setColor(Color.black); gs.setBackground(Color.WHITE); gs.dispose(); logoImg.flush(); }catch (Exception e){ log.error("生成二维码添加logo异常",e); } }
image=resizeBufferedImage(image, width, height, true); int picHeight = totalHeightShort; if(title.size()>1){ picHeight = totalHeight; } BufferedImage bi = new BufferedImage(width+outSpacing*2,picHeight,BufferedImage.TYPE_INT_RGB);
Graphics2D gp = bi.createGraphics(); gp.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON); gp.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gp.setColor(Color.white); gp.fillRect(0,0,600,600); gp.drawImage(image,outSpacing,outSpacing,width,height,null); Font font = new Font(APPLET_CODE_FONT_TYPE,Font.PLAIN,txtFontSize); Font titleFont = new Font(APPLET_CODE_FONT_TYPE,Font.PLAIN,titleFontSize); FontMetrics metrics = gp.getFontMetrics(font);
int loc_Y = totalHeightShort+titlesSpacing+titleFontSize+txtFontSize+txtRowSpacing-bottomSpacing; gp.setColor(Color.black); for(int i=0;i<title.size();i++){ String titleStr = (String) title.get(i); int titleStrLength = metrics.stringWidth(titleStr); if(i==0){ gp.setFont(titleFont); log.info("============订单号"); gp.drawString("订单号:",outSpacing,outSpacing+height+belowSpacing+titleFontSize); gp.setFont(font); gp.drawString(titleStr,outSpacing,totalHeightShort-bottomSpacing); } if(i==1){ gp.setFont(titleFont); if("sys".equals(type)){ gp.drawString("班级名称:",outSpacing,outSpacing+height+belowSpacing+titleFontSize*2+titlesSpacing+txtFontSize+txtRowSpacing); }else { gp.drawString("课程名称:",outSpacing,outSpacing+height+belowSpacing+titleFontSize*2+titlesSpacing+txtFontSize+txtRowSpacing); } if(titleStrLength<width){ gp.setFont(font); gp.drawString(titleStr,outSpacing,loc_Y); }else { gp.setFont(font); drawStringWithFontStyleLineFeed(gp,titleStr,width,outSpacing,loc_Y,txtRowSpacing,txtFontSize); } } } gp.dispose(); ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream imOut = ImageIO.createImageOutputStream(bs); ImageIO.write(bi, "jpg", imOut); return new ByteArrayInputStream(bs.toByteArray()); } catch (Exception e) { log.error("生成二维码" + txt + "异常",e); } return null; }
private static BitMatrix deleteWhite(BitMatrix matrix) { int[] rec = matrix.getEnclosingRectangle(); int resWidth = rec[2] + 1; int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); resMatrix.clear(); for (int i = 0; i < resWidth; i++) { for (int j = 0; j < resHeight; j++) { if (matrix.get(i + rec[0], j + rec[1])) resMatrix.set(i, j); } } return resMatrix; }
private static BufferedImage resizeBufferedImage(BufferedImage source, int targetW, int targetH, boolean flag) { int type = source.getType(); BufferedImage target = null; double sx = (double) targetW / source.getWidth(); double sy = (double) targetH / source.getHeight(); if (flag && sx > sy) { sx = sy; targetW = (int) (sx * source.getWidth()); } else if(flag && sx <= sy){ sy = sx; targetH = (int) (sy * source.getHeight()); } if (type == BufferedImage.TYPE_CUSTOM) { ColorModel cm = source.getColorModel(); WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH); boolean alphaPremultiplied = cm.isAlphaPremultiplied(); target = new BufferedImage(cm, raster, alphaPremultiplied, null); } else { target = new BufferedImage(targetW, targetH, type); } Graphics2D g = target.createGraphics(); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy)); g.dispose(); return target; }
private static void drawStringWithFontStyleLineFeed(Graphics2D g, String strContent, int maxWdith, int loc_X, int loc_Y,int rowSpacing,int txtFontSize){ int strWidth =getStringLength(g,strContent); int rowWidth=maxWdith; int strHeight=getStringHeight(g); if(strWidth>rowWidth){ char[] strContentArr = strContent.toCharArray(); int count = 0; int line = 0; int charWidth; int charWidthNext; int countValue = 0; int countValueNext; for(int j=0;j< strContentArr.length;j++){ charWidth = g.getFontMetrics().charWidth(strContentArr[j]); if(j==strContentArr.length-1){ charWidthNext=0; g.drawString(strContent.substring(count,j+1),loc_X,loc_Y+(txtFontSize+rowSpacing)*line); }else { charWidthNext = g.getFontMetrics().charWidth(strContentArr[j+1]); } countValue = charWidth + countValue; countValueNext = countValue+charWidthNext; if(countValue<maxWdith&&countValueNext<maxWdith){ continue; }else if(countValue<maxWdith&&countValueNext>=maxWdith){ g.drawString(strContent.substring(count,j),loc_X,loc_Y+(txtFontSize+rowSpacing)*line); countValue=0; line++; count=j; } }
}else{ g.drawString(strContent, loc_X, loc_Y); }
} private static int getStringLength(Graphics g, String str) { char[] strcha=str.toCharArray(); return g.getFontMetrics().charsWidth(strcha, 0, str.length()); }
private static int getStringHeight(Graphics g) { return g.getFontMetrics().getHeight(); } }
|