<dependency >
<groupId > com.google.zxing</groupId >
<artifactId > core</artifactId >
<version > 3.3.3</version >
</dependency >
<dependency >
<groupId > com.google.zxing</groupId >
<artifactId > javase</artifactId >
<version > 2.3.0</version >
</dependency >
package com.ytx.mobile.trade.restful;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
* @description QRCODE 条形码 需要javase.jar和core.jar两个包
*/
@Controller
@RequestMapping ("/comm/qrbar" )
public class QrBarCodeController {
/**
* 创建二维码
*/
@RequestMapping (value = "/create" , method = RequestMethod.GET)
public void create (HttpServletRequest request, HttpServletResponse response) {
int onColor = 0xFF000001 ;
int offColor = 0xFFFFFFFF ;
String code = request.getParameter("text" );
String width = request.getParameter("width" );
String outBackground = request.getParameter("outbg" );
String inBackground = request.getParameter("inbg" );
if (StringUtils.hasText(outBackground)) {
if (ColorUtil.getIntColorByName(outBackground) != 0 )
offColor = ColorUtil.getIntColorByName(outBackground);
}
if (StringUtils.hasText(inBackground)) {
if (ColorUtil.getIntColorByName(inBackground) != 0 )
onColor = ColorUtil.getIntColorByName(inBackground);
}
int size = 300 ;
if (StringUtils.hasText(width)) {
size = Integer.parseInt(width);
}
response.setHeader("Pragma" , "No-cache" );
response.setHeader("Cache-Control" , "no-cache" );
response.setDateHeader("Expires" , 0 );
response.setContentType("image/jpeg" );
Map hints = new HashMap();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
hints.put(EncodeHintType.MARGIN, 1 );
hints.put(EncodeHintType.CHARACTER_SET, "utf-8" );
try {
MatrixToImageConfig config = new MatrixToImageConfig(onColor, offColor);
BitMatrix bitMatrix = new MultiFormatWriter().encode(code,
BarcodeFormat.QR_CODE, size, size, hints);
MatrixToImageWriter.writeToStream(bitMatrix, "jpeg" , response.getOutputStream(), config);
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成名片二维码
* 如:http://localhost:8180/rest/comm/qrbar/create/card?name=%E7%8E%8B%E8%BF%9B&email=123445914@163.com&mobile=13878786716¬e=123
*
* @param name 姓名
* @param mobile 手机
* @param email email
* @param company 公司名称
* @param appellation 称呼
* @param address 地址
* @param note 备注
* @return void 返回类型
* @throws
*/
@RequestMapping (value = "/create/card" , method = RequestMethod.GET)
@ResponseBody
public void card (HttpServletRequest request, HttpServletResponse response) {
try {
String name = request.getParameter("name" );
String mobile = request.getParameter("mobile" );
String email = request.getParameter("email" );
String company = request.getParameter("company" );
String appellation = request.getParameter("appellation" );
String address = request.getParameter("address" );
String note = request.getParameter("note" );
String content = "BEGIN:VCARD\n" +
"VERSION:3.0\n" +
"N:" + (StringUtils.hasText(name) ? "" : name) + (StringUtils.hasText(appellation) ? "" : "-" + appellation) + "\n" +
"EMAIL:" + (StringUtils.hasText(email) ? "" : email) + "\n" +
"TEL:" + (StringUtils.hasText(mobile) ? "" : mobile) + "\n" +
"ADR:" + (StringUtils.hasText(address) ? "" : address) + "\n" +
"ORG:" +
"" + (StringUtils.hasText(company) ? "" : company) + "\n" +
"NOTE:" + (StringUtils.hasText(note) ? "" : note) + "\n" +
"END:VCARD" ;
QRCodeWriter writer = new QRCodeWriter();
response.setHeader("Pragma" , "No-cache" );
response.setHeader("Cache-Control" , "no-cache" );
response.setDateHeader("Expires" , 0 );
response.setContentType("image/jpeg" );
Map hints = new HashMap();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8" );
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, 300 , 300 , hints);
MatrixToImageWriter.writeToStream(bitMatrix, "jpeg" , response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建二维码并下载
*
* @param code 传入
* @param @param response 设定文件
* @return void 返回类型
* @throws
*/
@RequestMapping (value = "/createAndDownload" , method = RequestMethod.GET)
public void createAndDownload (HttpServletRequest request, HttpServletResponse response) {
String code = request.getParameter("text" );
if (StringUtils.hasText(code)) {
int i = 1 / 0 ;
}
String w = request.getParameter("w" );
int size = 300 ;
if (StringUtils.hasText(w)) {
size = Integer.parseInt(w);
}
QRCodeWriter writer = new QRCodeWriter();
response.reset();
response.setHeader("Content-Disposition" , "attachment; filename=qrcode.png" );
response.setContentType("image/png " );
Map hints = new HashMap();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
hints.put(EncodeHintType.MARGIN, 1 );
hints.put(EncodeHintType.CHARACTER_SET, "utf-8" );
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(code,
BarcodeFormat.QR_CODE, size, size, hints);
MatrixToImageWriter.writeToStream(bitMatrix, "jpeg" , response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.ytx.mobile.trade.restful;
import java.util.HashMap;
import java.util.Map;
public class ColorUtil {
public static int getIntColorByName (String name){
Map<String , Integer> m = new HashMap();
m.put("black" , 0x000000 );
m.put("maroon" , 0x800000 );
m.put("green" , 0x008000 );
m.put("olive" , 0x808000 );
m.put("navy" , 0x000080 );
m.put("purple" , 0x800080 );
m.put("teal" , 0x008080 );
m.put("gray" , 0x808080 );
m.put("silver" , 0xC0C0C0 );
m.put("red" , 0xFF0000 );
m.put("lime" , 0x00FF00 );
m.put("yellow" , 0xFFFF00 );
m.put("blue" , 0x0000FF );
m.put("fuchsia" , 0xFF00FF );
m.put("aqua" , 0x00FFFF );
m.put("white" , 0xFFFFFF );
m.put("aliceblue" , 0xF0F8FF );
m.put("antiquewhite" , 0xFAEBD7 );
m.put("aquamarine" , 0x7FFFD4 );
m.put("azure" , 0xF0FFFF );
m.put("beige" , 0xF5F5DC );
m.put("blueviolet" , 0x8A2BE2 );
m.put("brown" , 0xA52A2A );
m.put("burlywood" , 0xDEB887 );
m.put("cadetblue" , 0x5F9EA0 );
m.put("chartreuse" , 0x7FFF00 );
m.put("chocolate" , 0xD2691E );
m.put("coral" , 0xFF7F50 );
m.put("cornflowerblue" , 0x6495ED );
m.put("cornsilk" , 0xFFF8DC );
m.put("crimson" , 0xDC143C );
m.put("darkblue" , 0x00008B );
m.put("darkcyan" , 0x008B8B );
m.put("darkgoldenrod" , 0xB8860B );
m.put("darkgray" , 0xA9A9A9 );
m.put("darkgreen" , 0x006400 );
m.put("darkkhaki" , 0xBDB76B );
m.put("darkmagenta" , 0x8B008B );
m.put("darkolivegreen" , 0x556B2F );
m.put("darkorange" , 0xFF8C00 );
m.put("darkorchid" , 0x9932CC );
m.put("darkred" , 0x8B0000 );
m.put("darksalmon" , 0xE9967A );
m.put("darkseagreen" , 0x8FBC8F );
m.put("darkslateblue" , 0x483D8B );
m.put("darkslategray" , 0x2F4F4F );
m.put("darkturquoise" , 0x00CED1 );
m.put("darkviolet" , 0x9400D3 );
m.put("deeppink" , 0xFF1493 );
m.put("deepskyblue" , 0x00BFFF );
m.put("dimgray" , 0x696969 );
m.put("dodgerblue" , 0x1E90FF );
m.put("firebrick" , 0xB22222 );
m.put("forestgreen" , 0x228B22 );
m.put("gainsboro" , 0xDCDCDC );
m.put("ghostwhite" , 0xF8F8FF );
m.put("gold" , 0xFFD700 );
m.put("goldenrod" , 0xDAA520 );
m.put("greenyellow" , 0xADFF2F );
m.put("honeydew" , 0xF0FFF0 );
m.put("hotpink" , 0xFF69B4 );
m.put("indianred" , 0xCD5C5C );
m.put("indigo" , 0x4B0082 );
m.put("ivory" , 0xFFFFF0 );
m.put("khaki" , 0xF0E68C );
m.put("lavender" , 0xE6E6FA );
m.put("lavenderblush" , 0xFFF0F5 );
m.put("lawngreen" , 0x7CFC00 );
m.put("lemonchiffon" , 0xFFFACD );
m.put("lightblue" , 0xADD8E6 );
m.put("lightcoral" , 0xF08080 );
m.put("lightcyan" , 0xE0FFFF );
m.put("lightgoldenrodyellow" , 0xFAFAD2 );
m.put("lightgreen" , 0x90EE90 );
m.put("lightgrey" , 0xD3D3D3 );
m.put("lightpink" , 0xFFB6C1 );
m.put("lightsalmon" , 0xFFA07A );
m.put("lightseagreen" , 0x20B2AA );
m.put("lightskyblue" , 0x87CEFA );
m.put("lightslategray" , 0x778899 );
m.put("lightsteelblue" , 0xB0C4DE );
m.put("lightyellow" , 0xFFFFE0 );
m.put("limegreen" , 0x32CD32 );
m.put("linen" , 0xFAF0E6 );
m.put("mediumaquamarine" , 0x66CDAA );
m.put("mediumblue" , 0x0000CD );
m.put("mediumorchid" , 0xBA55D3 );
m.put("mediumpurple" , 0x9370D0 );
m.put("mediumseagreen" , 0x3CB371 );
m.put("mediumslateblue" , 0x7B68EE );
m.put("mediumspringgreen" , 0x00FA9A );
m.put("mediumturquoise" , 0x48D1CC );
m.put("mediumvioletred" , 0xC71585 );
m.put("midnightblue" , 0x191970 );
m.put("mintcream" , 0xF5FFFA );
m.put("mistyrose" , 0xFFE4E1 );
m.put("moccasin" , 0xFFE4B5 );
m.put("navajowhite" , 0xFFDEAD );
m.put("oldlace" , 0xFDF5E6 );
m.put("olivedrab" , 0x6B8E23 );
m.put("orange" , 0xFFA500 );
m.put("orangered" , 0xFF4500 );
m.put("orchid" , 0xDA70D6 );
m.put("palegoldenrod" , 0xEEE8AA );
m.put("palegreen" , 0x98FB98 );
m.put("paleturquoise" , 0xAFEEEE );
m.put("palevioletred" , 0xDB7093 );
m.put("papayawhip" , 0xFFEFD5 );
m.put("peachpuff" , 0xFFDAB9 );
m.put("peru" , 0xCD853F );
m.put("pink" , 0xFFC0CB );
m.put("plum" , 0xDDA0DD );
m.put("powderblue" , 0xB0E0E6 );
m.put("rosybrown" , 0xBC8F8F );
m.put("royalblue" , 0x4169E1 );
m.put("saddlebrown" , 0x8B4513 );
m.put("salmon" , 0xFA8072 );
m.put("sandybrown" , 0xF4A460 );
m.put("seagreen" , 0x2E8B57 );
m.put("seashell" , 0xFFF5EE );
m.put("sienna" , 0xA0522D );
m.put("skyblue" , 0x87CEEB );
m.put("slateblue" , 0x6A5ACD );
m.put("slategray" , 0x708090 );
m.put("snow" , 0xFFFAFA );
m.put("steelblue" , 0x4682B4 );
m.put("tan" , 0xD2B48C );
m.put("thistle" , 0xD8BFD8 );
m.put("tomato" , 0xFF6347 );
m.put("turquoise" , 0x40E0D0 );
m.put("violet" , 0xEE82EE );
m.put("wheat" , 0xF5DEB3 );
m.put("whitesmoke" , 0xF5F5F5 );
m.put("yellowgreen" , 0x9ACD32 );
return m.get(name)==null ?0 :m.get(name);
}
}