package com.times.util; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Hashtable; import java.util.Random; import java.awt.image.BufferedImage; /** * 生成二维码 * * */ public final class MatrixToImageWriter { static HttpServletRequest rq = SessionUtil.getRequest(); public static String rq_url = "https://"+ rq.getRemoteAddr()+":"+rq.getRemotePort()+"/"; private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private MatrixToImageWriter() {} 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; } public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } } /** * 获取二维码名称生成二维码 * @return */ public static String[] getQR_CODE(String f_appUrl,int width,int height) throws Exception{ //图片名称 String image_name = getRandomChar(10); //二维码的图片格式 String format = "jpg"; //二维码路径 String newImageName = SessionUtil.getSession().getServletContext().getRealPath("file")+"/"+image_name+".jpg"; Hashtable hints = new Hashtable(); //内容所使用编码 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(f_appUrl, BarcodeFormat.QR_CODE, width, height, hints); //生成二维码 File outputFile = new File(newImageName); MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile); //上传到图片服务器 File imgFile = new File(newImageName+".jpg"); InputStream in = new FileInputStream(imgFile); String[] resString = TimesHttpClient.uploadImage2(in,imgFile.getName()); imgFile.delete(); return resString; } /** * 根据内容生成二维码BufferedImage * @author 向龙飞 * @date 2015-10-10 * @param f_appUrl * @param width * @param height * @return * @throws WriterException */ public static BufferedImage getQr_imgbuffer(String f_appUrl,int width,int height) throws WriterException{ Hashtable hints = new Hashtable(); //内容所使用编码 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(f_appUrl, BarcodeFormat.QR_CODE, width, height, hints); //获取BufferedImage BufferedImage image = toBufferedImage(bitMatrix); return image; } /** * 获得0-9,a-z,A-Z范围的随机数 + 当前时间 * * * @date 2014-12-04 * @param length * 随机数长度 * @return String */ public static String getRandomChar(int length) { char[] chr = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘ }; Random random = new Random(); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < length; i++) { buffer.append(chr[random.nextInt(62)]); } // 获取当前时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH-mm-ss"); Date date = new Date(); String low_date = sdf.format(date); buffer.append(low_date); return buffer.toString(); } public String getRq_url() { return rq_url; } public void setRq_url(String rq_url) { this.rq_url = rq_url; } }
本文出自 “IdLong” 博客,谢绝转载!
原文地址:http://idlong.blog.51cto.com/10631184/1716989