buildBarCode.java 6.4 KB
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);
	}
}