com.jfinal.render;
com.google.zxing.BarcodeFormat;
com.google.zxing.EncodeHintType;
com.google.zxing.MultiFormatWriter;
com.google.zxing.WriterException;
com.google.zxing.client.j2se.MatrixToImageWriter;
com.google.zxing.common.BitMatrix;
com.google.zxing.qrcode.QRCodeWriter;
com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
com.jfinal.kit.StrKit;
sun.misc.IOUtils;
sun.nio.ch.IOUtil;
javax.imageio.ImageIO;
javax.servlet.ServletOutputStream;
java.awt.*;
java.awt.image.BufferedImage;
java.io.ByteArrayInputStream;
java.io.ByteArrayOutputStream;
java.io.File;
java.io.InputStream;
java.util.HashMap;
java.util.Map;
QRImageRender Render {
= ; = ; ErrorCorrectionLevel ; String ; ; ; File ; String ;QRImageRender(String content, width, height,File file) {
init(content, width, height, ,file,);
}
QRImageRender(String content, width, height,File file,String bottomContent) {
init(content, width, height, ,file,bottomContent);
}
init(String content, width, height, ErrorCorrectionLevel errorCorrectionLevel,File file,String bottomContent) {
(StrKit.(content)) {
IllegalArgumentException();
}
(width < || height < ) {
IllegalArgumentException();
}
(file==||!file.exists ()||file.isDirectory ()){
IllegalArgumentException();
}
.= content;
.= width;
.= height;
.= errorCorrectionLevel;
.=file;
(bottomContent==||bottomContent.isEmpty ()){
.=;
}{
.=bottomContent;
}
}
render() {
.setHeader(,);
.setHeader(,);
.setDateHeader(, );
.setContentType();
{
BufferedImage bim = getQR_CODEBufferedImage(, BarcodeFormat., , , getDecodeHintType());
InputStream inputStream=addLogo_QRCode(bim, File(.getPath ()), LogoConfig(), ,,);
len=;
[] data=[];
ServletOutputStream sos=.getOutputStream ();
{
((len=inputStream.read ( data ))!=-){
sos.write (data,,len);
}
}(Exception e){
RenderException ( );
}{
(sos!=){
sos.close ();
}
(inputStream!=){
inputStream.close ();
}
}
}(Exception e){
RenderException(e);
}
}
InputStream addLogo_QRCode(BufferedImage bim, File logoPic, LogoConfig logoConfig, String productName, width, height)
{
{
BufferedImage image = bim;
Graphics2D g = image.createGraphics();
BufferedImage logo = ImageIO.(logoPic);
widthLogo = logo.getWidth()>image.getWidth()*/?(image.getWidth()*/):logo.getWidth(),
heightLogo = logo.getHeight()>image.getHeight()*/?(image.getHeight()*/):logo.getWidth();
x = (image.getWidth() - widthLogo) / ;
y = (image.getHeight() - heightLogo) / ;
g.drawImage(logo, x, y, widthLogo, heightLogo, );
g.dispose();
(productName != && !productName.equals()) {
BufferedImage outImage = BufferedImage(width, height+, BufferedImage.);
Graphics2D outg = outImage.createGraphics();
outg.drawImage(image, , , image.getWidth(), image.getHeight(), );
outg.setColor(Color.);
outg.setFont(Font(,Font.,)); strWidth = outg.getFontMetrics().stringWidth(productName);
(strWidth > ) {
String productName1 = productName.substring(, productName.length()/);
String productName2 = productName.substring(productName.length()/, productName.length());
strWidth1 = outg.getFontMetrics().stringWidth(productName1);
strWidth2 = outg.getFontMetrics().stringWidth(productName2);
outg.drawString(productName1, strWidth1/, image.getHeight() + (outImage.getHeight() - image.getHeight())/+ );
BufferedImage outImage2 = BufferedImage(width, height+, BufferedImage.);
Graphics2D outg2 = outImage2.createGraphics();
outg2.drawImage(outImage, , , outImage.getWidth(), outImage.getHeight(), );
outg2.setColor(Color.);
outg2.setFont(Font(,Font.,)); outg2.drawString(productName2, strWidth2/, outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight())/+ );
outg2.dispose();
outImage2.flush();
outImage = outImage2;
}{
outg.drawString(productName, (width-strWidth+)/, image.getHeight() + (outImage.getHeight() - image.getHeight())/+ ); }
outg.dispose();
outImage.flush();
image = outImage;
}
logo.flush();
image.flush();
ByteArrayOutputStream baos = ByteArrayOutputStream();
ImageIO.(image, , baos);
InputStream is = ByteArrayInputStream (baos.toByteArray());
is;
}
(Exception e)
{
e.printStackTrace();
}
;
}
BufferedImage fileToBufferedImage(BitMatrix bm)
{
BufferedImage image = ;
{
w = bm.getWidth(), h = bm.getHeight();
image = BufferedImage(w, h, BufferedImage.);
(x = ; x < w; x++)
{
(y = ; y < h; y++)
{
image.setRGB(x, y, bm.get(x, y) ? : );
}
}
}
(Exception e)
{
e.printStackTrace();
}
image;
}
BufferedImage getQR_CODEBufferedImage(String content, BarcodeFormat barcodeFormat, width, height, Map<EncodeHintType, ?> hints)
{
MultiFormatWriter multiFormatWriter = ;
BitMatrix bm = ;
BufferedImage image = ;
{
multiFormatWriter = MultiFormatWriter();
bm = multiFormatWriter.encode(content, barcodeFormat, width, height, hints);
w = bm.getWidth();
h = bm.getHeight();
image = BufferedImage(w, h, BufferedImage.);
(x = ; x < w; x++)
{
(y = ; y < h; y++)
{
image.setRGB(x, y, bm.get(x, y) ? : );
}
}
}
(WriterException e)
{
e.printStackTrace();
}
image;
}
Map<EncodeHintType, Object> getDecodeHintType()
{
Map<EncodeHintType, Object> hints = HashMap<EncodeHintType, Object> ();
hints.put(EncodeHintType., ErrorCorrectionLevel.);
hints.put(EncodeHintType., );
hints.put(EncodeHintType., );
hints.put(EncodeHintType., );
hints.put(EncodeHintType., );
hints;
}
}
LogoConfig
{
Color = Color.;
= ;
= ;
= ;
Color ;
;
LogoConfig()
{
(, );
}
LogoConfig(Color borderColor, logoPart)
{
.= borderColor;
.= logoPart;
}
Color getBorderColor()
{
;
}
getBorder()
{
;
}
getLogoPart()
{
;
}
}