ZXing生成二维码
2016-07-25 09:11
253 查看
package com.lutai.user.service; import com.google.zxing.*; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.net.URL; import java.nio.file.FileSystems; import java.nio.file.Path; import java.util.HashMap; import java.util.Map; /** * ZXingCodeService * * @author FD * @date 2016/5/16 */ public class ZXingCodeService { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private static BufferedImage logoImg; enum FormatType{ JPG; } static { try { URL logoUrl = ZXingCodeService.class.getClassLoader().getResource("logo-min.png"); if (logoUrl != null) { logoImg = ImageIO.read(logoUrl); } } catch (IOException e) { e.printStackTrace(); } } /** * 生成二维码参数 * * @return */ private Map<EncodeHintType, Object> getEncodeHintType() { // 用于设置QR二维码参数 Map<EncodeHintType, Object> hints = new HashMap<>(); // 设置编码方式 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // // 设置QR二维码的纠错级别(H为最高级别)具体级别信息 // hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // hints.put(EncodeHintType.MARGIN, 0); return hints; } /** * 根据BitMatrix生成BufferedImage * @param bitMatrix * @return */ private BufferedImage getBufferedImage(BitMatrix bitMatrix) { int width = bitMatrix.getWidth(); int height = bitMatrix.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, bitMatrix.get(x, y) ? BLACK : WHITE); } } return image; } /** * 将BufferedImage写到文件中 * @param bitMatrix * @param format * @param file */ public void write2File(BitMatrix bitMatrix, String format, File file) { BufferedImage bufferedImage = getBufferedImage(bitMatrix); try { ImageIO.write(bufferedImage, format, file); } catch (IOException e) { e.printStackTrace(); } } /** * 将BufferedImage写到流中 * @param bitMatrix * @param format * @param os */ public void write2Stream(BitMatrix bitMatrix, String format, OutputStream os) { BufferedImage bufferedImage = getBufferedImage(bitMatrix); try { ImageIO.write(bufferedImage, format, os); } catch (IOException e) { e.printStackTrace(); } } public void addLogo() { } public String encode(String filePath, String fileName) { Path path = FileSystems.getDefault().getPath(filePath, fileName); String content = "www.baidu.com"; int width = 300, height = 300; Map hints = getEncodeHintType(); try { // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); MatrixToImageWriter.writeToPath(bitMatrix, "JPG", path); } catch (WriterException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return path.toString(); } public String decode(String filePath) { BufferedImage image = null; try { image = ImageIO.read(new File(filePath)); LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); Map<DecodeHintType, Object> hints = new HashMap<>(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); Result result = new MultiFormatReader().decode(binaryBitmap, hints); return result.getText(); } catch (IOException e) { e.printStackTrace(); } catch (NotFoundException e) { e.printStackTrace(); } return ""; } public static void main(String[] args) { // System.out.println(ZXingCodeService.class.getClassLoader().getResource("")); // System.out.println(ZXingCodeService.class.getClassLoader().getResource("jsonUtil.class")); // System.out.println(ZXingCodeService.class.getClassLoader().getResource("logback.xml")); System.out.println(new ZXingCodeService().encode("D://fd", "fd.jpg")); System.out.println(new ZXingCodeService().decode("D://fd/fd.jpg")); } }
相关文章推荐
- Spring data jpa配置出错
- java NIO实现同步非阻塞服务器
- 搞笑版卷积讲解
- Perforce安装
- ICE的异步方法调用
- Hadoop:Java API实现
- poj3321 Apple Tree
- 基于SOA思想下的WebService实战|WebService视频|SOA视频
- Linux 下编译安装OpenCV
- linux下查看软件的安装路径
- spring+quartz实现定时任务(每天零点删除指定文件夹中缓存文件)
- Android Studio 后台执行的定时操作
- SpringMVC 的第一个程序——HelloWorld
- Log4J日志配置详解
- pat 1021. Deepest Root (25)
- Scala面向对象学习
- “Options模式”下的配置是如何绑定为Options对象
- 无人机领域小神器,P440超宽带射频模块表现这么给力
- Node.js安装配置
- 【转】移动端轮播插件,zepto轮播插件