Kaynağa Gözat

添加生成二维码接口

houweiyu 4 yıl önce
ebeveyn
işleme
824a24221e

+ 13 - 0
pom.xml

@@ -134,6 +134,19 @@
             <version>2.12.0</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.google.zxing</groupId>
+            <artifactId>core</artifactId>
+            <version>3.4.0</version>
+        </dependency>
+
+
+        <dependency>
+            <groupId>com.google.zxing</groupId>
+            <artifactId>javase</artifactId>
+            <version>3.4.0</version>
+        </dependency>
+
     </dependencies>
 
 

+ 1 - 1
src/main/java/fcb/project/manager/FcbProjectManagerApplication.java

@@ -23,7 +23,7 @@ import java.time.format.DateTimeFormatter;
 @EnableFeignClients
 //@NacosPropertySource(dataId = "abner-test", autoRefreshed = true)
 @EnableDiscoveryClient
-@SpringBootApplication
+@SpringBootApplication(scanBasePackages = {"fdage.back.sdk.core" , "fcb.project.manager"})
 @MapperScan(basePackages = {"fcb.project.manager.base.dao"})
 public class FcbProjectManagerApplication {
 

+ 87 - 0
src/main/java/fcb/project/manager/base/utils/DataUtils.java

@@ -1,16 +1,29 @@
 package fcb.project.manager.base.utils;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.EncodeHintType;
+import com.google.zxing.MultiFormatWriter;
+import com.google.zxing.common.BitMatrix;
+import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
+import fdage.back.sdk.utils.FileUtils;
 import lombok.extern.log4j.Log4j2;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.*;
 
+import static com.google.zxing.client.j2se.MatrixToImageConfig.BLACK;
+import static com.google.zxing.client.j2se.MatrixToImageConfig.WHITE;
+
 /**
  * @author abnerhou
  * @date 2020/4/23 17:35
@@ -169,4 +182,78 @@ public class DataUtils {
         return re_md5.toLowerCase();
     }
 
+    /**
+    *
+     * 根据上传的照片生成二维码
+    * **/
+    public static boolean createQRCode(String url, String outPath, String logoPath) throws Exception {
+        // 生成二维码
+
+        int width = 300; // 二维码图片宽度 300
+        int height = 300; // 二维码图片高度300
+
+        String format = "jpg";// 二维码的图片格式 gif
+
+        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
+        // 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%)
+        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
+        // 内容所使用字符集编码
+        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
+//      hints.put(EncodeHintType.MAX_SIZE, 350);//设置图片的最大值
+//      hints.put(EncodeHintType.MIN_SIZE, 100);//设置图片的最小值
+        hints.put(EncodeHintType.MARGIN, 1);//设置二维码边的空度,非负数
+
+        BitMatrix bitMatrix = new MultiFormatWriter().encode(url,
+                //编码类型,目前zxing支持:Aztec 2D,CODABAR 1D format,Code 39 1D,Code 93 1D ,Code 128 1D,
+                //Data Matrix 2D , EAN-8 1D,EAN-13 1D,ITF (Interleaved Two of Five) 1D,
+                //MaxiCode 2D barcode,PDF417,QR Code 2D,RSS 14,RSS EXPANDED,UPC-A 1D,UPC-E 1D,UPC/EAN extension,UPC_EAN_EXTENSION
+                BarcodeFormat.QR_CODE,
+                width, //条形码的宽度
+                height, //条形码的高度
+                hints);//生成条形码时的一些配置,此项可选
+
+        //File outputFile = new File("d:" + File.separator + "new-1.gif");//指定输出路径
+        File outputFile = new File(outPath);//指定输出路径
+        FileUtils.deleteFile(outPath);
+        return writeToFile(bitMatrix, format, outputFile, logoPath);
+    }
+
+    public static BufferedImage toBufferedImage(BitMatrix matrix) {
+        int width = matrix.getWidth();
+        int height = matrix.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, (matrix.get(x, y) ? BLACK : WHITE));
+//              image.setRGB(x, y,  (matrix.get(x, y) ? Color.YELLOW.getRGB() : Color.CYAN.getRGB()));
+            }
+        }
+        return image;
+    }
+
+    public static boolean writeToFile(BitMatrix matrix, String format, File file, String logoPath) throws IOException {
+        BufferedImage image = toBufferedImage(matrix);
+        //设置logo图标
+        LogoConfig logoConfig = new LogoConfig();
+        image = logoConfig.LogoMatrix(image, logoPath);
+
+        File parFile = new File(file.getParent() + File.separator);
+        if (!parFile.exists()){
+            parFile.mkdirs();
+        }
+
+        if (!ImageIO.write(image, format, file)) {
+            //throw new IOException("Could not write an image of format " + format + " to " + file);
+            log.info("Could not write an image of format " + format + " to " + file);
+            return false;
+        } else {
+            log.info("二维码生成成功!");
+            return true;
+        }
+    }
+
+    public static void main(String[] args) throws Exception{
+        createQRCode("https://www.4dkankan.com/spc.html?m=t-pnj0IJX", "C:/Users/4dage/Desktop/logo-file/t-pnj0IJX1.png", null);
+    }
+
 }

+ 61 - 0
src/main/java/fcb/project/manager/base/utils/LogoConfig.java

@@ -0,0 +1,61 @@
+package fcb.project.manager.base.utils;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.geom.RoundRectangle2D;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+public class LogoConfig {
+	/**
+     * 设置 logo  
+     * @param matrixImage 源二维码图片 
+     * @return 返回带有logo的二维码图片 
+     * @throws IOException 
+     * @author Administrator sangwenhao 
+     */  
+     public BufferedImage LogoMatrix(BufferedImage matrixImage, String logoPath) throws IOException{
+         /** 
+          * 读取二维码图片,并构建绘图对象 
+          */  
+         Graphics2D g2 = matrixImage.createGraphics();  
+           
+         int matrixWidth = matrixImage.getWidth();  
+         int matrixHeigh = matrixImage.getHeight();
+         /**
+          * 读取Logo图片
+          */
+         if(logoPath == null){
+             URL url = this.getClass().getResource("logo.jpg");
+             if(null != url){
+                 logoPath = url.getPath();
+             }
+         }
+         if(null == logoPath){
+             return null;
+         }
+         BufferedImage logo = ImageIO.read(new File(logoPath));
+
+         //开始绘制图片  
+         g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制       
+         BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);   
+         g2.setStroke(stroke);// 设置笔画对象  
+         //指定弧度的圆角矩形  
+         RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);  
+         g2.setColor(Color.white);  
+         g2.draw(round);// 绘制圆弧矩形  
+           
+         //设置logo 有一道灰色边框  
+         BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);   
+         g2.setStroke(stroke2);// 设置笔画对象  
+         RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);  
+         g2.setColor(new Color(128,128,128));  
+         g2.draw(round2);// 绘制圆弧矩形  
+           
+         g2.dispose();  
+         matrixImage.flush() ;  
+         return matrixImage ;  
+     }  
+}

+ 103 - 0
src/main/java/fcb/project/manager/core/controller/HouseManagerController.java

@@ -13,15 +13,21 @@ import fcb.project.manager.base.utils.DataUtils;
 import fdage.back.sdk.base.entity.Result;
 import fdage.back.sdk.base.enums.ResultCodeEnum;
 import fdage.back.sdk.base.exception.CommonBaseException;
+import fdage.back.sdk.core.alibabaUtils.AlibabaOssHelper;
+import fdage.back.sdk.utils.FileUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.log4j.Log4j2;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.shiro.authz.annotation.Logical;
+import org.apache.shiro.authz.annotation.RequiresRoles;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.time.LocalDateTime;
 import java.util.HashMap;
@@ -47,6 +53,24 @@ public class HouseManagerController {
     @Autowired
     private TmEstateServiceImpl tmEstateService;
 
+    @Value("${image.local.path}")
+    private String imageLocalPath;
+
+    @Value("${vr.scene.host}")
+    private String sceneVrHost;
+
+    @Value("${vr.scene.link}")
+    private String sceneVrLink;
+
+    @Value("${share.logo.oss.path}")
+    private String ossPath;
+
+    @Value("${oss.query.url}")
+    private String ossQueryUrl;
+
+    @Autowired
+    private AlibabaOssHelper alibabaOssHelper;
+
     @GetMapping("/queryOrSearchList")
     @ApiOperation(value = "根据条件拉取所有楼盘")
     @ApiImplicitParams({
@@ -73,6 +97,85 @@ public class HouseManagerController {
                 resultPage.getCurrent(), resultPage.getRecords()));
     }
 
+    @GetMapping("/getQrImage")
+    @ApiOperation(value = "根据房源ID获取房源分享二维码")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "houseId", value = "房源ID", paramType = "query", required = true, dataType = "String"),
+    })
+    public Result<Object> getQrImage(@RequestParam(required = true) String houseId){
+        if(StringUtils.isBlank(houseId)){
+            return Result.failure("房源ID不能为空");
+        }
+        TmHouse tmHouse = tmHouseService.getById(houseId);
+        if(null == tmHouse || tmHouse.getIsDelete().compareTo(1) == 0){
+            return Result.failure("房源不存在");
+        }
+        if(StringUtils.isBlank(tmHouse.getSceneNum())){
+            return Result.failure("改房源未生成场景码");
+        }
+        String totalVrUrl = sceneVrHost + sceneVrLink + tmHouse.getId() + "&s=" + tmHouse.getSceneNum();
+        String localPath = imageLocalPath + "logo.png";
+        String outPutImageName = System.currentTimeMillis() + ".jpg";
+        String outPutImageResultPath = imageLocalPath + outPutImageName;
+        try {
+            DataUtils.createQRCode(totalVrUrl ,  outPutImageResultPath , localPath);
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.info("生成分享二维码出现异常");
+            return Result.failure("生成分享二维码出现异常");
+        }
+        String resultOssPath = ossPath + outPutImageName;
+        alibabaOssHelper.doUploadThenDelete(outPutImageResultPath , resultOssPath);
+        String totalOssQueryPath = ossQueryUrl + resultOssPath;
+
+        Map<String , Object> resultMap = new HashMap<>();
+        resultMap.put("QrCode" , totalOssQueryPath);
+        resultMap.put("vrLink" , totalVrUrl);
+        return Result.success(resultMap);
+
+    }
+
+    @ApiOperation(value = "上传照片并生成分享二维码")
+    @PostMapping("/uploadImage")
+    public Result uploadIntroduceVideo(@RequestParam("file") MultipartFile file , @RequestParam(required = true) String houseId) {
+        if (null == file) {
+            throw new CommonBaseException(ResultCodeEnum.D101 , "文件缺失");
+        }
+        if(StringUtils.isBlank(houseId)){
+            return Result.failure("房源ID不能为空");
+        }
+        TmHouse tmHouse = tmHouseService.getById(houseId);
+        if(null == tmHouse || tmHouse.getIsDelete().compareTo(1) == 0){
+            return Result.failure("房源不存在");
+        }
+        if(StringUtils.isBlank(tmHouse.getSceneNum())){
+            return Result.failure("改房源未生成场景码");
+        }
+
+        String localPath = imageLocalPath + file.getOriginalFilename();
+        String downLoanVideoPath = FileUtils.parseFile(file, localPath);
+        log.info("照片文件已经下载到本地:{}", downLoanVideoPath);
+
+        String totalVrUrl = sceneVrHost + sceneVrLink + tmHouse.getId() + "&s=" + tmHouse.getSceneNum();
+        String outPutImageName = System.currentTimeMillis() + ".jpg";
+        String outPutImageResultPath = imageLocalPath + outPutImageName;
+        try {
+            DataUtils.createQRCode(totalVrUrl ,  outPutImageResultPath , localPath);
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.info("生成分享二维码出现异常");
+            return Result.failure("生成分享二维码出现异常");
+        }
+        String resultOssPath = ossPath + outPutImageName;
+        alibabaOssHelper.doUploadThenDelete(outPutImageResultPath , resultOssPath);
+        String totalOssQueryPath = ossQueryUrl + resultOssPath;
+
+        Map<String , Object> resultMap = new HashMap<>();
+        resultMap.put("QrCode" , totalOssQueryPath);
+        resultMap.put("vrLink" , totalVrUrl);
+        return Result.success(resultMap);
+    }
+
     @GetMapping("/getHouseDetail")
     @ApiOperation(value = "根据房源ID获取房源详情")
     @ApiImplicitParams({

+ 12 - 0
src/main/resources/application-dev.properties

@@ -29,3 +29,15 @@ spring.redis.jedis.pool.min-idle=0
 
 fcb.client.code=zxd9WS2twZ3J4DuhJ2r3y6qz1OZ2hMbr
 fcb.client.secret=UyUaWPJuLw1xfN9wXusgiZxJ7vu9cre9
+
+
+oss.point=http://oss-cn-shenzhen.aliyuncs.com
+oss.key=LTAIUrvuHqj8pvry
+oss.secrey=JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4
+oss.bucket=4d-tjw
+
+share.logo.oss.path=domain/4dhouse/
+oss.query.url=https://houseoss.4dkankan.com/
+image.local.path=C:\\Users\\4dage\\Desktop\\logo-file\\
+vr.scene.host=test.com
+vr.scene.link=/vrscene/show.html?m=

+ 11 - 0
src/main/resources/application-prod.properties

@@ -31,3 +31,14 @@ spring.redis.jedis.pool.min-idle=0
 fcb.client.code=zxd9WS2twZ3J4DuhJ2r3y6qz1OZ2hMbr
 fcb.client.secret=UyUaWPJuLw1xfN9wXusgiZxJ7vu9cre9
 
+oss.point=http://oss-cn-shenzhen.aliyuncs.com
+oss.key=LTAIUrvuHqj8pvry
+oss.secrey=JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4
+oss.bucket=4d-tjw
+
+
+share.logo.oss.path=domain/4dhouse/
+oss.query.url=https://houseoss.4dkankan.com/
+image.local.path=/image/
+vr.scene.host=test.com
+vr.scene.link=/vrscene/show.html?m=

+ 13 - 1
src/main/resources/application-test.properties

@@ -30,4 +30,16 @@ spring.redis.jedis.pool.min-idle=0
 4dkankan.host.url=https://test.4dkankan.com/vrHouse.html?m=
 
 fcb.client.code=zxd9WS2twZ3J4DuhJ2r3y6qz1OZ2hMbr
-fcb.client.secret=UyUaWPJuLw1xfN9wXusgiZxJ7vu9cre9
+fcb.client.secret=UyUaWPJuLw1xfN9wXusgiZxJ7vu9cre9
+
+
+oss.point=http://oss-cn-shenzhen.aliyuncs.com
+oss.key=LTAIUrvuHqj8pvry
+oss.secrey=JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4
+oss.bucket=4d-tjw
+
+share.logo.oss.path=domain/4dhouse/
+oss.query.url=https://houseoss.4dkankan.com/
+image.local.path=/image/
+vr.scene.host=test.com
+vr.scene.link=/vrscene/show.html?m=

+ 2 - 2
src/main/resources/application.properties

@@ -1,8 +1,8 @@
 
 
 #spring.profiles.active=prod
-#spring.profiles.active=dev
-spring.profiles.active=test
+spring.profiles.active=dev
+#spring.profiles.active=test
 
 #应用名
 spring.application.name=fbc-project-manager