buildBarCode.java
6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.thinkgem.jeesite.common.barCode;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.imageio.ImageIO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.thinkgem.jeesite.modules.land.entity.LandRoadVeRecord;
public class buildBarCode {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private static String CREATEBARCODE = "http://10.50.7.11:8088/publiclogistics/createVehicleBarCode";
private static String CANCLEBARCODE = "http://10.50.7.11:8088/publiclogistics/cancelVehicleBarCode";
private static String CREATECAR = "http://10.50.7.11:8088/publiclogistics/saveVehicleRecord";
private static String CANCLECAR = "http://10.50.7.11:8088/publiclogistics/cancelVehicleRecord";
private static String CODE;
private static String MSG;
/**
* 根据字符串生成对应的二维码图片png 大小:200*200
*
* content:要转换的内容 path:生成的二维码图片的绝对路径 filename: 生成的文件名
*/
public static void buildQuickMark(String content, String path, String filename) throws Exception {
try {
BitMatrix byteMatrix = new MultiFormatWriter().encode(new String(content.getBytes(), "ISO-8859-1"),
BarcodeFormat.QR_CODE, 200, 200);
String format = "png";
File file = new File(path + "\\" + filename + "." + format);
BufferedImage image = toBufferedImage(byteMatrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 生成二维码规格
private 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);
}
}
return image;
}
// 发送数据并接收返回数据
public static String sendData(String url, String json) {
StringBuilder str = new StringBuilder();
BufferedReader bf = null;
OutputStreamWriter writer = null;
try {
URL Url = new URL(url);
HttpURLConnection conn = (HttpURLConnection) Url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("X-Rrquested-With", "XMLHttpRequest");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestMethod("POST");
conn.connect();
writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
writer.write(json);
writer.flush();
bf = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String inputLine = null;
while ((inputLine = bf.readLine()) != null) {
str.append(inputLine);
}
writer.close();
bf.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
// 解析返回的json里的data数据
public static Map jsonFormat(String json) {
JSONObject jsonObject = JSONObject.parseObject(json);
CODE = jsonObject.getString("code");
MSG =jsonObject.getString("msg");
Object data = jsonObject.get("data");
Map map =null;
if(data!=null) {
String content = "[" + data.toString() + "]";
JSONArray jsonArray = JSONArray.parseArray(content);
map= JSON.parseObject(jsonArray.getString(0));
}
return map;
}
public static String jsonFormatToString(String json) {
JSONObject jsonObject = JSONObject.parseObject(json);
String code = jsonObject.getString("code");
String msg =jsonObject.getString("msg");
return code;
}
public static String getPath () {
File directory = new File(".");
String path = null;
try {
path = directory.getCanonicalPath();//获取当前路径
} catch (IOException e) {
e.printStackTrace();
}
return path;
}
// 获取每一天的字符串格式
public static String getFileName() {
Date date = new Date();
SimpleDateFormat sp = new SimpleDateFormat("yyyyMMdd");
String st = sp.format(date);
return st;
}
//将日期转化为字符串
public static String getDateTime() {
Date date = new Date();
SimpleDateFormat sp = new SimpleDateFormat("yyyyMMddHHmmss");
String st = sp.format(date);
return st;
}
// 生成二维码
public static String CreateBarCode(String carCode,String mainifist) {
String json = "{\"token\":\"samples\",\"data\":{\"vehicle_no\":\""+carCode+"\",\"vehicle_no_color\":\"黄\",\"vehicle_rela_id\":\""+mainifist+"\"}}";
String content = buildBarCode.sendData(CREATEBARCODE, json);
String str = null;
try {
Map map = buildBarCode.jsonFormat(content);
str = map.get("vehicle_bar_code").toString();
}catch (Exception e) {
e.printStackTrace();
}
return str;
}
// 取消二维码
public static void cancleBarCode(String frameNo) {
String json = "{\"token\":\"samples\",\"data\":{\"vehicle_no\":\""+frameNo+"\",\"vehicle_no_color\":\"黄\"}}";
buildBarCode.sendData(CANCLEBARCODE, json);
}
// 车辆备案
public static String crateCar(LandRoadVeRecord ve) {
String json = "{\"token\":\"samples\",\"data\":{\"customs_code\":\""+ve.getMainPort()+"\",\"port_code\":\"001\","
+ "\"vehicle_no\":\""+ve.getDomesticLisenceNo()+"\",\"vehicle_no_color\":\"黄\""
+ ",\"vehicle_weight\":\""+ve.getSelfWt()+"\""
+ ",\"vehicle_type\":\"1\",\"register_at\":\""+getDateTime()+"\"}}";
String content = buildBarCode.sendData(CREATECAR, json);
String code = jsonFormatToString(content);
return code;
}
// 取消车辆备案
public static void cancleCar(LandRoadVeRecord ve) {
String json = "{\"token\":\"samples\",\"data\":{\"customs_code\":\""+ve.getMainPort()+"\",\"port_code\":\"001\","
+ "\"vehicle_no\":\""+ve.getDomesticLisenceNo()+"\",\"vehicle_no_color\":\"黄\"}}";
buildBarCode.sendData(CANCLECAR, json);
}
}