关于api:聚合数据二维码生成API-案例

4次阅读

共计 3186 个字符,预计需要花费 8 分钟才能阅读完成。

应用聚合数据提供的收费二维码生成器;
申请地址: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」的原创文章
原文链接:https://blog.csdn.net/weixin_…

正文完
 0