Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.yixan.base.web.controller.QrCodeLinkImageController Maven / Gradle / Ivy
package com.yixan.base.web.controller;
import java.io.IOException ;
import java.io.OutputStream ;
import java.util.HashMap ;
import java.util.Map ;
import java.util.Properties ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Controller ;
import org.springframework.web.bind.annotation.PathVariable ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RequestMethod ;
import com.yixan.base.annotation.OperateRecord ;
import com.yixan.tools.common.util.QrCode ;
import com.yixan.tools.common.util.StringUtil ;
import com.yixan.tools.common.util.config.Config ;
@Controller
@RequestMapping ("qrcode/link" )
@OperateRecord (enable = false )
public class QrCodeLinkImageController {
private static final Logger log = LoggerFactory .getLogger(QrCodeLinkImageController .class );
@Autowired
private Properties setting;
@RequestMapping (value = "detail/{type}/{id}.png" , method = RequestMethod .GET )
public void qrcode(@PathVariable ("type" ) String type , @PathVariable ("id" ) String id , HttpServletRequest request ,
HttpServletResponse response) {
Map params = new HashMap <>();
params.put("type" , type ) ;
params.put("id" , id);
generateQrCode(type , params , request , response ) ;
}
@RequestMapping (value = "detail/{type}/{module}/{id}.png" , method = RequestMethod .GET )
public void qrcode(@PathVariable ("type" ) String type , @PathVariable ("module" ) String module ,
@PathVariable ("id" ) String id, HttpServletRequest request, HttpServletResponse response) {
Map params = new HashMap <>();
params.put("type" , type ) ;
params.put("module" , module);
params.put("id" , id);
generateQrCode(type , params , request , response ) ;
}
@RequestMapping (value = "detail/{type}/{module}/{channel}/{id}.png" , method = RequestMethod .GET )
public void qrcode(@PathVariable ("type" ) String type , @PathVariable ("module" ) String module ,
@PathVariable ("channel" ) String channel, @PathVariable ("id" ) String id, HttpServletRequest request,
HttpServletResponse response) {
Map params = new HashMap <>();
params.put("type" , type ) ;
params.put("module" , module);
params.put("channel" , channel);
params.put("id" , id);
generateQrCode(type , params , request , response ) ;
}
private void generateQrCode(String type , Map params , HttpServletRequest request ,
HttpServletResponse response) {
try {
String string = Config .getRealValue(setting, "qrcode." + type , true ) ;
String content = StringUtil .format(string, params);
int size = getIntegerValue("qrcode." + type + ".size ", 200) ;
int margin = getIntegerValue("qrcode." + type + ".margin ", 0) ;
response.setContentType("image/png" );
OutputStream os = response.getOutputStream();
QrCode .generate(content, size, margin, os);
os.flush();
os.close();
} catch (IOException e) {
log.error("Failed to generate qr code, type={}, params={}" , type , StringUtil .toJsonString (params ), e ) ;
}
}
private int getIntegerValue(String key, int defvalue) {
if (setting.containsKey(key)) {
String value = setting.getProperty(key);
try {
return Integer .valueOf(value);
} catch (Exception ignore) {
log.warn("Config value '{} = {}' can't convert to integer number." , key, value);
}
}
return defvalue;
}
}