<label>配件编号</label>
<input class="easyui-validatebox" type="text" id="pjxxPjbh" name="pjxxPjbh" data-options="required:true" >
<div class="fitem" style="float: left;">
<div class="fitem" >
<label>二维码 </label>
<input type="checkbox" value="" id="tp">图片
<input type="checkbox" value="" id="bh">编号
<input type="checkbox" value="" id="mc">名称
</div>
<div class="fitem">
<label></label>
<input type="button" value="生成二维码" id="getCode" onclick="getCodeFunction()">
</div>
</div>
<div class="fitem" >
<div class="fitem" >
<img id="2code" src="" style="width:100px;height:80px;padding-left:100px">
</div>
<div class="fitem" style="float: right;margin-right: 140px" >
<a>下载</a>
</div>
</div>
function getCodeFunction(){
var pjxxPjbh;
if($('#bh').prop("checked")){
pjxxPjbh = $("#pjxxPjbh").val();
alert(pjxxPjbh);
$.ajax({
type:'POST',
url:'${msUrl}/pjPjxx/getCode.do?pjxxPjbh='+pjxxPjbh+'',
cache: false,
success: function(data){
$("#2code").attr('src','http://127.0.0.1:8080/INSIGMA_WEB/WebRoot/images/'+$("#pjxxPjbh").val()+'.gif');
}
})
}else{
alert('请选择二维码生成方式!');
}
};
@RequestMapping("/getCode")
public void getCode(String pjxxPjbh, HttpServletResponse response, HttpServletRequest request)
throws Exception {
File destImageFile = create2CodeImage(pjxxPjbh,request);
pjPjxxService.get2CodeImage(pjxxPjbh, destImageFile);
//HtmlUtil.writerJson(response, destImageFile.toString());
System.out.println(destImageFile);
}
//service
public File create2CodeImage(String pjbh,HttpServletRequest request){
String filePath = request.getSession().getServletContext()
.getRealPath("")
+ "/WebRoot/images" + "/" + pjbh + ".gif";
File destImageFile = new File(filePath);
File destImageParentFile = destImageFile.getParentFile();
if (!destImageParentFile.isDirectory()) {
destImageParentFile.mkdirs();
}
return destImageFile;
}
/**
* 最终调用该方法生成二维码图片
*
* @param url
* 要生成二维码的url
* @param imgPath
* 二维码生成的绝对路径
* @param logoPath
* 二维码中间logo绝对地址
* @param codeWidth
* 二维码宽度
* @param codeHeight
* 二维码长度
* @param logoLeftMargin
* logo图左边距离
* @param logoTopMargin
* logo图顶部距离
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void get2CodeImage(String pjCode, File imgPath) throws Exception {
int codeWidth = 300;
int codeHeight = 300;
String format = "png";
@SuppressWarnings("rawtypes")
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 矫错级别
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(pjCode,
BarcodeFormat.QR_CODE, codeWidth, codeHeight, hints);
writeToFile(bitMatrix, format, imgPath);
}
/**
*
* @param matrix
* 二维码矩阵相关
* @param format
* 二维码图片格式
* @param file
* 二维码图片文件
* @throws IOException
*/
public static void writeToFile(BitMatrix matrix, String format, File file
)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
ImageIO.write(image, "gif", new File(file.getPath()));
}
public 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;
}

3495

被折叠的 条评论
为什么被折叠?



