import cn.hutool.extra.qrcode.QrCodeUtil; import cn.hutool.extra.qrcode.QrConfig; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import io.swagger.annotations.Api; import org.jolokia.util.Base64Util; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; /** * @NAME: * @DATE: **/ @Api(tags = "图片生成") @RestController @RequestMapping(value = "/QRcode") public class QRcodeImageFont { @GetMapping("/createQrCode") public Object createQrCode(@RequestParam("imgUrl") String imgUrl,@RequestParam("qrUrl") String qrUrl, HttpServletResponse response) throws Exception { //在图片上生成二维码,并在图片上添加文字描述 return drawString(imgUrl,qrUrl,response); } /** * * @param * @param response * @throws Exception */ public Object drawString(String imgUrl,String qrUrl, HttpServletResponse response) throws Exception { BufferedImage image = addWater(imgUrl,qrUrl); //可以添加水印逻辑 /* Graphics2D gd = image.createGraphics(); // 3、设置对线段的锯齿状边缘处理 gd.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); // 5、设置水印文字颜色 gd.setColor(Color.red); // 6、设置水印文字Font gd.setFont(new Font("宋体", Font.BOLD, 20)); // 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y) gd.drawString("测试文字", 90, 260); gd.dispose();*/ // 输出图片流 /* ServletOutputStream outputStream = response.getOutputStream(); ImageIO.write(image, "png",outputStream ); System.out.println("添加完成"); outputStream.flush(); outputStream.close();*/ //输出base64 ByteArrayOutputStream boutputStream = new ByteArrayOutputStream(); ImageIO.write(image, "png", boutputStream); String base64Img = Base64Util.encode(boutputStream.toByteArray()); String res = "data:image/jpg;base64," + base64Img.toString(); System.out.println("base64: "+res); boutputStream.flush(); boutputStream.close(); return res; } /*** * 在一张背景图上添加二维码 */ public BufferedImage addWater(String imgUrl,String qrUrl) throws Exception { // 读取原图片信息 //通过本地文件路径获取图片信息一般放在 resources 文件夹下 // File file = new File("classpath:static/img/code.png"); //本地测试地址C:/Users/acer/Desktop/response.png /*File file = new File(imgUrl); //文件转化为图片 Image srcImg = ImageIO.read(file);*/ //通过网络地址获取图片 URL url = new URL(imgUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(15000); connection.setReadTimeout(15000); connection.connect(); //访问图片 DataInputStream dis = new DataInputStream(connection.getInputStream()); Image srcImg = ImageIO.read(dis);//将图片 //获取图片的宽 int srcImgWidth = srcImg.getWidth(null); //获取图片的高 int srcImgHeight = srcImg.getHeight(null); System.out.println("srcImgWidth: "+srcImgWidth+",srcImgHeight: "+srcImgHeight); BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB); // 图片上添加二维码 Graphics2D g = bufImg.createGraphics(); g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null); //使用工具类生成二维码 Image image = createQrCode(qrUrl,200,200); //Image image = createQrCode(qrUrl,srcImgWidth/3,srcImgWidth/3); //将小图片绘到大图片上,500,300 .表示你的小图片在大图片上的位置。 //g.drawImage(image, 400, 750, null); //g.drawImage(image, srcImgWidth-250, srcImgHeight-250, null); g.drawImage(image, srcImgWidth-250, srcImgHeight-250, null); //设置颜色。 g.setColor(Color.WHITE); g.dispose(); return bufImg; } private BufferedImage createQrCode(String content, int width, int height) throws IOException { QrConfig config = new QrConfig(width, height); //Image image = ImageIO.read(resourceLoader("classpath:static/img/onlyLogo.png")); // Image image = ImageIO.read(resourceLoader("F:/code.png")); //config.setImg(image); config.setErrorCorrection(ErrorCorrectionLevel.M); //设置二维码周边留白 config.setMargin(0); return QrCodeUtil.generate( content, config); } }
图片(路径图片,网络图片都支持)添加二维码,留白控制,转base64,直接反出图片流,获取绝对
最新推荐文章于 2023-07-31 11:45:08 发布