二维码编解码 Java调用示例代码
二维码编解码
支持二维码的生成与识别。二维码生成时,直接返回生成的二维码图片
二维码生成 复制代码
二维码解码 复制代码
注意,该示例代码仅适用于 www.apishop.net网站下API 使用该产品前,您需要通过 https://www.apishop.net/#/api/detail/?productID=128申请API服务
1、二维码生成
package net.apishop.www.controller; import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject; /** * httpUrlConnection访问远程接口工具 */
public class Api
{ /** * 方法体说明:向远程接口发起请求,返回字节流类型结果 * param url 接口地址 * param requestMethod 请求方式 * param params 传递参数 重点:参数值需要用Base64进行转码 * return InputStream 返回结果 */ public static InputStream httpRequestToStream(String url, String requestMethod, Map<String, String> params) { InputStream is = ; try { String parameters = ""; boolean hasParams = false; // 将参数集合拼接成特定格式,如name=zhangsan&age=24 for (String key : params.keySet()) { String value = URLEncoder.encode(params.get(key), "UTF-8"); parameters += key + "=" + value + "&"; hasParams = true; } if (hasParams) { parameters = parameters.substring(0, parameters.length() - 1); } // 请求方式是否为get boolean isGet = "get".equalsIgnoreCase(requestMethod); // 请求方式是否为post boolean isPost = "post".equalsIgnoreCase(requestMethod); if (isGet) { url += "?" + parameters; } URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); // 请求的参数类型(使用restlet框架时,为了兼容框架,必须设置Content-Type为“”空) conn.setRequestProperty("Content-Type", "application/octet-stream"); // conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 设置连接超时时间 conn.setConnectTimeout(50000); // 设置读取返回内容超时时间 conn.setReadTimeout(50000); // 设置向HttpURLConnection对象中输出,因为post方式将请求参数放在http正文内,因此需要设置为ture,默认false if (isPost) { conn.setDoOutput(true); } // 设置从HttpURLConnection对象读入,默认为true conn.setDoInput(true); // 设置是否使用缓存,post方式不能使用缓存 if (isPost) { conn.setUseCaches(false); } // 设置请求方式,默认为GET conn.setRequestMethod(requestMethod); // post方式需要将传递的参数输出到conn对象中 if (isPost) { DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(parameters); dos.flush(); dos.close(); } // 从HttpURLConnection对象中读取响应的消息 // 执行该语句时才正式发起请求 is = conn.getInputStream(); } catch(UnsupportedEncodingException e) { e.printStackTrace(); } catch(MalformedURLException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } return is; } public static void main(String args[]) { String url = "https://api.apishop.net/common/postcode/getPostCodeByAddr"; String requestMethod = "POST"; Map<String, String> params = new HashMap<String, String>(); params.put("content", ""); //内容 params.put("size", ""); //图片大小(范围:1~10),默认为4【注:手机扫描,size为4即可】 params.put("level", ""); //容错(0:L,1:M,:2:H,3:Q),默认为0 params.put("margin", ""); //边框大小(范围:1~10),默认为3 String result = ; try { InputStream is = httpRequestToStream(url, requestMethod, params); byte[] b = new byte[is.available()]; is.read(b); result = new String(b); } catch(IOException e) { e.printStackTrace(); } if (result != ) { JSONObject jsonObject = JSONObject.parseObject(result); String status_code = jsonObject.getString("statusCode"); if (status_code == "000000") { // 状态码为000000, 说明请求成功 System.out.println("请求成功:" + jsonObject.getString("result")); } else { // 状态码非000000, 说明请求失败 System.out.println("请求失败:" + jsonObject.getString("desc")); } } else { // 返回内容异常,发送请求失败,以下可根据业务逻辑自行修改 System.out.println("发送请求失败"); } }
}
[java] view plain copy
package net.apishop.www.controller; import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject; /** * httpUrlConnection访问远程接口工具 */
public class Api
{ /** * 方法体说明:向远程接口发起请求,返回字节流类型结果 * param url 接口地址 * param requestMethod 请求方式 * param params 传递参数 重点:参数值需要用Base64进行转码 * return InputStream 返回结果 */ public static InputStream httpRequestToStream(String url, String requestMethod, Map<String, String> params) { InputStream is = ; try { String parameters = ""; boolean hasParams = false; // 将参数集合拼接成特定格式,如name=zhangsan&age=24 for (String key : params.keySet()) { String value = URLEncoder.encode(params.get(key), "UTF-8"); parameters += key + "=" + value + "&"; hasParams = true; } if (hasParams) { parameters = parameters.substring(0, parameters.length() - 1); } // 请求方式是否为get boolean isGet = "get".equalsIgnoreCase(requestMethod); // 请求方式是否为post boolean isPost = "post".equalsIgnoreCase(requestMethod); if (isGet) { url += "?" + parameters; } URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); // 请求的参数类型(使用restlet框架时,为了兼容框架,必须设置Content-Type为“”空) conn.setRequestProperty("Content-Type", "application/octet-stream"); // conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 设置连接超时时间 conn.setConnectTimeout(50000); // 设置读取返回内容超时时间 conn.setReadTimeout(50000); // 设置向HttpURLConnection对象中输出,因为post方式将请求参数放在http正文内,因此需要设置为ture,默认false if (isPost) { conn.setDoOutput(true); } // 设置从HttpURLConnection对象读入,默认为true conn.setDoInput(true); // 设置是否使用缓存,post方式不能使用缓存 if (isPost) { conn.setUseCaches(false); } // 设置请求方式,默认为GET conn.setRequestMethod(requestMethod); // post方式需要将传递的参数输出到conn对象中 if (isPost) { DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(parameters); dos.flush(); dos.close(); } // 从HttpURLConnection对象中读取响应的消息 // 执行该语句时才正式发起请求 is = conn.getInputStream(); } catch(UnsupportedEncodingException e) { e.printStackTrace(); } catch(MalformedURLException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } return is; } public static void main(String args[]) { String url = "https://api.apishop.net/common/postcode/getPostCodeByAddr"; String requestMethod = "POST"; Map<String, String> params = new HashMap<String, String>(); params.put("content", ""); //内容 params.put("size", ""); //图片大小(范围:1~10),默认为4【注:手机扫描,size为4即可】 params.put("level", ""); //容错(0:L,1:M,:2:H,3:Q),默认为0 params.put("margin", ""); //边框大小(范围:1~10),默认为3 String result = ; try { InputStream is = httpRequestToStream(url, requestMethod, params); byte[] b = new byte[is.available()]; is.read(b); result = new String(b); } catch(IOException e) { e.printStackTrace(); } if (result != ) { JSONObject jsonObject = JSONObject.parseObject(result); String status_code = jsonObject.getString("statusCode"); if (status_code == "000000") { // 状态码为000000, 说明请求成功 System.out.println("请求成功:" + jsonObject.getString("result")); } else { // 状态码非000000, 说明请求失败 System.out.println("请求失败:" + jsonObject.getString("desc")); } } else { // 返回内容异常,发送请求失败,以下可根据业务逻辑自行修改 System.out.println("发送请求失败"); } }
}
复制代码
2、二维码解码
package net.apishop.www.controller; import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject; /** * httpUrlConnection访问远程接口工具 */
public class Api
{ /** * 方法体说明:向远程接口发起请求,返回字节流类型结果 * param url 接口地址 * param requestMethod 请求方式 * param params 传递参数 重点:参数值需要用Base64进行转码 * return InputStream 返回结果 */ public static InputStream httpRequestToStream(String url, String requestMethod, Map<String, String> params) { InputStream is = ; try { String parameters = ""; boolean hasParams = false; // 将参数集合拼接成特定格式,如name=zhangsan&age=24 for (String key : params.keySet()) { String value = URLEncoder.encode(params.get(key), "UTF-8"); parameters += key + "=" + value + "&"; hasParams = true; } if (hasParams) { parameters = parameters.substring(0, parameters.length() - 1); } // 请求方式是否为get boolean isGet = "get".equalsIgnoreCase(requestMethod); // 请求方式是否为post boolean isPost = "post".equalsIgnoreCase(requestMethod); if (isGet) { url += "?" + parameters; } URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); // 请求的参数类型(使用restlet框架时,为了兼容框架,必须设置Content-Type为“”空) conn.setRequestProperty("Content-Type", "application/octet-stream"); // conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 设置连接超时时间 conn.setConnectTimeout(50000); // 设置读取返回内容超时时间 conn.setReadTimeout(50000); // 设置向HttpURLConnection对象中输出,因为post方式将请求参数放在http正文内,因此需要设置为ture,默认false if (isPost) { conn.setDoOutput(true); } // 设置从HttpURLConnection对象读入,默认为true conn.setDoInput(true); // 设置是否使用缓存,post方式不能使用缓存 if (isPost) { conn.setUseCaches(false); } // 设置请求方式,默认为GET conn.setRequestMethod(requestMethod); // post方式需要将传递的参数输出到conn对象中 if (isPost) { DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(parameters); dos.flush(); dos.close(); } // 从HttpURLConnection对象中读取响应的消息 // 执行该语句时才正式发起请求 is = conn.getInputStream(); } catch(UnsupportedEncodingException e) { e.printStackTrace(); } catch(MalformedURLException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } return is; } public static void main(String args[]) { String url = "https://api.apishop.net/common/postcode/getPostCodeByAddr"; String requestMethod = "POST"; Map<String, String> params = new HashMap<String, String>(); params.put("image", ""); //二维码图片文件 String result = ; try { InputStream is = httpRequestToStream(url, requestMethod, params); byte[] b = new byte[is.available()]; is.read(b); result = new String(b); } catch(IOException e) { e.printStackTrace(); } if (result != ) { JSONObject jsonObject = JSONObject.parseObject(result); String status_code = jsonObject.getString("statusCode"); if (status_code == "000000") { // 状态码为000000, 说明请求成功 System.out.println("请求成功:" + jsonObject.getString("result")); } else { // 状态码非000000, 说明请求失败 System.out.println("请求失败:" + jsonObject.getString("desc")); } } else { // 返回内容异常,发送请求失败,以下可根据业务逻辑自行修改 System.out.println("发送请求失败"); } }
}
[java] view plain copy
package net.apishop.www.controller; import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject; /** * httpUrlConnection访问远程接口工具 */
public class Api
{ /** * 方法体说明:向远程接口发起请求,返回字节流类型结果 * param url 接口地址 * param requestMethod 请求方式 * param params 传递参数 重点:参数值需要用Base64进行转码 * return InputStream 返回结果 */ public static InputStream httpRequestToStream(String url, String requestMethod, Map<String, String> params) { InputStream is = ; try { String parameters = ""; boolean hasParams = false; // 将参数集合拼接成特定格式,如name=zhangsan&age=24 for (String key : params.keySet()) { String value = URLEncoder.encode(params.get(key), "UTF-8"); parameters += key + "=" + value + "&"; hasParams = true; } if (hasParams) { parameters = parameters.substring(0, parameters.length() - 1); } // 请求方式是否为get boolean isGet = "get".equalsIgnoreCase(requestMethod); // 请求方式是否为post boolean isPost = "post".equalsIgnoreCase(requestMethod); if (isGet) { url += "?" + parameters; } URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); // 请求的参数类型(使用restlet框架时,为了兼容框架,必须设置Content-Type为“”空) conn.setRequestProperty("Content-Type", "application/octet-stream"); // conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 设置连接超时时间 conn.setConnectTimeout(50000); // 设置读取返回内容超时时间 conn.setReadTimeout(50000); // 设置向HttpURLConnection对象中输出,因为post方式将请求参数放在http正文内,因此需要设置为ture,默认false if (isPost) { conn.setDoOutput(true); } // 设置从HttpURLConnection对象读入,默认为true conn.setDoInput(true); // 设置是否使用缓存,post方式不能使用缓存 if (isPost) { conn.setUseCaches(false); } // 设置请求方式,默认为GET conn.setRequestMethod(requestMethod); // post方式需要将传递的参数输出到conn对象中 if (isPost) { DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(parameters); dos.flush(); dos.close(); } // 从HttpURLConnection对象中读取响应的消息 // 执行该语句时才正式发起请求 is = conn.getInputStream(); } catch(UnsupportedEncodingException e) { e.printStackTrace(); } catch(MalformedURLException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } return is; } public static void main(String args[]) { String url = "https://api.apishop.net/common/postcode/getPostCodeByAddr"; String requestMethod = "POST"; Map<String, String> params = new HashMap<String, String>(); params.put("image", ""); //二维码图片文件 String result = ; try { InputStream is = httpRequestToStream(url, requestMethod, params); byte[] b = new byte[is.available()]; is.read(b); result = new String(b); } catch(IOException e) { e.printStackTrace(); } if (result != ) { JSONObject jsonObject = JSONObject.parseObject(result); String status_code = jsonObject.getString("statusCode"); if (status_code == "000000") { // 状态码为000000, 说明请求成功 System.out.println("请求成功:" + jsonObject.getString("result")); } else { // 状态码非000000, 说明请求失败 System.out.println("请求失败:" + jsonObject.getString("desc")); } } else { // 返回内容异常,发送请求失败,以下可根据业务逻辑自行修改 System.out.println("发送请求失败"); } }
} 复制代码
转载于:https://juejin.im/post/5ae1945b518825672033c469
二维码编解码 Java调用示例代码相关推荐
- 二维码生成api接口调用示例
二维码生成api接口,执行生成二维码,直接用标签调用接口地址即可.如今二维码已经得到极大的普及和流行,这不仅是因为二维码具有易识别.多终端支持的特点,更得利于其所含有的多种信息内容. 接口名称:二维码 ...
- opencv contrib模块 示例 WeChat qrcode微信二维码检测解码
在opencv 4.5.3 的 contrib中, 腾讯WeChatCV团队贡献了wechat_qrcode模块,3行代码即可在opencv中实现微信的扫码功能. 文档链接为https://docs. ...
- 条形码和二维码编码解码工具类源码
有一个好的工具,会让你的开发事半功倍.再将讲这个工具类之前,我先给小白补充一点条形码和二维码(以下基础知识选自,我本科阶段的一本教材:<物联网导论>(刘云浩 编著).有对物联网感兴趣的,可 ...
- opencv二维码识别解码
目的: 使用opencv库识别QR二维码,框出图片中的二维码,并使用开源库Zxing解码,在这过程中学习理解opencv库相应的函数. 环境: 1. window7系统 2. QT create 1. ...
- QR二维码编码解码原理算法介绍
一.什么是二维码: 二维码 (2-dimensional bar code),是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的. 在许多种类的二维条码中,常用 ...
- 标题 Python生成二维码和解码
标题 Python生成二维码和解码 一.首先下载第三方库 方法一:打开cmd命令窗口,通过命令pip install 包名 进行第三方库安装 1.找到自己的Python环境位置目录,输入cmd回车进入 ...
- 后端开发二维码接口供前端调用
好久没有更新博客,最近项目里有个功能是需要生成二维码,所以记录一下这个功能. 之前有用过自己写的二维码工具类结合谷歌的com.google.zxing版本3.3.3,自己写的一个,因为创建的文件比较多 ...
- Java生成二维码与解码二维码
下载QRCode.jar包 下载完成后在cmd命令行执行以下命令 mvn install:install-file -Dfile = QRCode的路径 -DgroupId=QRCode -Darti ...
- java生成二维码以及二维码的解码
1.依赖 <!--二维码--><dependency><groupId>com.google.zxing</groupId><artifactId ...
最新文章
- EXECL导入(检查服务器版本.包括NPOI方式导入.可以通过配置文件信息导入EXECL)代码记录下....
- 自居电路(升压电路)
- hibernate 延迟加载问题探讨
- 科大讯飞语音引擎_科大讯飞的1024:语音技术进一步突破,发布专用芯片
- Python基本数据类型 set()
- Eclipse Ant组件出现unable to find a javac compiler的解决方法
- 企业数字化新战场:低代码究竟是“毒瘤”还是“良药”?
- ibatis 核心原理解析
- c语言形式参数若为b 4,4月全国计算机等级二级C笔试考试题目
- java cronexpression_关于CronTrigger 的cronExpression用法
- COJ 0358 xjr考考你数据结构(根号3)线段树区间修改
- ipv6解析地理位置
- linux下解压system.img,小白如何修改解包打包system.img
- 华为HCIP-DATACOM391-420(821)
- 以太网二层技术——VPLS详解
- 《Cracking the Coding Interview程序员面试金典》----猫狗收容所
- 百度、百事、大众、脸书、WeWork、领航等公司高管变动
- windows输入法输入英文字母中间有空格
- 用 shader effect 实现雨滴落水效果!Cocos Creator 3D !
- 数据结构 堆 heap