1.生成二維碼
public static byte[] generateQRCodeImages(String text, int width, int height) throws WriterException, IOException {String binary = null;QRCodeWriter qrCodeWriter = new QRCodeWriter();//調整白邊大小Hashtable<EncodeHintType, Object> hints = new Hashtable<>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");hints.put(EncodeHintType.MARGIN, 1);BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);//BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);//bitMatrix = deleteWhite(bitMatrix);ByteArrayOutputStream out = new ByteArrayOutputStream();MatrixToImageWriter.writeToStream(bitMatrix,"PNG",out);byte[] byteArray = out.toByteArray();/*ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(out.toByteArray());byte[] bytes = new byte[out.size()];byteArrayInputStream.read(bytes);BASE64Encoder encoder = new BASE64Encoder();String base64 = encoder.encode(bytes);*/return byteArray;}
2.合并海報
public static void merge(String backGroundImageUrl, /*String qrCodeUrl*/byte[] buf, HttpServletResponse response) {String title="全平臺累計中獎100000人";int size = 35;// 添加字體的屬性設置Font font = new Font("黑體", Font.BOLD, size);try {//加載背景圖片(也就是模板圖)BufferedImage backGroundImage = ImageIO.read(Objects.requireNonNull(getInputStreamByGet(backGroundImageUrl)));//加載二維碼圖片(也就是需要合成到模板圖上的圖片)//BufferedImage imageCode = ImageIO.read(new File(qrCodeUrl));BufferedImage imageCode = ImageIO.read(new ByteArrayInputStream(buf));//把背景圖片當做為模板Graphics2D graphics = backGroundImage.createGraphics();//在模板上繪制圖象(需要繪圖的圖片,左邊距,上邊距,圖片寬度,圖片高度,圖像觀察者)同一個模板一般是不會變的//graphics.drawImage(imageCode, 30, 30, 20, 20, null);int width = Math.min(imageCode.getWidth(null), backGroundImage.getWidth() * 5 / 10);int height = imageCode.getHeight(null) > backGroundImage.getHeight() * 5 / 10 ? (backGroundImage.getHeight() * 5 / 10) : imageCode.getWidth(null);graphics.drawImage(imageCode, (backGroundImage.getWidth() - width) / 2, backGroundImage.getHeight() / 2 + 100, width, height, null);//設置字體graphics.setFont(font);//設置顏色graphics.setColor(Color.BLACK);//獲取字體度量(字體度量是指對于指定字號的某種字體,在度量方面的各種屬性)FontMetrics fontMetrics = graphics.getFontMetrics(font);//獲取字體度量的寬度int textWidth = fontMetrics.stringWidth(title);//左邊距=(模板圖寬度-文字寬度)/2int widthX = (backGroundImage.getWidth() - textWidth) / 2-150;//g.drawString(title, 820, 2850);//繪制文字(內容,左邊距,上邊距),同一個模板上邊距一般也是不變的graphics.drawString(title, widthX, backGroundImage.getHeight() - (size * 2 + 20));graphics.drawString(title, widthX, backGroundImage.getHeight() - (size + 20));//完成模板修改graphics.dispose();//獲取新文件的地址//imageName="E://aa.png;//File outPutFile = new File(imageName);ServletOutputStream outPutFile = response.getOutputStream();//生成新的合成過的用戶二維碼并寫入新圖片,指定類型為pngImageIO.write(backGroundImage, "png", outPutFile);// 刷新輸出流outPutFile.flush();outPutFile.close();} catch (Exception e) {e.printStackTrace();}// 返回給頁面的圖片地址(因為絕對路徑無法訪問)//return imageName;}public static InputStream getInputStreamByGet(String url) {try {HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();conn.setReadTimeout(50000);conn.setConnectTimeout(50000);conn.setRequestMethod("GET");if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {return conn.getInputStream();}} catch (IOException e) {e.printStackTrace();}return null;}
public static void main(String[] args) throws IOException, WriterException {byte[] bytes = generateQRCodeImages("4d5sa4ds4adsad4sd64d6s4a", 350, 350);ImageUtil.merge("http://oss.yunlegeyou.cn/wisdomlife/face/images/1715243426866.jpg",bytes,response);}