登录
转载

聚合数据二维码生成API 案例

发布于 2021-03-29 阅读 549
  • 聚合数据
转载

使用聚合数据提供的免费二维码生成器;
申请地址:https://www.juhe.cn/docs/api/id/296

在pom,xml中引入模板jar包

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>

在service层建立一个HttpClient,用于对外网发出http请求。

import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 1. @author DXG
 2. @date 2018/11/7 0007.
 */
@Service
public class HttpClient {
    public String client(String url, HttpMethod method, MultiValueMap<String, String> parms) {
        RestTemplate template = new RestTemplate();
        ResponseEntity<String> response = template.getForEntity(url, String.class);
        return response.getBody();
    }
}

建立base64与image的工具转换类

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64Test {

    /**
     * 图片转化成base64字符串
     *
     * @param imgFile 图片路径
     * @return
     */
    public static String GetImageStr(String imgFile) {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组  
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //对字节数组Base64编码  
        BASE64Encoder encoder = new BASE64Encoder();
        //返回Base64编码过的字节数组字符串
        return encoder.encode(data);
    }

    /**
     * base64字符串转化成图片
     *
     * @param imgStr
     * @return
     */
    public static boolean GenerateImage(String imgStr, String imgFilePath) {   //对字节数组字符串进行Base64解码并生成图片
        //图像数据为空
        if (imgStr == null) {
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //Base64解码  
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                //调整异常数据
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            //生成jpeg图片
            //新生成的图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

在Controller层建立测试连接。

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ixiaxiang.rq_code.Service.HttpClient;
import com.ixiaxiang.rq_code.Util.Base64Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 1. @author DXG
 2. @date 2018/11/7 0007.
 */
@RequestMapping("/code")
@RestController
public class CodeController {
    @Autowired
    private HttpClient httpClient;

    @RequestMapping("/gettest")
    public String getJson(@RequestParam("id") String id) {
        String AppKey = "这里填对应的KEY";
        String Text= "这里填需要存入的二维码的数据";
        String url = "http://apis.juhe.cn/qrcode/api?key=" + AppKey + "&text="+Text;
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        //调用工具类 返回API返回的Json 
        String str = httpClient.client(url, HttpMethod.GET, params);
        //使用的是alibaba的JSON工具类
        JSONObject object = JSON.parseObject(str);
        String base64 = object.getJSONObject("result").get("base64_image").toString();
        //路径自己修改,这里图方便直接写死了
        Base64Test.GenerateImage(base64, "D:\\a.jpg");
        return "";
    }
}

————————————————
版权声明:本文为CSDN博主「0老船长0」的原创文章

评论区

莲子
4粉丝

励志做一条安静的咸鱼,从此走上人生巅峰。

0

0

0

举报