使用Zxing生成二维码,并添加水印
来源:互联网 发布:程序员的自我修养 书签 编辑:程序博客网 时间:2024/05/21 13:55
简单通过Zxing实现二维码图片中间添加水印功能,即实现以下效果:
1、二维码生成工具类
import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.OutputStream;import java.util.Hashtable;import javax.imageio.ImageIO;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;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 com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;public class MatrixToImageWriterEx { private static final Log logger = LogFactory.getLog(EASWebserviceUtils2.class); private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private static final int LogoPart = 4; /** * 生成二维码前的配置信息 * @param content * @param format * @return */ public static BitMatrix createQRCode(String content,String format,int width,int hight){ Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //指定纠错等级 BitMatrix bitMatrix=null;try {bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, hight, hints);} catch (WriterException e) {logger.error("生成二维码错误",e);} return bitMatrix; } /** * 将logo图片放到二维码中间(水印效果) * 将生成的图片以流的形式输出到页面展示 * @param matrix * @param format * @param outStream * @param logoPath * @throws IOException */ public static void megerToFile(BitMatrix matrix,String format,OutputStream outStream,String logoPath) throws IOException { BufferedImage image = toBufferedImage(matrix); Graphics2D gs = image.createGraphics(); //载入logo图片 BufferedImage img = ImageIO.read(new File(logoPath)); int width = image.getWidth() / LogoPart; int height = image.getHeight() / LogoPart; //logo起始位置,以便logo居中显示 int x = (image.getWidth() - width) / 2; int y = (image.getHeight() - height) / 2; gs.drawImage(img, x, y, img.getWidth(), img.getHeight(), null); gs.dispose(); img.flush(); ImageIO.write(image, format, outStream); } /** * 将logo图片放到二维码中间(水印效果) * 将二维码图片生成到某硬盘路径下 * @param matrix * @param format * @param file * @param logoPath * @throws IOException */ public static void megerToFile2(BitMatrix matrix,String format,String imggePath,String logoPath) throws IOException { BufferedImage image = toBufferedImage(matrix); Graphics2D gs = image.createGraphics(); //载入logo图片 BufferedImage img = ImageIO.read(new File(logoPath)); int width = image.getWidth() / LogoPart; int height = image.getHeight() / LogoPart; //logo居中显示 int x = (image.getWidth() - width) / 2; int y = (image.getHeight() - height) / 2; gs.drawImage(img, x, y, img.getWidth(), img.getHeight(), null); gs.dispose(); img.flush(); ImageIO.write(image, format, new File(imggePath)); } 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; } }2、测试类
import java.io.IOException;import com.google.zxing.common.BitMatrix;public class Test {public static void main(String[] args) throws IOException {String content="http://www.baidu.com";BitMatrix bitMatrix = MatrixToImageWriterEx.createQRCode(content, "jpg", 300, 300);MatrixToImageWriterEx.megerToFile2(bitMatrix, "jpg", "D:\\img\\showImg.jpg", "D:\\img\\logo.jpg");}}
0 0
- 使用Zxing生成二维码,并添加水印
- 使用zxing生成二维码图片,并解析
- 使用zxing生成二维码
- 使用ZXing生成二维码
- 使用zxing生成二维码
- 使用zxing生成二维码
- 使用zxing生成二维码
- 使用zxing生成二维码
- 使用zxing生成二维码
- 使用 zxing生成二维码
- 使用zxing生成二维码
- Android:使用ZXing生成二维码(支持添加Logo图案)
- Android:使用ZXing生成二维码(支持添加Logo图案)
- ZXing 设置二维码颜色,添加背景图片,添加logo,添加水印
- 【ZXing】Android使用zxing生成二维码图片
- java 使用zxing 生成二维码
- java 使用zxing生成二维码
- java 使用zxing 生成二维码
- Mongodb
- Maven系列一pom.xml 配置详解
- Linux 学习笔记1(文件)
- 字典对象与Json字符串相互转换
- ASCII、Unicode和UTF-8编码的区别
- 使用Zxing生成二维码,并添加水印
- Java分页查询&条件查询
- mysql 批量修改表中字段数据成不同的值 用一个表更新另一个表的方法
- list和数组的相互转化及数组的自然排序
- R语言数据分析、展现与实例(02)
- [LCT] BZOJ2002: [Hnoi2010]Bounce 弹飞绵羊
- echarts使用时间轴搭配百度地图定位错误
- 设计模式 简介
- 最近想转行前端,整理了一份复习知识点