二维码生成工具
来源:互联网 发布:收藏品数据公司 编辑:程序博客网 时间:2024/05/16 13:54
import com.google.zxing.*;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import org.apache.commons.codec.binary.Base64;import org.apache.commons.codec.binary.Base64OutputStream;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.*;import java.util.HashMap;import java.util.Map;public class QRCodeUtils { public static final String QRCODE_DEFAULT_CHARSET = "UTF-8"; public static final int QRCODE_DEFAULT_HEIGHT = 220; public static final int QRCODE_DEFAULT_WIDTH = 220; private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; public static void main(String[] args) throws IOException, NotFoundException { String data = "http://www.baidu.com"; File logoFile = new File("E:\\1.jpg"); BufferedImage image = QRCodeUtils.createQRCodeWithLogo(data, logoFile); ImageIO.write(image, "png", new File("E:\\test.png")); System.out.println("done"); } /** * Create qrcode with default settings * * @param data * @return * @author stefli */ public static BufferedImage createQRCode(String data) { return createQRCode(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT); } /** * Create qrcode with default charset * * @param data * @param width * @param height * @return * @author stefli */ public static BufferedImage createQRCode(String data, int width, int height) { return createQRCode(data, QRCODE_DEFAULT_CHARSET, width, height); } /** * Create qrcode with specified charset * * @param data * @param charset * @param width * @param height * @return * @author stefli */ @SuppressWarnings({"unchecked", "rawtypes"}) public static BufferedImage createQRCode(String data, String charset, int width, int height) { Map hint = new HashMap(); hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hint.put(EncodeHintType.CHARACTER_SET, charset); return createQRCode(data, charset, hint, width, height); } /** * Create qrcode with specified hint * * @param data * @param charset * @param hint * @param width * @param height * @return * @author stefli */ public static BufferedImage createQRCode(String data, String charset, Map<EncodeHintType, ?> hint, int width, int height) { BitMatrix matrix; try { matrix = new MultiFormatWriter().encode(new String(data.getBytes(charset), charset), BarcodeFormat.QR_CODE, width, height, hint); return toBufferedImage(matrix); } catch (WriterException e) { throw new RuntimeException(e.getMessage(), e); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } 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; } /** * Create qrcode with default settings and logo * * @param data * @param logoFile * @return * @author stefli */ public static BufferedImage createQRCodeWithLogo(String data, File logoFile) { return createQRCodeWithLogo(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT, logoFile); } /** * Create qrcode with default charset and logo * * @param data * @param width * @param height * @param logoFile * @return * @author stefli */ public static BufferedImage createQRCodeWithLogo(String data, int width, int height, File logoFile) { return createQRCodeWithLogo(data, QRCODE_DEFAULT_CHARSET, width, height, logoFile); } /** * Create qrcode with specified charset and logo * * @param data * @param charset * @param width * @param height * @param logoFile * @return * @author stefli */ @SuppressWarnings({"unchecked", "rawtypes"}) public static BufferedImage createQRCodeWithLogo(String data, String charset, int width, int height, File logoFile) { Map hint = new HashMap(); hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hint.put(EncodeHintType.CHARACTER_SET, charset); return createQRCodeWithLogo(data, charset, hint, width, height, logoFile); } /** * Create qrcode with specified hint and logo * * @param data * @param charset * @param hint * @param width * @param height * @param logoFile * @return * @author stefli */ public static BufferedImage createQRCodeWithLogo(String data, String charset, Map<EncodeHintType, ?> hint, int width, int height, File logoFile) { try { BufferedImage qrcode = createQRCode(data, charset, hint, width, height); BufferedImage logo = ImageIO.read(logoFile); /** * 设置logo的大小,设置为二维码图片的20%,因为过大会盖掉二维码 */ int widthLogo = logo.getWidth(null) > qrcode.getWidth() * 1 / 5 ? (qrcode.getWidth() * 1 / 5) : logo.getWidth(null); int heightLogo = logo.getHeight(null) > qrcode.getHeight() * 1 / 5 ? (qrcode.getHeight() * 1 / 5) : logo.getWidth(null); /** * logo放在中心 */ int x = (qrcode.getWidth() - widthLogo) / 2; int y = (qrcode.getHeight() - heightLogo) / 2; //开始绘制图片 Graphics2D g = qrcode.createGraphics(); g.drawImage(logo, x, y, widthLogo, heightLogo, null); g.dispose(); return qrcode; } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } /** * Return base64 for image * * @param image * @return * @author stefli */ public static String getImageBase64String(BufferedImage image) { String result = null; try { ByteArrayOutputStream os = new ByteArrayOutputStream(); OutputStream b64 = new Base64OutputStream(os); ImageIO.write(image, "png", b64); result = os.toString("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage(), e); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } return result; } /** * Decode the base64Image data to image * * @param base64ImageString * @param file * @author stefli */ public static void convertBase64StringToImage(String base64ImageString, File file) { FileOutputStream os; try { Base64 d = new Base64(); byte[] bs = d.decode(base64ImageString); os = new FileOutputStream(file.getAbsolutePath()); os.write(bs); os.close(); } catch (FileNotFoundException e) { throw new RuntimeException(e.getMessage(), e); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }}
转自:https://gitee.com/taotao666/codes/iz5qrg1ohv63pu427k89t36
阅读全文
0 0
- 二维码生成工具
- 生成二维码的工具
- 前端二维码生成工具
- 二维码在线生成工具
- 二维码生成工具类
- 生成二维码工具类
- 二维码生成工具类
- 二维码生成工具
- 二维码生成工具,扫描二维码查看页面
- 生成二维码的三个工具
- linux 安装二维码生成工具
- ZXing二维码生成工具类
- java工具类------生成二维码
- java生成二维码工具类
- Qrcode生成二维码工具类
- java二维码工具类:生成二维码和解析二维码
- java 二维码工具类(二维码生成 解析)
- 二维码识别/生成工具 c#实现
- 求10 个整数中最大值
- java file文件类操作使用方法大全
- Udacity机器学习入门笔记——决策树(Decision Tree)
- 如何利用编辑器添加PDF页眉页脚
- 国外网友制作的“重启关闭PATCHGUARD”工具(支持VISTA~8.1)
- 二维码生成工具
- 读取BMP文件并显示出来(C++实现)
- HDU 6232 Confliction
- Linux三个网络监视器之《二》——nethogs
- android上层相关服务使用
- python圣斗士(十七):令人欲罢不能的正则
- HydroComp Propcad 2005
- IDEA快捷键列表
- pd.read_csv之OSError: Initializing from file failed的解决方案