BufferedImage 将照片logo添加到二维码中间
生活随笔
收集整理的這篇文章主要介紹了
BufferedImage 将照片logo添加到二维码中间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.
/*** 將照片logo添加到二維碼中間** @param image 生成的二維碼照片對象* @param imagePath 照片保存路徑* @param logoPath logo照片路徑* @param formate 照片格式*/public static void overlapImage(BufferedImage image,String formate,String imagePath,String logoPath,MatrixToLogoImageConfig logoConfig) {try {BufferedImage logo = ImageIO.read(new File(logoPath));Graphics2D g = image.createGraphics();// 考慮到logo照片貼到二維碼中,建議大小不要超過二維碼的1/5;int width = image.getWidth() / logoConfig.getLogoPart();int height = image.getHeight() / logoConfig.getLogoPart();// logo起始位置,此目的是為logo居中顯示int x = (image.getWidth() - width) / 2;int y = (image.getHeight() - height) / 2;// 繪制圖g.drawImage(logo, x, y, width, height, null);// 給logo畫邊框// 構造一個具有指定線條寬度以及 cap 和 join 風格的默認值的實心 BasicStrokeg.setStroke(new BasicStroke(logoConfig.getBorder()));g.setColor(logoConfig.getBorderColor());g.drawRect(x, y, width, height);g.dispose();// 寫入logo照片到二維碼ImageIO.write(image, formate, new File(imagePath));} catch (Exception e) {e.printStackTrace();}} /*** 插入LOGO** @param source 二維碼圖片* @param imgPath LOGO圖片地址* @param needCompress 是否壓縮* @throws Exception*/private static void insertImage(BufferedImage source, String imgPath, boolean needCompress)throws Exception {File file = new File(imgPath);if (!file.exists()) {System.err.println("" + imgPath + " 該文件不存在!");return;}Image src = ImageIO.read(new File(imgPath));int width = src.getWidth(null);int height = src.getHeight(null);if (needCompress) { // 壓縮LOGOif (width > WIDTH) {width = WIDTH;}if (height > HEIGHT) {height = HEIGHT;}Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics g = tag.getGraphics();g.drawImage(image, 0, 0, null); // 繪制縮小后的圖g.dispose();src = image;}// 插入LOGOGraphics2D graph = source.createGraphics();int x = (QRCODE_WIDTH - width) / 2;int y = (QRCODE_HEIGHT - height) / 2;graph.drawImage(src, x, y, width, height, null);Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);graph.setStroke(new BasicStroke(3f));graph.draw(shape);graph.dispose();}總結
以上是生活随笔為你收集整理的BufferedImage 将照片logo添加到二维码中间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java计算机毕业设计小区疫情防控管理系
- 下一篇: 智慧城市直饮水智能监控系统解决方案